Class: Xing::Builders::ListDifferenceBuilder
- Inherits:
-
Object
- Object
- Xing::Builders::ListDifferenceBuilder
show all
- Includes:
- Services::Locator
- Defined in:
- lib/xing/builders/list_difference_builder.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
#normalize_path, #route_to, #router
Constructor Details
#initialize(list_data, collection, mapper_class) ⇒ ListDifferenceBuilder
list_data is is an array of JSON objects passed in by the mapper (the new list of records) collection is the ActiveRecord collection of existing records (i.e. person.pets, rainbow.colors, book.chapters)
10
11
12
13
14
15
16
|
# File 'lib/xing/builders/list_difference_builder.rb', line 10
def initialize(list_data, collection, mapper_class)
@list_data = list_data
@collection = collection
@mapper_class = mapper_class
@errors = Hash.new { |hash, key| hash[key] = {} }
end
|
Instance Attribute Details
#errors ⇒ Object
Returns the value of attribute errors.
18
19
20
|
# File 'lib/xing/builders/list_difference_builder.rb', line 18
def errors
@errors
end
|
Instance Method Details
#build ⇒ Object
20
21
22
23
24
25
|
# File 'lib/xing/builders/list_difference_builder.rb', line 20
def build
sort_json_items
map_items
{ save: @new_list, delete: @delete_ids }
end
|
#locator_for(data) ⇒ Object
41
42
43
|
# File 'lib/xing/builders/list_difference_builder.rb', line 41
def locator_for(data)
route_to(data[:links][:self])[:id].to_i
end
|
#map_items ⇒ Object
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
# File 'lib/xing/builders/list_difference_builder.rb', line 45
def map_items
@new_list = []
@list_data.each_with_index do |item, index|
mapper = @mapper_class.new(item[:incoming], item[:locator])
@collection << mapper.record
mapper.perform_mapping
@new_list << mapper
@errors[index] = mapper.errors[:data] unless mapper.errors[:data].blank?
end
end
|
#set_locator(data) ⇒ Object
37
38
39
|
# File 'lib/xing/builders/list_difference_builder.rb', line 37
def set_locator(data)
locator_for(data) unless (data[:links] || {})[:self].blank?
end
|
#sort_json_items ⇒ Object
27
28
29
30
31
32
33
34
35
|
# File 'lib/xing/builders/list_difference_builder.rb', line 27
def sort_json_items
@existing_ids = @collection.map{|item| item.send(@mapper_class.locator_attribute_name)}
@list_data = @list_data.map do |data|
{ :locator => set_locator(data), :incoming => data}
end
@delete_ids = @existing_ids - @list_data.map { |item| item[:locator] }
end
|