Module: HappyMapper::ClassMethods

Defined in:
lib/happymapper.rb

Instance Method Summary collapse

Instance Method Details

#attribute(name, type, options = {}) ⇒ Object



22
23
24
25
26
27
# File 'lib/happymapper.rb', line 22

def attribute(name, type, options={})
  attribute = Attribute.new(name, type, options)
  @attributes[to_s] ||= []
  @attributes[to_s] << attribute
  attr_accessor attribute.method_name.intern
end

#attributesObject



29
30
31
# File 'lib/happymapper.rb', line 29

def attributes
  @attributes[to_s] || []
end

#element(name, type, options = {}) ⇒ Object



33
34
35
36
37
38
# File 'lib/happymapper.rb', line 33

def element(name, type, options={})
  element = Element.new(name, type, options)
  @elements[to_s] ||= []
  @elements[to_s] << element
  attr_accessor element.method_name.intern
end

#elementsObject



40
41
42
# File 'lib/happymapper.rb', line 40

def elements
  @elements[to_s] || []
end

#has_many(name, type, options = {}) ⇒ Object



53
54
55
# File 'lib/happymapper.rb', line 53

def has_many(name, type, options={})
  element name, type, {:single => false}.merge(options)
end

#has_one(name, type, options = {}) ⇒ Object



49
50
51
# File 'lib/happymapper.rb', line 49

def has_one(name, type, options={})
  element name, type, {:single => true}.merge(options)
end

#namespace(namespace = nil) ⇒ Object

Specify a namespace if a node and all its children are all namespaced elements. This is simpler than passing the :namespace option to each defined element.



60
61
62
63
# File 'lib/happymapper.rb', line 60

def namespace(namespace = nil)
  @namespace = namespace if namespace
  @namespace
end

#parse(xml, options = {}) ⇒ Object



73
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/happymapper.rb', line 73

def parse(xml, options = {})
  # locally scoped copy of namespace for this parse run
  namespace = @namespace

  if xml.is_a?(XML::Node)
    node = xml
  else
    if xml.is_a?(XML::Document)
      node = xml.root
    else
      node = XML::Parser.string(xml).parse.root
    end

    root = node.name == tag_name
  end

  # This is the entry point into the parsing pipeline, so the default
  # namespace prefix registered here will propagate down
  namespaces = node.namespaces
  if namespaces && namespaces.default
    # don't assign the default_prefix if it has already been assigned
    namespaces.default_prefix = DEFAULT_NS unless namespaces.find_by_prefix(DEFAULT_NS)
    namespace ||= DEFAULT_NS
  end

  xpath = root ? '/' : './/'
  xpath += "#{namespace}:" if namespace
  #puts "parse: #{xpath}"

  nodes = []
  # when finding nodes, do it in this order:
  # 1. specified tag
  # 2. name of element
  # 3. tag_name (derived from class name by default)
  [options[:tag], options[:name], tag_name].compact.each do |xpath_ext|
    nodes = node.find(xpath + xpath_ext.to_s)
    break if nodes && nodes.size > 0
  end
  collection = nodes.collect do |n|
    obj = new

    attributes.each do |attr|
      obj.send("#{attr.method_name}=",
                attr.from_xml_node(n, namespace))
    end

    elements.each do |elem|
      obj.send("#{elem.method_name}=",
                elem.from_xml_node(n, namespace))
    end

    obj.send("#{@text_node.method_name}=", 
              @text_node.from_xml_node(n, namespace)) if @text_node
    
    obj
  end

  # per http://libxml.rubyforge.org/rdoc/classes/LibXML/XML/Document.html#M000354
  nodes = nil

  if options[:single] || root
    collection.first
  else
    collection
  end
end

#tag(new_tag_name) ⇒ Object



65
66
67
# File 'lib/happymapper.rb', line 65

def tag(new_tag_name)
  @tag_name = new_tag_name.to_s unless new_tag_name.nil? || new_tag_name.to_s.empty?
end

#tag_nameObject



69
70
71
# File 'lib/happymapper.rb', line 69

def tag_name
  @tag_name ||= to_s.split('::')[-1].downcase
end

#text_node(name, type, options = {}) ⇒ Object



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

def text_node(name, type, options={})
  @text_node = TextNode.new(name, type, options)
  attr_accessor @text_node.method_name.intern
end