Class: Xing::Builders::ListBuilder

Inherits:
Object
  • Object
show all
Includes:
Services::Locator
Defined in:
lib/xing/builders/list_builder.rb

Direct Known Subclasses

OrderedListBuilder

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Services::Locator

#normalize_path, #route_to, #router

Constructor Details

#initialize(list_data, mapper_class) ⇒ ListBuilder

list_data is is an array of JSON objects passed in by the mapper (the new list of records)



9
10
11
12
13
14
# File 'lib/xing/builders/list_builder.rb', line 9

def initialize(list_data, mapper_class)
  @list_data = list_data
  @mapper_class = mapper_class

  @errors = Hash.new { |hash, key| hash[key] = {} }
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



16
17
18
# File 'lib/xing/builders/list_builder.rb', line 16

def errors
  @errors
end

Instance Method Details

#buildObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/xing/builders/list_builder.rb', line 22

def build
  @new_list = []
  @list_data.each_with_index do |data, index|

    mapper = @mapper_class.new(data, locator_for(data))

    mapper.perform_mapping
    set_position(mapper.record, index)

    @new_list << mapper.record
    @errors[index] = mapper.errors[:data] unless mapper.errors[:data].blank?
  end
  @new_list
end

#locator_for(data) ⇒ Object



18
19
20
# File 'lib/xing/builders/list_builder.rb', line 18

def locator_for(data)
  route_to(data[:links][:self])[:id].to_i unless (data[:links] || {})[:self].blank?
end

#set_position(record, index) ⇒ Object



37
38
39
# File 'lib/xing/builders/list_builder.rb', line 37

def set_position(record, index)
  # position is not set in list builder
end