Class: Datamappify::Lazy::AttributesHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/datamappify/lazy/attributes_handler.rb

Overview

Overrides attribute setters and getters for lazy loading

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(entity) ⇒ AttributesHandler

Returns a new instance of AttributesHandler.

Parameters:



9
10
11
12
13
14
# File 'lib/datamappify/lazy/attributes_handler.rb', line 9

def initialize(entity)
  @entity              = entity
  @uncached_attributes = entity.attributes.keys

  entity.add_observer(self)
end

Instance Attribute Details

#uncached_attributesArray

Returns:

  • (Array)


6
7
8
# File 'lib/datamappify/lazy/attributes_handler.rb', line 6

def uncached_attributes
  @uncached_attributes
end

Class Method Details

.walk_attributes(name, entity, attributes) ⇒ void

This method returns an undefined value.

Parameters:



107
108
109
110
111
112
113
114
115
116
117
118
119
# 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

Instance Method Details

#all_attributesSet<Attribute> (private)

Returns:

  • (Set<Attribute>)

See Also:



31
32
33
# File 'lib/datamappify/lazy/attributes_handler.rb', line 31

def all_attributes
  @all_attributes ||= @entity.repository.data_mapper.attributes
end

#attribute_by_name(name) ⇒ Data::Mapper::Attribute (private)

Parameters:

  • name (Symbol)

Returns:



97
98
99
# 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) ⇒ Set<Data::Mapper::Attribute> (private)

Parameters:

  • name (Symbol)

Returns:



84
85
86
87
88
89
90
91
92
# 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!void (private)

This method returns an undefined value.

Removes the cached attributes from the uncached attributes array



38
39
40
# File 'lib/datamappify/lazy/attributes_handler.rb', line 38

def cache_attributes!
  @uncached_attributes = @uncached_attributes - @entity.cached_attributes.keys
end

#override_attribute(name) ⇒ void (private)

This method returns an undefined value.

Overrides attribute setters and getters

Parameters:

  • name (Symbol)


47
48
49
50
# 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) ⇒ void (private)

This method returns an undefined value.

Parameters:

  • name (Symbol)


68
69
70
71
72
73
74
75
76
77
78
79
# 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) ⇒ void (private)

This method returns an undefined value.

Parameters:

  • name (Symbol)


55
56
57
58
59
60
61
62
63
# 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

#update(query_method, attributes) ⇒ Object

Triggers only when a reader query (e.g. Repository::QueryMethod::Find) is performed

Parameters:



19
20
21
22
23
24
# 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