Class: SafeYAML::PsychHandler

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

Instance Method Summary collapse

Constructor Details

#initialize(options, &block) ⇒ PsychHandler

Returns a new instance of PsychHandler.



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

def initialize(options, &block)
  @options      = SafeYAML::OPTIONS.merge(options || {})
  @block        = block
  @initializers = @options[:custom_initializers] || {}
  @anchors      = {}
  @stack        = []
  @current_key  = nil
  @result       = nil
  @begun        = false
end

Instance Method Details

#add_to_current_structure(value, anchor = nil, quoted = nil, tag = nil) ⇒ Object



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
51
52
53
# File 'lib/safe_yaml/psych_handler.rb', line 21

def add_to_current_structure(value, anchor=nil, quoted=nil, tag=nil)
  value = Transform.to_proper_type(value, quoted, tag, @options)

  @anchors[anchor] = value if anchor

  if !@begun
    @begun = true
    @result = value
    @current_structure = @result
    return
  end

  if @current_structure.respond_to?(:<<)
    @current_structure << value

  elsif @current_structure.respond_to?(:[]=)
    if @current_key.nil?
      @current_key = value

    else
      if @current_key == "<<"
        @current_structure.merge!(value)
      else
        @current_structure[@current_key] = value
      end

      @current_key = nil
    end

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

#alias(anchor) ⇒ Object

event handlers



65
66
67
# File 'lib/safe_yaml/psych_handler.rb', line 65

def alias(anchor)
  add_to_current_structure(@anchors[anchor])
end

#end_current_structureObject



55
56
57
58
# File 'lib/safe_yaml/psych_handler.rb', line 55

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

#end_document(implicit) ⇒ Object



73
74
75
# File 'lib/safe_yaml/psych_handler.rb', line 73

def end_document(implicit)
  @block.call(@result)
end

#end_mappingObject



84
85
86
# File 'lib/safe_yaml/psych_handler.rb', line 84

def end_mapping
  self.end_current_structure()
end

#end_sequenceObject



95
96
97
# File 'lib/safe_yaml/psych_handler.rb', line 95

def end_sequence
  self.end_current_structure()
end

#resultObject



17
18
19
# File 'lib/safe_yaml/psych_handler.rb', line 17

def result
  @begun ? @result : false
end

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



69
70
71
# File 'lib/safe_yaml/psych_handler.rb', line 69

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

#start_mapping(anchor, tag, implicit, style) ⇒ Object



77
78
79
80
81
82
# File 'lib/safe_yaml/psych_handler.rb', line 77

def start_mapping(anchor, tag, implicit, style)
  map = @initializers.include?(tag) ? @initializers[tag].call : {}
  self.add_to_current_structure(map, anchor)
  @current_structure = map
  @stack.push(map)
end

#start_sequence(anchor, tag, implicit, style) ⇒ Object



88
89
90
91
92
93
# File 'lib/safe_yaml/psych_handler.rb', line 88

def start_sequence(anchor, tag, implicit, style)
  seq = @initializers.include?(tag) ? @initializers[tag].call : []
  self.add_to_current_structure(seq, anchor)
  @current_structure = seq
  @stack.push(seq)
end

#streaming?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/safe_yaml/psych_handler.rb', line 60

def streaming?
  true
end