Class: ActiveRecord::AttributeMethods::AttributeMethodCache

Inherits:
Object
  • Object
show all
Defined in:
lib/active_record/attribute_methods.rb

Instance Method Summary collapse

Constructor Details

#initializeAttributeMethodCache

Returns a new instance of AttributeMethodCache.



38
39
40
41
# File 'lib/active_record/attribute_methods.rb', line 38

def initialize
  @module = Module.new
  @method_cache = ThreadSafe::Cache.new
end

Instance Method Details

#[](name) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'lib/active_record/attribute_methods.rb', line 43

def [](name)
  @method_cache.compute_if_absent(name) do
    safe_name = name.unpack('h*').first
    temp_method = "__temp__#{safe_name}"
    ActiveRecord::AttributeMethods::AttrNames.set_name_cache safe_name, name
    @module.module_eval method_body(temp_method, safe_name), __FILE__, __LINE__
    @module.instance_method temp_method
  end
end