Class: Yayaml::Handler

Inherits:
Psych::Handler
  • Object
show all
Defined in:
lib/yayaml/handler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(matcher, filename: nil) ⇒ Handler

Returns a new instance of Handler.



15
16
17
18
# File 'lib/yayaml/handler.rb', line 15

def initialize(matcher, filename: nil)
  @matcher = matcher
  @filename = filename
end

Instance Attribute Details

#filenameObject (readonly)

State transitions: branch -> start_map -> branch (no-op) leaf -> start_map -> branch (no-op) branch -> scalar -> leaf (push key) leaf -> scalar -> leaf (pop key, handle value) branch -> end_map -> branch (pop key) leaf -> end_map -> branch (pop key)



13
14
15
# File 'lib/yayaml/handler.rb', line 13

def filename
  @filename
end

#matcherObject (readonly)

State transitions: branch -> start_map -> branch (no-op) leaf -> start_map -> branch (no-op) branch -> scalar -> leaf (push key) leaf -> scalar -> leaf (pop key, handle value) branch -> end_map -> branch (pop key) leaf -> end_map -> branch (pop key)



13
14
15
# File 'lib/yayaml/handler.rb', line 13

def matcher
  @matcher
end

Instance Method Details

#end_mappingObject



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/yayaml/handler.rb', line 41

def end_mapping
  transition("end map") do
    case @state
    when :branch, :leaf
      @state = :branch
      @keys.pop
    else
      panic("end_mapping")
    end
  end
end

#event_location(start_line, start_column, end_line, end_column) ⇒ Object



69
70
71
72
# File 'lib/yayaml/handler.rb', line 69

def event_location(start_line, start_column, end_line, end_column)
  @line = start_line + 1
  @col = start_column + 1
end

#scalar(value, *args) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/yayaml/handler.rb', line 53

def scalar(value, *args)
  transition("scalar") do
    case @state
    when :branch
      @state = :leaf
      @keys.push(value)
    when :leaf
      @state = :branch
      matcher.on_node(@keys.dup, value)
      @keys.pop
    else
      panic("scalar")
    end
  end
end

#start_document(*args) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/yayaml/handler.rb', line 20

def start_document(*args)
  debug "--> Start document #{filename}"

  @state = :branch
  @prev = nil
  @keys = []
  @line = 0
  @col = 0
end

#start_mapping(*args) ⇒ Object



30
31
32
33
34
35
36
37
38
39
# File 'lib/yayaml/handler.rb', line 30

def start_mapping(*args)
  transition("start map") do
    case @state
    when :branch, :leaf
      @state = :branch
    else
      panic("start_mapping")
    end
  end
end