Class: SMG::Document

Inherits:
Nokogiri::XML::SAX::Document
  • Object
show all
Defined in:
lib/smg/document.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object, context = nil, thing = nil) ⇒ Document

Returns a new instance of Document.



6
7
8
9
10
11
12
13
14
15
# File 'lib/smg/document.rb', line 6

def initialize(object, context = nil, thing = nil)
  @object   = object
  @mapping  = object.class.mapping
  @stack    = []
  @docs     = []
  @elements = []
  @thing    = thing
  @context  = context
  @parsed   = []
end

Instance Attribute Details

#objectObject (readonly)

Returns the value of attribute object.



4
5
6
# File 'lib/smg/document.rb', line 4

def object
  @object
end

#thingObject (readonly)

Returns the value of attribute thing.



4
5
6
# File 'lib/smg/document.rb', line 4

def thing
  @thing
end

Instance Method Details

#cdata_block(string) ⇒ Object



77
78
79
# File 'lib/smg/document.rb', line 77

def cdata_block(string)
  characters(string)
end

#characters(string) ⇒ Object



72
73
74
75
# File 'lib/smg/document.rb', line 72

def characters(string)
  @docs.each { |doc| doc.characters(string) }
  @elements.each { |e| e.last << string }
end

#end_element(name) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/smg/document.rb', line 52

def end_element(name)

  if e = @elements.last and e.first.path == @stack
    e,chars = *@elements.pop
    @object.__send__(e.accessor, e.cast(chars))
    @parsed << e.object_id unless e.collection?
  end

  @docs.each { |doc| doc.end_element(name) }

  if doc = @docs.last and doc.thing.path == @stack
    @object.__send__(doc.thing.accessor, doc.object)
    @parsed << doc.thing.object_id unless doc.thing.collection?
    @docs.pop
  end

  @stack.pop

end

#start_element(name, attrs) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/smg/document.rb', line 17

def start_element(name, attrs)

  @stack << name
  ahash = nil

  if thing = @mapping.nested[@stack] and
     !@parsed.include?(thing.object_id) &&
     thing.in_context_of?(@context) &&
     thing.with?(ahash ||= Hash[*attrs.flatten])
    @docs << Document.new(thing.data_class.new,@context,thing)
  end

  @docs.each { |doc| doc.start_element(name,attrs) }

  if !attrs.empty? && maps = @mapping.attributes[@stack]
    #maps.values_at(*(ahash ||= Hash[*attrs]).keys).compact.each do |m|
    maps.values_at(*(ahash ||= Hash[*attrs.flatten]).keys).flatten.compact.each do |m|
      if !@parsed.include?(m.object_id) &&
         m.in_context_of?(@context) &&
         m.with?(ahash)
        @object.__send__(m.accessor, m.cast(ahash[m.at]))
        @parsed << m.object_id unless m.collection?
      end
    end
  end

  if e = @mapping.elements[@stack] and
     !@parsed.include?(e.object_id) &&
     e.in_context_of?(@context) &&
     e.with?(ahash ||= Hash[*attrs.flatten])
    @elements << [e,""]
  end

end