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.

  1. Check if @emitter is a StyledTreeBuilder.

  2. If #1 and target is a Blueberry, get the Styler(s) from target and add them to @emitter.

  3. If #1 and @emitter.deref_aliases?, prevent target from becoming an alias.

  4. Call super and store the result.

  5. If #2, remove the Styler(s) from @emitter.

  6. 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



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
96
97
# File 'lib/psychgus/ext/yaml_tree_ext.rb', line 71

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