Class: SafeYAML::Handler

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

Instance Method Summary collapse

Constructor Details

#initializeHandler

Returns a new instance of Handler.



5
6
7
# File 'lib/handler.rb', line 5

def initialize
  @stack = []
end

Instance Method Details

#add_to_current_structure(value) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/handler.rb', line 13

def add_to_current_structure(value)
  if @result.nil?
    @result = value
    @current_structure = @result
    return
  end

  case @current_structure
  when Array
    @current_structure.push(transform_value(value))

  when Hash
    if @current_key.nil?
      @current_key = transform_value(value)
    else
      @current_structure[@current_key] = transform_value(value)
      @current_key = nil
    end

  else
    raise "Don't know how to add to a #{@current_structure.class}!"
  end
end

#end_mappingObject



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

def end_mapping
  @stack.pop
  @current_structure = @stack.last
end

#end_sequenceObject



81
82
83
84
# File 'lib/handler.rb', line 81

def end_sequence
  @stack.pop
  @current_structure = @stack.last
end

#resultObject



9
10
11
# File 'lib/handler.rb', line 9

def result
  @result
end

#scalar(value, anchor, tag, plain, quoted, style) ⇒ Object

event handlers



58
59
60
# File 'lib/handler.rb', line 58

def scalar(value, anchor, tag, plain, quoted, style)
  add_to_current_structure(value)
end

#start_mapping(*args) ⇒ Object

anchor, tag, implicit, style



62
63
64
65
66
67
# File 'lib/handler.rb', line 62

def start_mapping(*args) # anchor, tag, implicit, style
  map = {}
  self.add_to_current_structure(map)
  @current_structure = map
  @stack.push(map)
end

#start_sequence(*args) ⇒ Object

anchor, tag, implicit, style



74
75
76
77
78
79
# File 'lib/handler.rb', line 74

def start_sequence(*args) # anchor, tag, implicit, style
  seq = []
  self.add_to_current_structure(seq)
  @current_structure = seq
  @stack.push(seq)
end

#streaming?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/handler.rb', line 53

def streaming?
  false
end

#transform_value(value) ⇒ Object



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

def transform_value(value)
  if value.is_a?(String)
    if value.match(/^:\w+$/)
      return value[1..-1].to_sym

    elsif value.match(/^\d+$/)
      return value.to_i

    elsif value.match(/^\d+(?:\.\d*)?$/) || value.match(/^\.\d+$/)
      return value.to_f
    end
  end

  value
end