Module: HappyMapper::ClassMethods

Defined in:
lib/happymapper.rb

Instance Method Summary collapse

Instance Method Details

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



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

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

#attributesObject



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

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

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



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

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

#elementsObject



42
43
44
# File 'lib/happymapper.rb', line 42

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

#get_tag_nameObject



58
59
60
# File 'lib/happymapper.rb', line 58

def get_tag_name
  @tag_name ||= to_s.downcase
end

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



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

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

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



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

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

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



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/happymapper.rb', line 62

def parse(xml, o={})
  options = {
    :single => false,
    :use_default_namespace => false,
  }.merge(o)
  
  namespace = "default_ns:" if options[:use_default_namespace]
  doc = xml.is_a?(LibXML::XML::Node) ? xml : xml.to_libxml_doc
  
  nodes = if namespace
    node = doc.respond_to?(:root) ? doc.root : doc
    node.register_default_namespace(namespace.chop)
    node.find("#{namespace}#{get_tag_name}")
  else
    doc.find("//#{get_tag_name}")
  end
  
  collection = create_collection(nodes, namespace)
  
  # per http://libxml.rubyforge.org/rdoc/classes/LibXML/XML/Document.html#M000354
  nodes = nil
  GC.start
  
  options[:single] ? collection.first : collection
end

#tag(new_tag_name) ⇒ Object



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

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