Class: CompaniesHouseInputGateway::Forms::FormAbstractBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/companies_house_input_gateway/forms/form_abstract_builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(xml: nil, data: {}) ⇒ FormAbstractBuilder

Returns a new instance of FormAbstractBuilder.



8
9
10
11
# File 'lib/companies_house_input_gateway/forms/form_abstract_builder.rb', line 8

def initialize(xml: nil, data: {})
  @xml = xml
  @data = data
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



6
7
8
# File 'lib/companies_house_input_gateway/forms/form_abstract_builder.rb', line 6

def data
  @data
end

#xmlObject (readonly)

Returns the value of attribute xml.



6
7
8
# File 'lib/companies_house_input_gateway/forms/form_abstract_builder.rb', line 6

def xml
  @xml
end

Instance Method Details

#build_form(_request_type, _requested_form = nil) ⇒ Object

Raises:

  • (NotImplementedError)


13
14
15
# File 'lib/companies_house_input_gateway/forms/form_abstract_builder.rb', line 13

def build_form(_request_type, _requested_form = nil)
  raise NotImplementedError, "#{self.class} has not implemented method '#{__method__}'"
end

#generate_xml_from_incoming_data(data, request_type, parent = nil, first_iteration = false) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/companies_house_input_gateway/forms/form_abstract_builder.rb', line 17

def generate_xml_from_incoming_data(data, request_type, parent = nil, first_iteration = false)
  return if data.to_s.empty?
  return unless data.is_a?(Hash)

  unless parent
    builder = Nokogiri::XML::Builder.new do |xml|
      xml.send(request_type, form_namespace) do
        generate_xml_from_incoming_data(data, request_type, xml)
      end
    end

    return builder
  end

  if first_iteration
    request_type = Util.camelize(request_type)
    parent.send(request_type, form_namespace) do
      generate_xml_from_incoming_data(data, request_type, parent)
    end

    return parent
  end

  data.each do |label, value|
    label = Constants::SPECIAL_FIELDS_MAPPER[label] || Util.camelize(label)
    if value.is_a?(Hash)
      parent.send(label) do
        generate_xml_from_incoming_data(value, request_type, parent)
      end

    elsif value.is_a?(Array)
      value.each do |el|
        el = { label => el }
        generate_xml_from_incoming_data(el, request_type, parent)
      end

    else
      value = value.is_a?(BigDecimal) ? value.to_f : value
      parent.send(label, value)
    end
  end
end