Module: HappyMapper::ClassMethods

Defined in:
lib/happymapper.rb

Instance Method Summary collapse

Instance Method Details

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



18
19
20
21
22
23
# File 'lib/happymapper.rb', line 18

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



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

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

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



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

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



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

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

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



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

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

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



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

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.



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

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

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



64
65
66
67
68
69
70
71
72
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
# File 'lib/happymapper.rb', line 64

def parse(xml, options = {})
  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

  namespace = @namespace || (node.namespaces && node.namespaces.default)
  namespace = "#{DEFAULT_NS}:#{namespace}" if namespace

  xpath = root ? '/' : './/'
  xpath += "#{DEFAULT_NS}:" if namespace
  xpath += tag_name
  
  nodes = node.find(xpath, Array(namespace))
  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
  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



56
57
58
# File 'lib/happymapper.rb', line 56

def tag(new_tag_name)
  @tag_name = new_tag_name.to_s
end

#tag_nameObject



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

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