Module: Psychgus::Ext::YAMLTreeExt

Defined in:
lib/psychgus/ext/yaml_tree_ext.rb

Overview

Extensions to Psych::Visitors::YAMLTree.

Author:

  • Jonathan Bradley Whited (@esotericpig)

Since:

  • 1.0.0

Instance Method Summary collapse

Instance Method Details

#accept(target) ⇒ Object

Accepts a new Object to convert to YAML.

This is roughly the same place where Psych checks if target responds to :encode_with.

This will check if @emitter is a StyledTreeBuilder and if target is a Blueberry.

  1. If the above is true, get the Styler(s) and add them to @emitter.

  2. Call super.

  3. If the above is true, remove the Styler(s) from @emitter.

  4. Return the result of super.

Parameters:

  • target (Object)

    the Object to pass to super

Returns:

  • the result of super

See Also:

Since:

  • 1.0.0


69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/psychgus/ext/yaml_tree_ext.rb', line 69

def accept(target)
  styler_count = 0
  
  if @emitter.is_a?(StyledTreeBuilder)
    # Blueberry?
    if target.respond_to?(:psychgus_stylers)
      stylers = target.psychgus_stylers(@emitter.sniffer)
      stylers_old_len = @emitter.stylers.length
      
      @emitter.add_styler(*stylers)
      
      styler_count = @emitter.stylers.length - stylers_old_len
    end
    
    # Dereference aliases?
    if @emitter.deref_aliases?()
      @st.remove_alias(target) if target.respond_to?(:object_id) && @st.key?(target)
    end
  end
  
  result = super(target)
  
  # Check styler_count because @emitter may not be a StyledTreeBuilder and target may not be a Blueberry
  @emitter.pop_styler(styler_count) if styler_count > 0
  
  return result
end