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.new('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`



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

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:



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

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



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

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

#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



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

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



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/hermod/xml_section_builder.rb', line 147

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|
    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



169
170
171
172
173
# File 'lib/hermod/xml_section_builder.rb', line 169

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



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

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



135
136
137
138
139
# File 'lib/hermod/xml_section_builder.rb', line 135

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



122
123
124
125
126
# File 'lib/hermod/xml_section_builder.rb', line 122

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