Class: Peanuts::Mapper

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/peanuts/mapper.rb

Defined Under Namespace

Classes: Footprint, MappingFootprint, ReaderFootprint

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ns_context = nil, default_ns = nil) ⇒ Mapper

Returns a new instance of Mapper.



10
11
12
13
14
# File 'lib/peanuts/mapper.rb', line 10

def initialize(ns_context = nil, default_ns = nil)
  @ns_context, @default_ns = ns_context, default_ns
  @mappings, @footprints = [], {}
  @namespaces = ns_context ? Hash.new {|h, k| ns_context[k] || raise(IndexError) } : {}
end

Instance Attribute Details

#default_nsObject (readonly)

Returns the value of attribute default_ns.



7
8
9
# File 'lib/peanuts/mapper.rb', line 7

def default_ns
  @default_ns
end

#namespacesObject (readonly)

Returns the value of attribute namespaces.



7
8
9
# File 'lib/peanuts/mapper.rb', line 7

def namespaces
  @namespaces
end

#ns_contextObject (readonly)

Returns the value of attribute ns_context.



7
8
9
# File 'lib/peanuts/mapper.rb', line 7

def ns_context
  @ns_context
end

#rootObject

Returns the value of attribute root.



7
8
9
# File 'lib/peanuts/mapper.rb', line 7

def root
  @root
end

#schemaObject

Returns the value of attribute schema.



8
9
10
# File 'lib/peanuts/mapper.rb', line 8

def schema
  @schema
end

Instance Method Details

#<<(mapping) ⇒ Object



26
27
28
29
30
# File 'lib/peanuts/mapper.rb', line 26

def <<(mapping)
  fp = MappingFootprint.new(mapping)
  raise "mapping already defined for #{fp}" if @footprints.include?(fp)
  @mappings << (@footprints[fp] = mapping)
end

#clear(nut) ⇒ Object



45
46
47
# File 'lib/peanuts/mapper.rb', line 45

def clear(nut)
  @mappings.each {|m| m.clear(nut) }
end

#each(&block) ⇒ Object



22
23
24
# File 'lib/peanuts/mapper.rb', line 22

def each(&block)
  @mappings.each(&block)
end

#restore(nut, reader) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/peanuts/mapper.rb', line 32

def restore(nut, reader)
  rdfp = ReaderFootprint.new(reader)
  reader.each do
    m = @footprints[rdfp]
    m.restore(nut, reader) if m
  end
  nut
end

#save(nut, writer) ⇒ Object



41
42
43
# File 'lib/peanuts/mapper.rb', line 41

def save(nut, writer)
  @root ? @root.save(writer) { _save(nut, writer) } : _save(nut, writer)
end