Class: Datamappify::Data::Criteria::Relational::FindMultiple

Inherits:
Common
  • Object
show all
Defined in:
lib/datamappify/data/criteria/relational/find_multiple.rb

Instance Attribute Summary collapse

Attributes inherited from Common

#attributes, #attributes_and_values, #criteria, #entity, #options, #source_class

Instance Method Summary collapse

Methods inherited from Common

#any_attribute, #default_source_class_name, #ignore?, #ignore_attribute?, #key_name, #key_value, #new_record?, #perform_with_callbacks, #pk, #primary_record?, #source_class_name, #store_attribute_value

Constructor Details

#initialize(*args) ⇒ FindMultiple

Returns a new instance of FindMultiple.



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/datamappify/data/criteria/relational/find_multiple.rb', line 10

def initialize(*args)
  super

  @primaries   = []
  @secondaries = []

  updated_attributes.each do |attribute|
    collector = attribute.primary_attribute? ? @primaries : @secondaries
    collector << attribute
  end
end

Instance Attribute Details

#primariesObject (readonly)

Returns the value of attribute primaries.



8
9
10
# File 'lib/datamappify/data/criteria/relational/find_multiple.rb', line 8

def primaries
  @primaries
end

#secondariesObject (readonly)

Returns the value of attribute secondaries.



8
9
10
# File 'lib/datamappify/data/criteria/relational/find_multiple.rb', line 8

def secondaries
  @secondaries
end

#structured_criteriaObject (readonly)

Returns the value of attribute structured_criteria.



8
9
10
# File 'lib/datamappify/data/criteria/relational/find_multiple.rb', line 8

def structured_criteria
  @structured_criteria
end

Instance Method Details

#data_record_for(attribute, primary_record) ⇒ Object (private)

Returns an ORM model object (ActiveRecord or Sequel, etc).

Parameters:

  • attribute (Attribute)
  • primary_record (Class)

    an ORM model object (ActiveRecord or Sequel, etc)

Returns:

  • (Object)

    an ORM model object (ActiveRecord or Sequel, etc)



66
67
68
69
70
71
72
# File 'lib/datamappify/data/criteria/relational/find_multiple.rb', line 66

def data_record_for(attribute, primary_record)
  if attribute.primary_attribute?
    primary_record
  else
    primary_record.send(attribute.source_key)
  end
end

#performvoid

This method returns an undefined value.



23
24
25
26
27
28
29
# File 'lib/datamappify/data/criteria/relational/find_multiple.rb', line 23

def perform
  records.map do |record|
    entity = entity_class.new
    update_entity(entity, record)
    entity
  end
end

#record_value_for(attribute, record) ⇒ any (private)

Parameters:

  • attribute (Attribute)
  • record (Class)

    an ORM model object (ActiveRecord or Sequel, etc)

Returns:

  • (any)


80
81
82
# File 'lib/datamappify/data/criteria/relational/find_multiple.rb', line 80

def record_value_for(attribute, record)
  record.nil? ? nil : record.send(attribute.source_attribute_name)
end

#update_entity(entity, primary_record) ⇒ void (private)

This method returns an undefined value.

Parameters:

  • entity (Entity)
  • record (Class)


50
51
52
53
54
55
56
57
# File 'lib/datamappify/data/criteria/relational/find_multiple.rb', line 50

def update_entity(entity, primary_record)
  attributes.each do |attribute|
    record = data_record_for(attribute, primary_record)
    value  = record_value_for(attribute, record)

    entity.send("#{attribute.name}=", value)
  end
end

#updated_attributesArray (private)

Returns:

  • (Array)


34
35
36
37
38
39
40
41
42
43
# File 'lib/datamappify/data/criteria/relational/find_multiple.rb', line 34

def updated_attributes
  unless criteria.keys & attributes.map(&:key) == criteria.keys
    raise EntityAttributeInvalid
  end

  @updated_attributes ||= attributes.select do |attribute|
    attribute.value = criteria[attribute.key]
    criteria.keys.include?(attribute.key)
  end
end