- Inheritance
- < Object
- Included Modules
- ActiveSupport::CoreExtensions::Module, ActiveSupport::Deprecation::ClassMethods
Also, modules included into Object need to be scanned and have their instance methods removed from blank slate. In theory, modules included into Kernel would have to be removed as well, but a "feature" of Ruby prevents late includes into modules from being exposed in the first place.
Aliases
Method | Alias | Description |
---|---|---|
append_features | → blankslate_original_append_features |
Methods
Instance
Visibility | Signature |
---|---|
public | append_features (mod) |
public | as_load_path () |
public | attr_accessor_with_default (sym, default = nil, &block) |
public | attr_internal (*attrs) |
public | attr_internal_accessor (*attrs) |
public | attr_internal_reader (*attrs) |
public | attr_internal_writer (*attrs) |
public | delegate (*methods) |
public | included_in_classes () |
public | mattr_accessor (*syms) |
public | mattr_reader (*syms) |
public | mattr_writer (*syms) |
public | synchronize (*methods) |
Instance Method Detail
append_features(mod)
as_load_path()
Returns String#underscore applied to the module name minus trailing classes.
ActiveRecord.as_load_path # => "active_record" ActiveRecord::Associations.as_load_path # => "active_record/associations" ActiveRecord::Base.as_load_path # => "active_record" (Base is a class)
The Kernel module gives an empty string by definition.
Kernel.as_load_path # => "" Math.as_load_path # => "math"
attr_accessor_with_default(sym, default = nil, &block)
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 some_person.age => 25 some_person.age = 26 some_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 }
attr_internal(*attrs)
Alias for attr_internal_accessor
attr_internal_accessor(*attrs)
Declares an attribute reader and writer backed by an internally-named instance variable.
attr_internal_reader(*attrs)
Declares an attribute reader backed by an internally-named instance variable.
attr_internal_writer(*attrs)
Declares an attribute writer backed by an internally-named instance variable.
delegate(*methods)
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 as the final :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 object to which you delegate can be nil, you may want to use the :allow_nil option. In that case, it returns nil instead of raising a NoMethodError exception:
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
included_in_classes()
Returns the classes in the current ObjectSpace where this module has been mixed in according to Module#included_modules.
module M end module N include M end class C include M end class D < C end p M.included_in_classes # => [C, D]
mattr_accessor(*syms)
mattr_reader(*syms)
mattr_writer(*syms)
synchronize(*methods)
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