Class: SafeYAML::PsychHandler
- Inherits:
-
Psych::Handler
- Object
- Psych::Handler
- SafeYAML::PsychHandler
- Defined in:
- lib/safe_yaml/psych_handler.rb
Instance Method Summary collapse
- #add_to_current_structure(value, anchor = nil, quoted = nil, tag = nil) ⇒ Object
-
#alias(anchor) ⇒ Object
event handlers.
- #end_current_structure ⇒ Object
- #end_mapping ⇒ Object
- #end_sequence ⇒ Object
-
#initialize(options) ⇒ PsychHandler
constructor
A new instance of PsychHandler.
- #result ⇒ Object
- #scalar(value, anchor, tag, plain, quoted, style) ⇒ Object
- #start_mapping(anchor, tag, implicit, style) ⇒ Object
- #start_sequence(anchor, tag, implicit, style) ⇒ Object
- #streaming? ⇒ Boolean
Constructor Details
#initialize(options) ⇒ PsychHandler
Returns a new instance of PsychHandler.
6 7 8 9 10 11 12 13 |
# File 'lib/safe_yaml/psych_handler.rb', line 6 def initialize() @options = SafeYAML::OPTIONS.merge( || {}) @initializers = @options[:custom_initializers] || {} @anchors = {} @stack = [] @current_key = nil @result = nil end |
Instance Method Details
#add_to_current_structure(value, anchor = nil, quoted = nil, tag = nil) ⇒ Object
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 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/safe_yaml/psych_handler.rb', line 19 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 @result.nil? @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 elsif @current_structure.nil? # It appears that a YAML document may containing trailing text that should not be considered # part of the serialized data. See issue 48: # # https://github.com/dtao/safe_yaml/issues/48 # # I need to investigate this a bit further; but for now just explicitly ignoring nil should # fix the issue (since in theory the only scenario where this would happen is after the # serialized structure has "closed"). else raise "Don't know how to add to a #{@current_structure.class}!" end end |
#alias(anchor) ⇒ Object
event handlers
72 73 74 |
# File 'lib/safe_yaml/psych_handler.rb', line 72 def alias(anchor) add_to_current_structure(@anchors[anchor]) end |
#end_current_structure ⇒ Object
62 63 64 65 |
# File 'lib/safe_yaml/psych_handler.rb', line 62 def end_current_structure @stack.pop @current_structure = @stack.last end |
#end_mapping ⇒ Object
87 88 89 |
# File 'lib/safe_yaml/psych_handler.rb', line 87 def end_mapping self.end_current_structure() end |
#end_sequence ⇒ Object
98 99 100 |
# File 'lib/safe_yaml/psych_handler.rb', line 98 def end_sequence self.end_current_structure() end |
#result ⇒ Object
15 16 17 |
# File 'lib/safe_yaml/psych_handler.rb', line 15 def result @result end |
#scalar(value, anchor, tag, plain, quoted, style) ⇒ Object
76 77 78 |
# File 'lib/safe_yaml/psych_handler.rb', line 76 def scalar(value, anchor, tag, plain, quoted, style) add_to_current_structure(value, anchor, quoted, tag) end |
#start_mapping(anchor, tag, implicit, style) ⇒ Object
80 81 82 83 84 85 |
# File 'lib/safe_yaml/psych_handler.rb', line 80 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
91 92 93 94 95 96 |
# File 'lib/safe_yaml/psych_handler.rb', line 91 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
67 68 69 |
# File 'lib/safe_yaml/psych_handler.rb', line 67 def streaming? false end |