Class: Hermod::XmlSectionBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/hermod/xml_section_builder.rb

Overview

Public: Used to build an anonymous subclass of XmlSection with methods for defining nodes on that subclass of varying types used by HMRC.

Constant Summary collapse

ZERO =
BigDecimal("0").freeze
BOOLEAN_VALUES =
[
  YES = "yes".freeze,
  NO  = "no".freeze,
]

Instance Method Summary collapse

Constructor Details

#initialize(new_class) ⇒ XmlSectionBuilder

Internal: Sets up the builder with the anonymous subclass of XmlSection. Don’t use this directly, instead see ‘Hermod::XmlSection.build`



44
45
46
47
# File 'lib/hermod/xml_section_builder.rb', line 44

def initialize(new_class)
  @new_class = new_class
  @node_order = []
end

Instance Method Details

#build {|_self| ... } ⇒ Object

Internal: Takes a block to build the class using the methods on this builder and then sets the correct node_order (to ensure the nodes are ordered correctly in the XML).

Returns the newly built class. This should be assigned to a constant before use.

Yields:

  • (_self)

Yield Parameters:



55
56
57
58
59
# File 'lib/hermod/xml_section_builder.rb', line 55

def build(&block)
  yield self
  @new_class.node_order = @node_order
  @new_class
end

#date_node(name, options = {}) ⇒ Object

Public: defines a node for sending a date to HMRC

name - the name of the node. This will become the name of the method on the XmlSection. options - a hash of options used to set up validations.

Returns nothing you should rely on



105
106
107
108
109
110
111
112
113
114
# File 'lib/hermod/xml_section_builder.rb', line 105

def date_node(name, options={})
  validators = [].tap do |validators|
    validators << Validators::ValuePresence.new unless options.delete(:optional)
    validators << Validators::TypeChecker.new(Date) { |value| value.respond_to? :strftime }
  end

  create_method(name, [], validators, options) do |value, attributes|
    [(value ? value.strftime(format_for(:date)) : nil), attributes]
  end
end

#datetime_node(name, options = {}) ⇒ Object

Public: defines a node for sending a datetime to HMRC

name - the name of the node. This will become the name of the method on the XmlSection. options - a hash of options used to set up validations.

Returns nothing you should rely on



122
123
124
125
126
127
128
129
130
131
# File 'lib/hermod/xml_section_builder.rb', line 122

def datetime_node(name, options={})
  validators = [].tap do |validators|
    validators << Validators::ValuePresence.new unless options.delete(:optional)
    validators << Validators::TypeChecker.new(DateTime) { |value| value.respond_to? :strftime }
  end

  create_method(name, [], validators, options) do |value, attributes|
    [(value ? value.strftime(format_for(:datetime)) : nil), attributes]
  end
end

#integer_node(name, options = {}) ⇒ Object

Public: defines a node for sending an integer to HMRC

name - the name of the node. This will become the name of the method on the XmlSection. options - a hash of options used to set up validations.

Returns nothing you should rely on



88
89
90
91
92
93
94
95
96
97
# File 'lib/hermod/xml_section_builder.rb', line 88

def integer_node(name, options={})
  validators = [].tap do |validators|
    if options.has_key? :range
      validators << Validators::Range.new(options[:range][:min], options[:range][:max])
    end
  end
  create_method(name, [], validators, options) do |value, attributes|
    [value.to_s, attributes]
  end
end

#monetary_node(name, options = {}) ⇒ Object

Public: defines a node for sending a monetary value to HMRC

name - the name of the node. This will become the name of the method on the XmlSection. options - a hash of options used to set up validations.

Returns nothing you should rely on



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/hermod/xml_section_builder.rb', line 165

def monetary_node(name, options={})
  validators = [].tap do |validators|
    validators << Validators::NonNegative.new unless options.fetch(:negative, true)
    validators << Validators::NonZero.new unless options.fetch(:zero, true)
    validators << Validators::WholeUnits.new if options[:whole_units]
  end

  create_method(name, [], validators, options) do |value, attributes|
    value ||= value.to_i
    if options[:optional] && value == 0
      [nil, attributes]
    else
      [sprintf(format_for(:money), value), attributes]
    end
  end
end

#parent_node(name, options = {}) ⇒ Object

Public: defines an XML parent node that wraps other nodes

name - the name of the node. This will become the name of the method on the XmlSection. options - a hash of options used to set up validations.

Returns nothing you should rely on



188
189
190
191
192
# File 'lib/hermod/xml_section_builder.rb', line 188

def parent_node(name, options={})
  create_method(name, [], [], options) do |value, attributes|
    [value, attributes]
  end
end

#string_node(name, options = {}) ⇒ Object

Public: defines a node for sending a string to HMRC

name - the name of the node. This will become the name of the method on the XmlSection. options - a hash of options used to set up validations.

Returns nothing you should rely on



67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/hermod/xml_section_builder.rb', line 67

def string_node(name, options={})
  mutators = [].tap do |mutators|
    mutators << InputMutator.new(options.delete(:input_mutator)) if options.has_key? :input_mutator
  end
  validators = [].tap do |validators|
    validators << Validators::AllowedValues.new(options.delete(:allowed_values)) if options.has_key? :allowed_values
    validators << Validators::RegularExpression.new(options.delete(:matches)) if options.has_key? :matches
    validators << Validators::ValuePresence.new unless options.delete(:optional)
    validators << Validators::Attributes.new(options.fetch(:attributes, {}).keys)
  end
  create_method(name, mutators, validators, options) do |value, attributes|
    [value, attributes]
  end
end

#yes_no_node(name, options = {}) ⇒ Object

Public: defines a node for sending a boolean to HMRC. A “yes” will be sent if it’s true and a “no” will be sent if it’s false

name - the name of the node. This will become the name of the method on the XmlSection. options - a hash of options used to set up validations.

Returns nothing you should rely on



153
154
155
156
157
# File 'lib/hermod/xml_section_builder.rb', line 153

def yes_no_node(name, options={})
  create_method(name, [], [], options) do |value, attributes|
    [(value ? YES : NO), attributes]
  end
end

#yes_node(name, options = {}) ⇒ Object

Public: defines a node for sending a boolean to HMRC. It will only be sent if the boolean is true.

name - the name of the node. This will become the name of the method on the XmlSection. options - a hash of options used to set up validations.

Returns nothing you should rely on



140
141
142
143
144
# File 'lib/hermod/xml_section_builder.rb', line 140

def yes_node(name, options={})
  create_method(name, [], [], options) do |value, attributes|
    [(value ? YES : nil), attributes]
  end
end