Class: ONIX::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/onix/builder.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}, root = nil, &block) ⇒ Builder

Returns a new instance of Builder.



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
# File 'lib/onix/builder.rb', line 20

def initialize(options = {}, root = nil, &block)
  if root
    @doc = root
    @parent = root
  else
    @parent = @doc = Root.new
  end

  @context = nil
  @arity = nil

  return unless block_given?

  @arity = block.arity
  if @arity <= 0
    @context = eval("self", block.binding)
    instance_eval(&block)
  else
    yield self
  end

  @lax = false

  @parent = @doc
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/onix/builder.rb', line 74

def method_missing(method, *args, &block)
  # :nodoc:
  if @context && @context.respond_to?(method)
    @context.send(method, *args, &block)
  else
    if @parent
      parser_el = @parent.registered_elements[method.to_s]
      if parser_el
        if ONIX.const_defined?(parser_el.klass_name)
          node = get_class(parser_el.klass_name, args)
        end

        if parser_el.is_array?
          arr = @parent.send(parser_el.underscore_name)
          case parser_el.type
          when :ignore
          when :subset
            node.attributes = get_attributes(args[1]) if args.length > 1
            arr << node
          else
            arr << get_primitive(args)
          end
        else
          case parser_el.type
          when :ignore
          when :subset
            # FIXME: dirty
            if @parent.class.included_modules.include?(DateMethods) && node.is_a?(DateFormat)
              @parent.deprecated_date_format = true
            end
            node.attributes = get_attributes(args[1]) if args.length > 1
            @parent.send(parser_el.underscore_name + "=", node)
          when :datestamp
            @parent.send(parser_el.underscore_name + "=", DateStamp.new(args[0], args[1] || "%Y%m%d"))
          else
            @parent.send(parser_el.underscore_name + "=", get_primitive(args))
          end
        end
      else
        raise BuilderInvalidChildElement, [@parent.class.to_s.sub("ONIX::", ""), method.to_s]
      end
    else
      node = get_class(method, args)
    end

    insert(node, &block)
  end
end

Instance Method Details

#dump(io = STDOUT) ⇒ Object



50
51
52
# File 'lib/onix/builder.rb', line 50

def dump(io = STDOUT)
  ONIX::Serializer::Dump.serialize(io, @doc, "Root")
end

#lax(&block) ⇒ Object

code in block will not check for validity



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/onix/builder.rb', line 61

def lax &block
  if block_given?
    @lax = true
    @arity ||= block.arity
    if @arity <= 0
      instance_eval(&block)
    else
      block.call(self)
    end
    @lax = false
  end
end

#serialize(xml) ⇒ Object



46
47
48
# File 'lib/onix/builder.rb', line 46

def serialize(xml)
  ONIX::Serializer::Default.serialize(xml, @doc, "Root")
end

#to_xmlObject



54
55
56
57
58
# File 'lib/onix/builder.rb', line 54

def to_xml
  Nokogiri::XML::Builder.new(:encoding => "UTF-8") do |xml|
    serialize(xml)
  end.to_xml
end