class Datamappify::Lazy::AttributesHandler
Overrides attribute setters and getters for lazy loading
Attributes
uncached_attributes[RW]
@return [Array]
Public Class Methods
new(entity)
click to toggle source
@param entity [Entity]
# File lib/datamappify/lazy/attributes_handler.rb, line 9 def initialize(entity) @entity = entity @uncached_attributes = entity.attributes.keys entity.add_observer(self) end
Private Class Methods
walk_attributes(name, entity, attributes)
click to toggle source
@param name [Symbol]
@param attributes [Set<Data::Mapper::Attribute>]
@return [void]
# File lib/datamappify/lazy/attributes_handler.rb, line 107 def walk_attributes(name, entity, attributes) SourceAttributesWalker.new({ :entity => entity, :provider_name => attributes.first.provider_name, :attributes => attributes, :dirty_aware? => true, :dirty_attributes => [] }).execute do |provider_name, source_class, attributes| entity.repository.data_mapper.provider(provider_name).build_criteria( :FindByKey, source_class, entity, attributes ) end end
Public Instance Methods
update(query_method, attributes)
click to toggle source
Triggers only when a reader query (e.g. {Repository::QueryMethod::Find}) is performed
@param (see Repository::QueryMethod::Method::SourceAttributesWalker#walk_performed
)
# File lib/datamappify/lazy/attributes_handler.rb, line 19 def update(query_method, attributes) if query_method && query_method.reader? cache_attributes! uncached_attributes.each { |name| override_attribute(name) } end end
Private Instance Methods
all_attributes()
click to toggle source
@return (see Data::Mapper#attributes
)
# File lib/datamappify/lazy/attributes_handler.rb, line 31 def all_attributes @all_attributes ||= @entity.repository.data_mapper.attributes end
attribute_by_name(name)
click to toggle source
@param name [Symbol]
@return [Data::Mapper::Attribute]
# File lib/datamappify/lazy/attributes_handler.rb, line 97 def attribute_by_name(name) all_attributes.find { |attribute| attribute.key == name } end
attributes_from_same_source(name)
click to toggle source
@param name [Symbol]
@return [Set<Data::Mapper::Attribute>]
# File lib/datamappify/lazy/attributes_handler.rb, line 84 def attributes_from_same_source(name) source_class_name = attribute_by_name(name).source_class_name attributes = all_attributes.select do |attribute| attribute.source_class_name == source_class_name end Set.new(attributes) end
cache_attributes!()
click to toggle source
Removes the cached attributes from the uncached attributes array
@return [void]
# File lib/datamappify/lazy/attributes_handler.rb, line 38 def cache_attributes! @uncached_attributes = @uncached_attributes - @entity.cached_attributes.keys end
override_attribute(name)
click to toggle source
Overrides attribute setters and getters
@param name [Symbol]
@return [void]
# File lib/datamappify/lazy/attributes_handler.rb, line 47 def override_attribute(name) override_attribute_setter(name) override_attribute_getter(name) end
override_attribute_getter(name)
click to toggle source
@param (see override_attribute
)
@return [void]
# File lib/datamappify/lazy/attributes_handler.rb, line 68 def override_attribute_getter(name) entity = @entity attributes = attributes_from_same_source(name) entity.define_singleton_method name do Logger.performed(:override_attribute, name) AttributesHandler.walk_attributes(name, entity, attributes) instance_variable_get "@#{name}" end end
override_attribute_setter(name)
click to toggle source
@param (see override_attribute
)
@return [void]
Calls superclass method
# File lib/datamappify/lazy/attributes_handler.rb, line 55 def override_attribute_setter(name) @entity.define_singleton_method "#{name}=" do |value| super(value) self.define_singleton_method name do instance_variable_set "@#{name}", value end end end