Module: HappyMapper::ClassMethods
- Defined in:
- lib/happymapper.rb
Instance Method Summary collapse
- #attribute(name, type, options = {}) ⇒ Object
- #attributes ⇒ Object
- #element(name, type, options = {}) ⇒ Object
- #elements ⇒ Object
- #get_tag_name ⇒ Object
- #has_many(name, type, options = {}) ⇒ Object
- #has_one(name, type, options = {}) ⇒ Object
- #parse(xml, o = {}) ⇒ Object
- #tag(new_tag_name) ⇒ Object
Instance Method Details
#attribute(name, type, options = {}) ⇒ Object
24 25 26 27 28 29 |
# File 'lib/happymapper.rb', line 24 def attribute(name, type, ={}) attribute = Attribute.new(name, type, ) @attributes[to_s] ||= [] @attributes[to_s] << attribute create_accessor(attribute.name) end |
#attributes ⇒ Object
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, ={}) element = Element.new(name, type, ) @elements[to_s] ||= [] @elements[to_s] << element create_accessor(element.name) end |
#elements ⇒ Object
42 43 44 |
# File 'lib/happymapper.rb', line 42 def elements @elements[to_s] || [] end |
#get_tag_name ⇒ Object
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, ={}) element name, type, {:single => false}.merge() end |
#has_one(name, type, options = {}) ⇒ Object
46 47 48 |
# File 'lib/happymapper.rb', line 46 def has_one(name, type, ={}) element name, type, {:single => true}.merge() 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 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/happymapper.rb', line 62 def parse(xml, o={}) = { :single => false, :use_default_namespace => false, }.merge(o) namespace = "default_ns:" if [: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 nodes = if namespace node = doc.respond_to?(:root) ? doc.root : doc node.register_default_namespace(namespace.chop) node.find("#{namespace}#{get_tag_name}") else nested = '.' unless doc.respond_to?(:root) path = "#{nested}//#{get_tag_name}" doc.find(path) end collection = create_collection(nodes, namespace) # per http://libxml.rubyforge.org/rdoc/classes/LibXML/XML/Document.html#M000354 nodes = nil GC.start [: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 |