Class: Psychgus::SuperSniffer::Parent

Inherits:
Delegator
  • Object
show all
Defined in:
lib/psychgus/super_sniffer/parent.rb

Overview

A container for the parent of a Psych::Nodes::Node.

A parent is a Mapping, Sequence, or a Key (Scalar) of a Mapping.

You can use the getters in this class in Psychgus::Styler to filter what to change.

If a Node method has not been exposed, you can use #node:

if parent.node_of?(:scalar)
  parent.value = 'FUBAR'
  parent.node.value = 'FUBAR' # Same as above

  parent.fubar = true      # NoMethodError
  parent.node.fubar = true # Use some new Psych::Nodes::Node method not in this version
                           #   of Psychgus or that is not exposed by Parent
end

See Also:

Author:

  • Jonathan Bradley Whited (@esotericpig)

Since:

  • 1.0.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sniffer, node, debug_tag: nil, child_type: nil) ⇒ Parent

Initialize this class with parent data.

Parameters:

  • sniffer (SuperSniffer)

    the sniffer that contains this parent (not stored; used for data)

  • node (Psych::Nodes::Node)

    the node of this parent

  • debug_tag (:noface, Symbol, String) (defaults to: nil)

    the tag (class name, value) used for debugging and in #to_s

  • child_type (nil, :key, :value) (defaults to: nil)

    the next child's Mapping type, if node is a Mapping

Since:

  • 1.0.0



74
75
76
77
78
79
80
81
# File 'lib/psychgus/super_sniffer/parent.rb', line 74

def initialize(sniffer,node,debug_tag: nil,child_type: nil)
  @child_position = 1
  @child_type = child_type
  @debug_tag = debug_tag
  @level = sniffer.level
  @node = node
  @position = sniffer.position
end

Instance Attribute Details

#child_positionInteger

Calling the getter is fine; calling the setter is not and could cause weird results.

Returns:

  • (Integer)

    the next child's position

Since:

  • 1.0.0



54
55
56
# File 'lib/psychgus/super_sniffer/parent.rb', line 54

def child_position
  @child_position
end

#child_typenil, ...

Calling the getter is fine; calling the setter is not and could cause weird results.

Returns:

  • (nil, :key, :value)

    the next child's Mapping type, if #node is a Mapping

Since:

  • 1.0.0



59
60
61
# File 'lib/psychgus/super_sniffer/parent.rb', line 59

def child_type
  @child_type
end

#debug_tag:noface, ... (readonly)

Returns a tag (class name, value) for debugging; also used in #to_s.

Returns:

  • (:noface, Symbol, String)

    a tag (class name, value) for debugging; also used in #to_s

Since:

  • 1.0.0



62
63
64
# File 'lib/psychgus/super_sniffer/parent.rb', line 62

def debug_tag
  @debug_tag
end

#levelInteger (readonly)

Returns the level of this Node in the YAML.

Returns:

  • (Integer)

    the level of this Node in the YAML

Since:

  • 1.0.0



64
65
66
# File 'lib/psychgus/super_sniffer/parent.rb', line 64

def level
  @level
end

#nodePsych::Nodes::Node (readonly)

Returns the Node of this parent.

Returns:

  • (Psych::Nodes::Node)

    the Node of this parent

Since:

  • 1.0.0



65
66
67
# File 'lib/psychgus/super_sniffer/parent.rb', line 65

def node
  @node
end

#positionInteger (readonly)

Returns the position of this Node in the YAML.

Returns:

  • (Integer)

    the position of this Node in the YAML

Since:

  • 1.0.0



66
67
68
# File 'lib/psychgus/super_sniffer/parent.rb', line 66

def position
  @position
end

Instance Method Details

#__getobj__Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 1.0.0



84
85
86
# File 'lib/psychgus/super_sniffer/parent.rb', line 84

def __getobj__
  return @node
end

#child_key?true, false

Check if the children of this parent are keys to a Mapping.

Returns:

  • (true, false)

    whether the children are keys to a Mapping

Since:

  • 1.2.0



93
94
95
# File 'lib/psychgus/super_sniffer/parent.rb', line 93

def child_key?()
  return @child_type == :key
end

#child_value?true, false

Check if the children of this parent are values to a Mapping (i.e., values to a key).

Returns:

  • (true, false)

    whether the children are values to a Mapping (i.e., values to a key)

Since:

  • 1.2.0



102
103
104
# File 'lib/psychgus/super_sniffer/parent.rb', line 102

def child_value?()
  return @child_type == :value
end

#implicit?Boolean

Returns:

  • (Boolean)

See Also:

  • Psych::Nodes::Document#implicit
  • Psych::Nodes::Mapping#implicit
  • Psych::Nodes::Sequence#implicit

Since:

  • 1.0.0



109
110
111
# File 'lib/psychgus/super_sniffer/parent.rb', line 109

def implicit?()
  return @node.implicit
end

#implicit_end?Boolean

Returns:

  • (Boolean)

See Also:

  • Psych::Nodes::Document#implicit_end

Since:

  • 1.0.0



114
115
116
# File 'lib/psychgus/super_sniffer/parent.rb', line 114

def implicit_end?()
  return @node.implicit_end
end

#node_of?(*names) ⇒ true, false

Check if this Node is of a certain type (Alias, Mapping, Scalar, Sequence, etc.).

New versions of Psych have alias?(), mapping?(), etc., so this is for old versions.

This is equivalent to the following (with less typing):

node.is_a?(Psych::Nodes::Alias)
node.is_a?(Psych::Nodes::Mapping)
node.is_a?(Psych::Nodes::Scalar)
node.is_a?(Psych::Nodes::Sequence)

Examples:

node.node_of?(:alias)
node.node_of?(:mapping)
node.node_of?(:scalar)
node.node_of?(:sequence)
node.node_of?(:alias,:mapping,:scalar,:sequence) # OR
node.node_of?(:doc,:map,:seq) # OR

Parameters:

  • names (Symbol, String)

    the type(s) to check using OR

Returns:

  • (true, false)

    true if this Node is one of the names type, else false

See Also:

Since:

  • 1.0.0



119
120
121
# File 'lib/psychgus/super_sniffer/parent.rb', line 119

def node_of?(*names)
  return @node.node_of?(*names)
end

#plain?Boolean

Returns:

  • (Boolean)

See Also:

  • Psych::Nodes::Scalar#plain

Since:

  • 1.0.0



124
125
126
# File 'lib/psychgus/super_sniffer/parent.rb', line 124

def plain?()
  return @node.plain
end

#quoted?Boolean

Returns:

  • (Boolean)

See Also:

  • Psych::Nodes::Scalar#quoted

Since:

  • 1.0.0



129
130
131
# File 'lib/psychgus/super_sniffer/parent.rb', line 129

def quoted?()
  return @node.quoted
end

#to_sString

Note:

If this method is modified, then tests will fail

Returns a String representation of this class for debugging and testing.

Returns:

  • (String)

    a String representation of this class for debugging and testing

Since:

  • 1.0.0



136
137
138
# File 'lib/psychgus/super_sniffer/parent.rb', line 136

def to_s()
  return "<#{@debug_tag}:(#{@level}:#{@position}):#{@child_type}:(:#{@child_position})>"
end