Module: Peanuts::ClassMethods

Includes:
Mappings
Defined in:
lib/peanuts/nuts.rb

Overview

See also InstanceMethods.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#mapperObject (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

Yields:

  • (type, source, options)


163
164
165
166
167
168
# File 'lib/peanuts/nuts.rb', line 163

def _source_or_dest(*args)
  options = args.last.is_a?(Hash) ? args.pop : {}
  type, source = *args
  type, source = :string, type unless type.is_a?(Symbol)
  yield type, source, options
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 (see Converter).

options

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

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, options|
    define_accessor name
    mapper << Attribute.new(name, type, options)
  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 (see Converter).

options

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

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, options|
    define_accessor name
    (mapper << (type.is_a?(Class) ? Element : ElementValue).new(name, type, options)).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 (see Converter).

options

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

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, options|
    define_accessor name
    (mapper << (type.is_a?(Class) ? Elements : ElementValues).new(name, type, options)).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, options|
    restore(XML::Reader.new(source, source_type, options))
  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, options = {})
  mapper.root = Root.new(xmlname, prepare_options(options)) 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