Module: Atomic::Parser

Defined Under Namespace

Modules: ClassMethods Classes: Node

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



8
9
10
# File 'lib/atomic/parser.rb', line 8

def self.included(base)
  base.extend(ClassMethods)
end

Instance Method Details

#deserialize(reader) ⇒ Object



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
51
52
53
54
55
# File 'lib/atomic/parser.rb', line 24

def deserialize(reader)
  stack = []
  start_depth = reader.depth
  loop do
    node = Node.new(reader.depth - start_depth, reader.local_name, reader.namespace_uri, reader.attributes)
    if (stack.empty? || node.depth > stack.last.depth)
      # child
      if(reader.value?)
        stack.last.text = reader.value unless stack.empty?
        reader.read
      else
        redo if handle_open_element(node,reader)
        stack.push(node)
      end
    elsif(node.depth < stack.last.depth)
      # parent
      handle_close_element(stack.last)
      stack.pop
    else
      # sibling
      handle_close_element(stack.last)
      stack.pop
      unless stack.empty?
        redo if handle_open_element(node,reader)
        stack.push(node)
      end
    end
    break if stack.empty?
    reader.read
  end
  return self
end

#handle_close_element(node) ⇒ Object



63
64
65
# File 'lib/atomic/parser.rb', line 63

def handle_close_element(node)
  puts("Close Element - #{node.inspect}")
end

#handle_open_element(node, reader) ⇒ Object



57
58
59
60
61
# File 'lib/atomic/parser.rb', line 57

def handle_open_element(node, reader)
  progressed = false
  puts("Open Element - #{node.inspect}")
  return progressed
end