Module: XmlNuts::Nut::ClassMethods

Includes:
Mappings, XmlBackend
Defined in:
lib/xmlnuts/nuts.rb

Overview

See also Nut.

Instance Method Summary collapse

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. :string assumed if omitted (see Converter).

options

:xmlname, :xmlns, converter options (see Converter).

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, options = {})
  define_accessor name
  mappings << Attribute.new(name, type, prepare_options(options))
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, options = {})
  options, result = result, :string if result.is_a?(Hash)
  options[:xmlname] ||= root.xmlname
  options[:xmlns_prefix] = namespaces.invert[options[:xmlns] ||= root.xmlns]
  backend.build(result, options) {|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. :string assumed if omitted (see Converter).

options

:xmlname, :xmlns, converter options (see Converter).

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, options = {}, &block)
  type, options = prepare_args(type, options, &block)
  define_accessor name
  (mappings << (type.is_a?(Class) ? Element : ElementValue).new(name, type, prepare_options(options))).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. :string assumed if omitted (see Converter).

options

:xmlname, :xmlns, converter options (see Converter).

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, options = {}, &block)
  type, options = prepare_args(type, options, &block)
  define_accessor name
  (mappings << (type.is_a?(Class) ? Elements : ElementValues).new(name, type, prepare_options(options))).last
end

#mappingsObject

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, options = {})
  backend.parse(source, options) {|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, options = {})
  @root = Root.new(xmlname, prepare_options(options)) if xmlname
  @root ||= Root.new('root')
end