Module: Peanuts::ClassMethods
- Includes:
- Mappings
- Defined in:
- lib/peanuts/nuts.rb
Overview
See also InstanceMethods
.
Instance Attribute Summary collapse
-
#mapper ⇒ Object
readonly
mapper -> Mapper.
Instance Method Summary collapse
- #_source_or_dest(*args) {|type, source, options| ... } ⇒ Object
-
#attribute(name, *args) ⇒ Object
attribute(name, [type[, options]]) -> Mappings::Attribute or Mappings::AttributeValue.
-
#element(name, *args, &block) ⇒ Object
element(name, [type[, options]]) -> Mappings::Element or Mappings::ElementValue element(name[, options]) { block } -> Mappings::Element.
-
#elements(name, *args, &block) ⇒ Object
elements(name, [type[, options]]) -> Mappings::Element or Mappings::ElementValue elements(name[, options]) { block } -> Mappings::Element.
-
#namespaces(map = nil) ⇒ Object
namespaces(hash) -> Hash namespaces -> Hash.
- #restore(reader) ⇒ Object
- #restore_from(*args) ⇒ Object
-
#root(xmlname = nil, options = {}) ⇒ Object
root(xmlname[, :xmlns => …]) -> Mappings::Root root -> Mappings::Root.
- #save(nut, writer) ⇒ Object
- #schema(schema = nil) ⇒ Object
Instance Attribute Details
#mapper ⇒ Object (readonly)
mapper -> Mapper
Returns the mapper for the class.
25 26 27 |
# File 'lib/peanuts/nuts.rb', line 25 def mapper @mapper end |
Instance Method Details
#_source_or_dest(*args) {|type, source, options| ... } ⇒ Object
163 164 165 166 167 168 |
# File 'lib/peanuts/nuts.rb', line 163 def _source_or_dest(*args) = args.last.is_a?(Hash) ? args.pop : {} type, source = *args type, source = :string, type unless type.is_a?(Symbol) yield type, source, end |
#attribute(name, *args) ⇒ Object
attribute(name, [type[, options]]) -> Mappings::Attribute or Mappings::AttributeValue
Defines attribute mapping.
Arguments
name
-
Accessor name
type
-
Element type.
:string
assumed if omitted (seeConverter
). options
-
:xmlname
,:xmlns
, converter options (seeConverter
).
Example:
class Cat
include Peanuts
...
element :name, :string, :whitespace => :collapse
element :cheeseburger, Cheeseburger, :xmlname => :cheezburger
...
end
135 136 137 138 139 140 |
# File 'lib/peanuts/nuts.rb', line 135 def attribute(name, *args) prepare_args(args, nil, :xmlns => nil) do |type, | define_accessor name mapper << Attribute.new(name, type, ) end end |
#element(name, *args, &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.
:string
assumed if omitted (seeConverter
). options
-
:xmlname
,:xmlns
, converter options (seeConverter
). block
-
An anonymous class definition.
Example:
class Cat
include Peanuts
...
element :name, :string, :whitespace => :collapse
element :cheeseburger, Cheeseburger, :xmlname => :cheezburger
...
end
85 86 87 88 89 90 |
# File 'lib/peanuts/nuts.rb', line 85 def element(name, *args, &block) prepare_args(args, block) do |type, | define_accessor name (mapper << (type.is_a?(Class) ? Element : ElementValue).new(name, type, )).last end end |
#elements(name, *args, &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.
:string
assumed if omitted (seeConverter
). options
-
:xmlname
,:xmlns
, converter options (seeConverter
). block
-
An anonymous class definition.
Example:
class RichCat
include Peanuts
...
elements :ration, :string, :whitespace => :collapse
elements :cheeseburgers, Cheeseburger, :xmlname => :cheezburger
...
end
111 112 113 114 115 116 |
# File 'lib/peanuts/nuts.rb', line 111 def elements(name, *args, &block) prepare_args(args, block) do |type, | define_accessor name (mapper << (type.is_a?(Class) ? Elements : ElementValues).new(name, type, )).last end end |
#namespaces(map = 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 Peanuts
namespaces :lol => 'urn:lol', ...
...
end
40 41 42 |
# File 'lib/peanuts/nuts.rb', line 40 def namespaces(map = nil) map ? mapper.namespaces.update(map) : mapper.namespaces end |
#restore(reader) ⇒ Object
147 148 149 150 |
# File 'lib/peanuts/nuts.rb', line 147 def restore(reader) e = reader.find_element e && _restore(e) end |
#restore_from(*args) ⇒ Object
152 153 154 155 156 |
# File 'lib/peanuts/nuts.rb', line 152 def restore_from(*args) _source_or_dest(*args) do |source_type, source, | restore(XML::Reader.new(source, source_type, )) end 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 Peanuts
...
root :kitteh, :xmlns => 'urn:lol'
...
end
61 62 63 64 |
# File 'lib/peanuts/nuts.rb', line 61 def root(xmlname = nil, = {}) mapper.root = Root.new(xmlname, ()) if xmlname mapper.root end |
#save(nut, writer) ⇒ Object
158 159 160 161 |
# File 'lib/peanuts/nuts.rb', line 158 def save(nut, writer) _save(nut, writer) writer.result end |
#schema(schema = nil) ⇒ Object
142 143 144 145 |
# File 'lib/peanuts/nuts.rb', line 142 def schema(schema = nil) mapper.schema = schema if schema mapper.schema end |