attr_internal_naming_format | [RW] |
Allows you to make aliases for attributes, which includes getter, setter, and query methods.
Example:
class Content < ActiveRecord::Base # has a title attribute end class Email < Content alias_attribute :subject, :title end e = Email.find(1) e.title # => "Superstars" e.subject # => "Superstars" e.subject? # => true e.subject = "Megastars" e.title # => "Megastars"
Encapsulates the common pattern of:
alias_method :foo_without_feature, :foo alias_method :foo, :foo_with_feature
With this, you simply do:
alias_method_chain :foo, :feature
And both aliases are set up for you.
Query and bang methods (foo?, foo!) keep the same punctuation:
alias_method_chain :foo?, :feature
is equivalent to
alias_method :foo_without_feature?, :foo? alias_method :foo?, :foo_with_feature?
so you can safely chain foo, foo?, and foo! with the same feature.
A module may or may not have a name.
module M; end M.name # => "M" m = Module.new m.name # => ""
A module gets a name when it is first assigned to a constant. Either via the module or class keyword or by an explicit assignment:
m = Module.new # creates an anonymous module M = m # => m gets a name here as a side-effect m.name # => "M"
Declare an attribute accessor with an initial default return value.
To give attribute :age the initial value 25:
class Person attr_accessor_with_default :age, 25 end person = Person.new person.age # => 25 person.age = 26 person.age # => 26
To give attribute :element_name a dynamic default value, evaluated in scope of self:
attr_accessor_with_default(:element_name) { name.underscore }
Provides a delegate class method to easily expose contained objects’ methods as your own. Pass one or more methods (specified as symbols or strings) and the name of the target object via the :to option (also a symbol or string). At least one method and the :to option are required.
Delegation is particularly useful with Active Record associations:
class Greeter < ActiveRecord::Base def hello "hello" end def goodbye "goodbye" end end class Foo < ActiveRecord::Base belongs_to :greeter delegate :hello, :to => :greeter end Foo.new.hello # => "hello" Foo.new.goodbye # => NoMethodError: undefined method `goodbye' for #<Foo:0x1af30c>
Multiple delegates to the same target are allowed:
class Foo < ActiveRecord::Base belongs_to :greeter delegate :hello, :goodbye, :to => :greeter end Foo.new.goodbye # => "goodbye"
Methods can be delegated to instance variables, class variables, or constants by providing them as a symbols:
class Foo CONSTANT_ARRAY = [0,1,2,3] @@class_array = [4,5,6,7] def initialize @instance_array = [8,9,10,11] end delegate :sum, :to => :CONSTANT_ARRAY delegate :min, :to => :@@class_array delegate :max, :to => :@instance_array end Foo.new.sum # => 6 Foo.new.min # => 4 Foo.new.max # => 11
Delegates can optionally be prefixed using the :prefix option. If the value is true, the delegate methods are prefixed with the name of the object being delegated to.
Person = Struct.new(:name, :address) class Invoice < Struct.new(:client) delegate :name, :address, :to => :client, :prefix => true end john_doe = Person.new("John Doe", "Vimmersvej 13") invoice = Invoice.new(john_doe) invoice.client_name # => "John Doe" invoice.client_address # => "Vimmersvej 13"
It is also possible to supply a custom prefix.
class Invoice < Struct.new(:client) delegate :name, :address, :to => :client, :prefix => :customer end invoice = Invoice.new(john_doe) invoice.customer_name # => "John Doe" invoice.customer_address # => "Vimmersvej 13"
If the delegate object is nil an exception is raised, and that happens no matter whether nil responds to the delegated method. You can get a nil instead with the +:allow_nil+ option.
class Foo attr_accessor :bar def initialize(bar = nil) @bar = bar end delegate :zoo, :to => :bar end Foo.new.zoo # raises NoMethodError exception (you called nil.zoo) class Foo attr_accessor :bar def initialize(bar = nil) @bar = bar end delegate :zoo, :to => :bar, :allow_nil => true end Foo.new.zoo # returns nil
Declare that a method has been deprecated.
deprecate :foo deprecate :bar => 'message' deprecate :foo, :bar, :baz => 'warning!', :qux => 'gone!'
Modules are not duplicable:
m = Module.new # => #<Module:0x10328b6e0> m.dup # => #<Module:0x10328b6e0>
Note dup returned the same module object.
Returns the names of the constants defined locally rather than the constants themselves. See local_constants.
Returns the constants that have been defined locally by this object and not in an ancestor. This method is exact if running under Ruby 1.9. In previous versions it may miss some constants if their definition in some ancestor is identical to their definition in the receiver.
Extends the module object with module and instance accessors for class attributes, just like the native attr* accessors for instance attributes.
module AppConfiguration mattr_accessor :google_api_key self.google_api_key = "123456789" mattr_accessor :paypal_url self.paypal_url = "www.sandbox.paypal.com" end AppConfiguration.google_api_key = "overriding the api key!"
Returns all the parents of this module according to its name, ordered from nested outwards. The receiver is not contained within the result.
module M module N end end X = M::N M.parents # => [Object] M::N.parents # => [M, Object] X.parents # => [M, Object]
Synchronize access around a method, delegating synchronization to a particular mutex. A mutex (either a Mutex, or any object that responds to synchronize and yields to a block) must be provided as a final :with option. The :with option should be a symbol or string, and can represent a method, constant, or instance or class variable. Example:
class SharedCache @@lock = Mutex.new def expire ... end synchronize :expire, :with => :@@lock end