Module: XmlNuts::Nut::ClassMethods
- Includes:
- Mappings, XmlBackend
- Defined in:
- lib/xmlnuts/nuts.rb
Overview
See also Nut.
Instance Method Summary collapse
-
#attribute(name, type = :string, options = {}) ⇒ Object
attribute(name, [type[, options]]) -> Mappings::Attribute or Mappings::AttributeValue.
- #build(nut, result = :string, options = {}) ⇒ Object
-
#build_node(nut, node) ⇒ Object
:nodoc:.
-
#element(name, type = :string, options = {}, &block) ⇒ Object
element(name, [type[, options]]) -> Mappings::Element or Mappings::ElementValue element(name[, options]) { block } -> Mappings::Element.
-
#elements(name, type = :string, options = {}, &block) ⇒ Object
elements(name, [type[, options]]) -> Mappings::Element or Mappings::ElementValue elements(name[, options]) { block } -> Mappings::Element.
-
#mappings ⇒ Object
mappings -> Array.
-
#namespaces(mappings = nil) ⇒ Object
namespaces(hash) -> Hash namespaces -> Hash.
- #parse(source, options = {}) ⇒ Object
-
#parse_node(nut, node) ⇒ Object
:nodoc:.
-
#root(xmlname = nil, options = {}) ⇒ Object
root(xmlname[, :xmlns => …]) -> Mappings::Root root -> Mappings::Root.
Methods included from XmlBackend
current, current=, default, default=
Instance Method Details
#attribute(name, type = :string, options = {}) ⇒ Object
attribute(name, [type[, options]]) -> Mappings::Attribute or Mappings::AttributeValue
Defines attribute mapping.
Arguments
name-
Accessor name
type-
Element type.
:stringassumed if omitted (seeConverter). options-
:xmlname,:xmlns, converter options (seeConverter).
Example:
class Cat
include XmlNuts::Nut
...
element :name, :string, :whitespace => :collapse
element :cheeseburger, Cheeseburger, :xmlname => :cheezburger
...
end
122 123 124 125 |
# File 'lib/xmlnuts/nuts.rb', line 122 def attribute(name, type = :string, = {}) define_accessor name mappings << Attribute.new(name, type, ()) end |
#build(nut, result = :string, options = {}) ⇒ Object
138 139 140 141 142 143 |
# File 'lib/xmlnuts/nuts.rb', line 138 def build(nut, result = :string, = {}) , result = result, :string if result.is_a?(Hash) [:xmlname] ||= root.xmlname [:xmlns_prefix] = namespaces.invert[[:xmlns] ||= root.xmlns] backend.build(result, ) {|node| build_node(nut, node) } end |
#build_node(nut, node) ⇒ Object
:nodoc:
145 146 147 148 149 |
# File 'lib/xmlnuts/nuts.rb', line 145 def build_node(nut, node) #:nodoc: backend.add_namespaces(node, namespaces) callem(:to_xml, nut, node) node end |
#element(name, type = :string, options = {}, &block) ⇒ Object
element(name, [type[, options]]) -> Mappings::Element or Mappings::ElementValue
element(name[, options]) { block } -> Mappings::Element
Defines single-element mapping.
Arguments
name-
Accessor name
type-
Element type.
:stringassumed if omitted (seeConverter). options-
:xmlname,:xmlns, converter options (seeConverter). block-
An anonymous class definition.
Example:
class Cat
include XmlNuts::Nut
...
element :name, :string, :whitespace => :collapse
element :cheeseburger, Cheeseburger, :xmlname => :cheezburger
...
end
74 75 76 77 78 |
# File 'lib/xmlnuts/nuts.rb', line 74 def element(name, type = :string, = {}, &block) type, = prepare_args(type, , &block) define_accessor name (mappings << (type.is_a?(Class) ? Element : ElementValue).new(name, type, ())).last end |
#elements(name, type = :string, options = {}, &block) ⇒ Object
elements(name, [type[, options]]) -> Mappings::Element or Mappings::ElementValue
elements(name[, options]) { block } -> Mappings::Element
Defines multiple elements mapping.
Arguments
name-
Accessor name
type-
Element type.
:stringassumed if omitted (seeConverter). options-
:xmlname,:xmlns, converter options (seeConverter). block-
An anonymous class definition.
Example:
class RichCat
include XmlNuts::Nut
...
elements :ration, :string, :whitespace => :collapse
elements :cheeseburgers, Cheeseburger, :xmlname => :cheezburger
...
end
99 100 101 102 103 |
# File 'lib/xmlnuts/nuts.rb', line 99 def elements(name, type = :string, = {}, &block) type, = prepare_args(type, , &block) define_accessor name (mappings << (type.is_a?(Class) ? Elements : ElementValues).new(name, type, ())).last end |
#mappings ⇒ Object
mappings -> Array
Returns all mappings defined on a class.
130 131 132 |
# File 'lib/xmlnuts/nuts.rb', line 130 def mappings @mappings ||= [] end |
#namespaces(mappings = nil) ⇒ Object
namespaces(hash) -> Hash
namespaces -> Hash
Updates and returns class-level prefix mappings. When given a hash of mappings merges it over current. When called withot arguments simply returns current mappings.
Example:
class Cat
include XmlNuts::Nut
namespaces :lol => 'urn:lol', ...
...
end
28 29 30 31 |
# File 'lib/xmlnuts/nuts.rb', line 28 def namespaces(mappings = nil) @namespaces ||= {} mappings ? @namespaces.update(mappings) : @namespaces end |
#parse(source, options = {}) ⇒ Object
134 135 136 |
# File 'lib/xmlnuts/nuts.rb', line 134 def parse(source, = {}) backend.parse(source, ) {|node| parse_node(new, node) } end |
#parse_node(nut, node) ⇒ Object
:nodoc:
151 152 153 154 |
# File 'lib/xmlnuts/nuts.rb', line 151 def parse_node(nut, node) #:nodoc: callem(:from_xml, nut, node) nut end |
#root(xmlname = nil, options = {}) ⇒ Object
root(xmlname[, :xmlns => …]) -> Mappings::Root
root -> Mappings::Root
Defines element name. TODO: moar details
Arguments
xmlname-
Element name
options-
<tt>:xmlns => <tt> Element namespace
Example:
class Cat
include XmlNuts::Nut
...
root :kitteh, :xmlns => 'urn:lol'
...
end
50 51 52 53 |
# File 'lib/xmlnuts/nuts.rb', line 50 def root(xmlname = nil, = {}) @root = Root.new(xmlname, ()) if xmlname @root ||= Root.new('root') end |