class Hermod::XmlSectionBuilder
def create_method(name, mutators, validators, options = {}, &block)
block - a block that takes the value and attributes and does any post-validation mutation on them
errors under given conditions
validators - an array of Validator::Base subclasses that are applied to the value and attributes and raise
mutators - an array of InputMutator objects (normally one) that change the value and attributes in some way
name - the name of the new method
certain options. This is used to implement all the node type methods.
and a set of validators to change and validate the input according to
Private: creates a method with a given name that uses a set of mutators
def create_method(name, mutators, validators, options = {}, &block) raise DuplicateNodeError, "#{name} is already defined" if @node_order.include? name @node_order << name xml_name = options.fetch(:xml_name, name.to_s.camelize) @new_class.send :define_method, name do |value, attributes = {}| mutators.each { |mutator| value, attributes = mutator.mutate!(value, attributes, self) } begin validators.each { |validator| validator.valid?(value, attributes) } rescue InvalidInputError => ex raise InvalidInputError, "#{name} #{ex.message}" end value, attributes = instance_exec(value, attributes, &block) if value.present? nodes[name] << XmlNode.new(xml_name, value, attributes).rename_attributes(options[:attributes]) end end end