append_features | -> | blankslate_original_append_features |
# File vendor/rails/activesupport/lib/active_support/vendor/builder-2.1.2/blankslate.rb, line 105 105: def append_features(mod) 106: result = blankslate_original_append_features(mod) 107: return result if mod != Object 108: instance_methods.each do |name| 109: BlankSlate.hide(name) 110: end 111: result 112: end
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"
# File vendor/rails/activesupport/lib/active_support/core_ext/module/loading.rb, line 12 12: def as_load_path 13: if self == Object || self == Kernel 14: '' 15: elsif is_a? Class 16: parent == self ? '' : parent.as_load_path 17: else 18: name.split('::').collect do |word| 19: word.underscore 20: end * '/' 21: end 22: end
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 }
# File vendor/rails/activesupport/lib/active_support/core_ext/module/attr_accessor_with_default.rb, line 21 21: def attr_accessor_with_default(sym, default = nil, &block) 22: raise 'Default value or block required' unless !default.nil? || block 23: define_method(sym, block_given? ? block : Proc.new { default }) 24: module_eval("def \#{sym}=(value)\nclass << self; attr_reader :\#{sym} end\n@\#{sym} = value\nend\n", __FILE__, __LINE__) 25: end
Declares an attribute reader and writer backed by an internally-named instance variable.
# File vendor/rails/activesupport/lib/active_support/core_ext/module/attr_internal.rb, line 18 18: def attr_internal_accessor(*attrs) 19: attr_internal_reader(*attrs) 20: attr_internal_writer(*attrs) 21: end
Declares an attribute reader backed by an internally-named instance variable.
# File vendor/rails/activesupport/lib/active_support/core_ext/module/attr_internal.rb, line 3 3: def attr_internal_reader(*attrs) 4: attrs.each do |attr| 5: module_eval "def #{attr}() #{attr_internal_ivar_name(attr)} end" 6: end 7: end
Declares an attribute writer backed by an internally-named instance variable.
# File vendor/rails/activesupport/lib/active_support/core_ext/module/attr_internal.rb, line 10 10: def attr_internal_writer(*attrs) 11: attrs.each do |attr| 12: module_eval "def #{attr}=(v) #{attr_internal_ivar_name(attr)} = v end" 13: end 14: end
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"
# File vendor/rails/activesupport/lib/active_support/core_ext/module/delegation.rb, line 75 75: def delegate(*methods) 76: options = methods.pop 77: unless options.is_a?(Hash) && to = options[:to] 78: raise ArgumentError, "Delegation needs a target. Supply an options hash with a :to key as the last argument (e.g. delegate :hello, :to => :greeter)." 79: end 80: 81: if options[:prefix] == true && options[:to].to_s =~ /^[^a-z_]/ 82: raise ArgumentError, "Can only automatically set the delegation prefix when delegating to a method." 83: end 84: 85: prefix = options[:prefix] && "#{options[:prefix] == true ? to : options[:prefix]}_" 86: 87: methods.each do |method| 88: module_eval("def \#{prefix}\#{method}(*args, &block)\n\#{to}.__send__(\#{method.inspect}, *args, &block)\nend\n", "(__DELEGATION__)", 1) 89: end 90: end
# File vendor/rails/railties/lib/console_with_helpers.rb, line 2 2: def include_all_modules_from(parent_module) 3: parent_module.constants.each do |const| 4: mod = parent_module.const_get(const) 5: if mod.class == Module 6: send(:include, mod) 7: include_all_modules_from(mod) 8: end 9: end 10: end
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]
# File vendor/rails/activesupport/lib/active_support/core_ext/module/inclusion.rb, line 21 21: def included_in_classes 22: classes = [] 23: ObjectSpace.each_object(Class) { |k| classes << k if k.included_modules.include?(self) } 24: 25: classes.reverse.inject([]) do |unique_classes, klass| 26: unique_classes << klass unless unique_classes.collect { |k| k.to_s }.include?(klass.to_s) 27: unique_classes 28: end 29: end
# File vendor/rails/activesupport/lib/active_support/core_ext/module/attribute_accessors.rb, line 56 56: def mattr_accessor(*syms) 57: mattr_reader(*syms) 58: mattr_writer(*syms) 59: end
# File vendor/rails/activesupport/lib/active_support/core_ext/module/attribute_accessors.rb, line 14 14: def mattr_reader(*syms) 15: syms.each do |sym| 16: next if sym.is_a?(Hash) 17: class_eval("unless defined? @@\#{sym}\n@@\#{sym} = nil\nend\n\ndef self.\#{sym}\n@@\#{sym}\nend\n\ndef \#{sym}\n@@\#{sym}\nend\n", __FILE__, __LINE__) 18: end 19: end
# File vendor/rails/activesupport/lib/active_support/core_ext/module/attribute_accessors.rb, line 34 34: def mattr_writer(*syms) 35: options = syms.extract_options! 36: syms.each do |sym| 37: class_eval("unless defined? @@\#{sym}\n@@\#{sym} = nil\nend\n\ndef self.\#{sym}=(obj)\n@@\#{sym} = obj\nend\n\n\#{\"\ndef \#{sym}=(obj)\n@@\#{sym} = obj\nend\n\" unless options[:instance_writer] == false }\n", __FILE__, __LINE__) 38: end 39: end
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
# File vendor/rails/activesupport/lib/active_support/core_ext/module/synchronization.rb, line 15 15: def synchronize(*methods) 16: options = methods.extract_options! 17: unless options.is_a?(Hash) && with = options[:with] 18: raise ArgumentError, "Synchronization needs a mutex. Supply an options hash with a :with key as the last argument (e.g. synchronize :hello, :with => :@mutex)." 19: end 20: 21: methods.each do |method| 22: aliased_method, punctuation = method.to_s.sub(/([?!=])$/, ''), $1 23: 24: if method_defined?("#{aliased_method}_without_synchronization#{punctuation}") 25: raise ArgumentError, "#{method} is already synchronized. Double synchronization is not currently supported." 26: end 27: 28: module_eval("def \#{aliased_method}_with_synchronization\#{punctuation}(*args, &block)\n\#{with}.synchronize do\n\#{aliased_method}_without_synchronization\#{punctuation}(*args, &block)\nend\nend\n", __FILE__, __LINE__) 29: 30: alias_method_chain method, :synchronization 31: end 32: end