Module: XmlParsable::ClassMethods

Defined in:
lib/xmlparsable.rb

Instance Method Summary collapse

Instance Method Details

#collection(elem_name, *extras) ⇒ Object

collection :hive, BeeElement, => :bee, array-options…, :item => {item-options…} collection :hive, :bee, BeeElement, :item => {item-options…} collection :hive, :item => {:class => BeeElement, item-options…}

Raises:

  • (ArgumentError)


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

def collection(elem_name, *extras)
  arguments   = extras.pop if extras.last.is_a?(Hash)
  arguments ||= Hash.new

  arguments[:item] ||= Hash.new
  arguments[:item_name] ||= extras.shift unless extras.first.is_a?(Class)
  arguments[:item_name] ||= elem_name.to_s.singularize
  arguments[:class] ||= extras.shift if extras.first.is_a?(Class)
  arguments[:class] ||= Elements::StringElement
  raise ArgumentError, "extra arguments" unless extras.empty?

  element(elem_name, Elements::ArrayElement, arguments)
end

#element(elem_name, *extras) ⇒ Object

element :name, StringElement, options… element :name, => StringElement, options…

Raises:

  • (ArgumentError)


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
59
60
61
62
63
# File 'lib/xmlparsable.rb', line 26

def element(elem_name, *extras)
  arguments   = extras.pop if extras.last.is_a?(Hash)
  arguments ||= Hash.new

  elem_type   = extras.shift if extras.first.is_a?(Class)
  elem_type ||= arguments.delete(:class)
  elem_type ||= Elements::StringElement
  raise ArgumentError, "extra arguments" unless extras.empty?

  # Look up the inheritance hierarchy to map element name to parser
  parent = superclass < XmlParsable ?
    superclass.instance_variable_get(:@parsers) : Hash.new

  @parsers ||= Hash.new{|h,k| parent[k] }
  @parsers[elem_name.to_s] = [elem_type.parsable, arguments]

  # Define reader and writer methods (or don't, see if I care!)
  accessor = arguments.fetch(:accessor, elem_name)
  reader   = arguments.fetch(:reader, accessor)
  writer   = arguments.fetch(:writer, accessor)

  if reader and not method_defined?(reader)
    define_method(reader) do
      hash = instance_variable_get(:@xmlelems)
      hash and hash[elem_name.to_sym]
    end
  end

  if writer and not method_defined?("#{writer}=")
    define_method("#{writer}=") do |value|
      unless hash = instance_variable_get(:@xmlelems)
        hash = Hash.new{|h,k| h.fetch(k.to_sym, nil) }
        instance_variable_set(:@xmlelems, hash)
      end
      hash.update(elem_name.to_sym => value)
    end
  end
end

#parsableObject



102
103
104
# File 'lib/xmlparsable.rb', line 102

def parsable
  Elements::RecordElement::Proxy.new(self, @parsers)
end

#parse(input, *extras) ⇒ Object



92
93
94
95
96
97
98
99
100
# File 'lib/xmlparsable.rb', line 92

def parse(input, *extras)
  root   = XmlParsable::Elements::RecordElement.new(nil, nil, nil, new, @parsers, Hash.new)
  stack  = XmlParsable::Parser.new(root)
  parser = XmlParsable.parser(input)

  parser.callbacks = stack
  parser.parse
  root.target
end

#repeated(elem_name, *extras) ⇒ Object



82
83
84
85
86
87
88
89
90
# File 'lib/xmlparsable.rb', line 82

def repeated(elem_name, *extras)
  if extras.last.is_a?(Hash)
    extras.last[:repeated] = true
    element(elem_name, *extras)
  else
    extras.push(Hash[:repeated => true])
    element(elem_name, *extras)
  end
end