Class: Psychgus::SuperSniffer

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

Overview

Note:

You should never call the methods that are not readers, like #add_alias, #start_mapping, etc. unless you are extending this class (creating a subclass).

This is used in StyledTreeBuilder to “sniff” information about the YAML.

Then this information can be used in a Styler and/or a Blueberry.

Most information is straightforward:

  • #aliases # Array of Psych::Nodes::Alias processed so far

  • #mappings # Array of Psych::Nodes::Mapping processed so far

  • #nodes # Array of all Psych::Nodes::Node processed so far

  • #scalars # Array of Psych::Nodes::Scalar processed so far

  • #sequences # Array of Psych::Nodes::Sequence processed so far

#parent is the current Parent of the node being processed, which is nil for the first node.

#parents are all of the (grand)Parent(s) for the current node, which is empty for the first node.

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

#level and #position can be best understood by an example.

If you have this YAML:

Burgers:
   Classic:
     Sauce:  [Ketchup,Mustard]
     Cheese: American
     Bun:    Sesame Seed
   BBQ:
     Sauce:  Honey BBQ
     Cheese: Cheddar
     Bun:    Kaiser
   Fancy:
     Sauce:  Spicy Wasabi
     Cheese: Smoked Gouda
     Bun:    Hawaiian
 Toppings:
   - Mushrooms
   - [Lettuce, Onions, Pickles, Tomatoes]
   - [[Ketchup,Mustard], [Salt,Pepper]]

Then the levels and positions will be as follows:

# (level:position):current_node - <parent:(parent_level:parent_position)>

(1:1):Psych::Nodes::Stream - <nil>
(1:1):Psych::Nodes::Document - <stream:(1:1)>
(1:1):Psych::Nodes::Mapping - <doc:(1:1)>
 (2:1):Burgers - <map:(1:1)>
  (3:1):Psych::Nodes::Mapping - <Burgers:(2:1)>
   (4:1):Classic - <map:(3:1)>
    (5:1):Psych::Nodes::Mapping - <Classic:(4:1)>
     (6:1):Sauce - <map:(5:1)>
      (7:1):Psych::Nodes::Sequence - <Sauce:(6:1)>
       (8:1):Ketchup - <seq:(7:1)>
       (8:2):Mustard - <seq:(7:1)>
     (6:2):Cheese - <map:(5:1)>
      (7:1):American - <Cheese:(6:2)>
     (6:3):Bun - <map:(5:1)>
      (7:1):Sesame Seed - <Bun:(6:3)>
   (4:2):BBQ - <map:(3:1)>
    (5:1):Psych::Nodes::Mapping - <BBQ:(4:2)>
     (6:1):Sauce - <map:(5:1)>
      (7:1):Honey BBQ - <Sauce:(6:1)>
     (6:2):Cheese - <map:(5:1)>
      (7:1):Cheddar - <Cheese:(6:2)>
     (6:3):Bun - <map:(5:1)>
      (7:1):Kaiser - <Bun:(6:3)>
   (4:3):Fancy - <map:(3:1)>
    (5:1):Psych::Nodes::Mapping - <Fancy:(4:3)>
     (6:1):Sauce - <map:(5:1)>
      (7:1):Spicy Wasabi - <Sauce:(6:1)>
     (6:2):Cheese - <map:(5:1)>
      (7:1):Smoked Gouda - <Cheese:(6:2)>
     (6:3):Bun - <map:(5:1)>
      (7:1):Hawaiian - <Bun:(6:3)>
 (2:2):Toppings - <map:(1:1)>
  (3:1):Psych::Nodes::Sequence - <Toppings:(2:2)>
   (4:1):Mushrooms - <seq:(3:1)>
   (4:2):Psych::Nodes::Sequence - <seq:(3:1)>
    (5:1):Lettuce - <seq:(4:2)>
    (5:2):Onions - <seq:(4:2)>
    (5:3):Pickles - <seq:(4:2)>
    (5:4):Tomatoes - <seq:(4:2)>
   (4:3):Psych::Nodes::Sequence - <seq:(3:1)>
    (5:1):Psych::Nodes::Sequence - <seq:(4:3)>
     (6:1):Ketchup - <seq:(5:1)>
     (6:2):Mustard - <seq:(5:1)>
    (5:2):Psych::Nodes::Sequence - <seq:(4:3)>
     (6:1):Salt - <seq:(5:2)>
     (6:2):Pepper - <seq:(5:2)>

The “Super Sniffer” is the nickname for Gus's nose from the TV show Psych because he has a very refined sense of smell.

See Also:

Author:

  • Jonathan Bradley Whited (@esotericpig)

Since:

  • 1.0.0

Direct Known Subclasses

Empty

Defined Under Namespace

Classes: Empty, Parent

Constant Summary collapse

EMPTY =

Since:

  • 1.0.0

Empty.new().freeze()

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSuperSniffer

Initialize this class for sniffing.

Since:

  • 1.0.0


168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/psychgus/super_sniffer.rb', line 168

def initialize()
  @aliases = []
  @documents = []
  @level = 1
  @mappings = []
  @nodes = []
  @parent = nil
  @parents = []
  @position = 1
  @scalars = []
  @sequences = []
  @streams = []
end

Instance Attribute Details

#aliasesObject (readonly)

Since:

  • 1.0.0


155
156
157
# File 'lib/psychgus/super_sniffer.rb', line 155

def aliases
  @aliases
end

#documentsObject (readonly)

Since:

  • 1.0.0


156
157
158
# File 'lib/psychgus/super_sniffer.rb', line 156

def documents
  @documents
end

#levelObject (readonly)

Since:

  • 1.0.0


157
158
159
# File 'lib/psychgus/super_sniffer.rb', line 157

def level
  @level
end

#mappingsObject (readonly)

Since:

  • 1.0.0


158
159
160
# File 'lib/psychgus/super_sniffer.rb', line 158

def mappings
  @mappings
end

#nodesObject (readonly)

Since:

  • 1.0.0


159
160
161
# File 'lib/psychgus/super_sniffer.rb', line 159

def nodes
  @nodes
end

#parentObject (readonly)

Since:

  • 1.0.0


160
161
162
# File 'lib/psychgus/super_sniffer.rb', line 160

def parent
  @parent
end

#parentsObject (readonly)

Since:

  • 1.0.0


161
162
163
# File 'lib/psychgus/super_sniffer.rb', line 161

def parents
  @parents
end

#positionObject (readonly)

Since:

  • 1.0.0


162
163
164
# File 'lib/psychgus/super_sniffer.rb', line 162

def position
  @position
end

#scalarsObject (readonly)

Since:

  • 1.0.0


163
164
165
# File 'lib/psychgus/super_sniffer.rb', line 163

def scalars
  @scalars
end

#sequencesObject (readonly)

Since:

  • 1.0.0


164
165
166
# File 'lib/psychgus/super_sniffer.rb', line 164

def sequences
  @sequences
end

#streamsObject (readonly)

Since:

  • 1.0.0


165
166
167
# File 'lib/psychgus/super_sniffer.rb', line 165

def streams
  @streams
end

Instance Method Details

#add_alias(node) ⇒ Object

Add a Psych::Nodes::Alias to this class only (not to the YAML).

A Psychgus::Styler should probably never call this.

Parameters:

  • node (Psych::Nodes::Alias)

    the alias to add

See Also:

  • #add_child

Since:

  • 1.0.0


189
190
191
192
# File 'lib/psychgus/super_sniffer.rb', line 189

def add_alias(node)
  add_child(node)
  @aliases.push(node)
end

#add_scalar(node) ⇒ Object

Add a Psych::Nodes::Scalar to this class only (not to the YAML).

A Psychgus::Styler should probably never call this.

Parameters:

  • node (Psych::Nodes::Scalar)

    the scalar to add

See Also:

  • #add_child

Since:

  • 1.0.0


201
202
203
204
# File 'lib/psychgus/super_sniffer.rb', line 201

def add_scalar(node)
  add_child(node)
  @scalars.push(node)
end

#end_documentObject

End a Psych::Nodes::Document started with #start_document.

Pops off a parent from #parents and sets #parent to the last one. #level and #position are reset according to the last parent.

A Psychgus::Styler should probably never call this.

Since:

  • 1.0.0


212
213
214
# File 'lib/psychgus/super_sniffer.rb', line 212

def end_document()
  end_parent(top_level: true)
end

#end_mappingObject

End a Psych::Nodes::Mapping started with #start_mapping.

Pops off a parent from #parents and sets #parent to the last one. #level and #position are reset according to the last parent.

A Psychgus::Styler should probably never call this.

See Also:

  • #end_parent

Since:

  • 1.0.0


224
225
226
# File 'lib/psychgus/super_sniffer.rb', line 224

def end_mapping()
  end_parent(mapping_value: true)
end

#end_sequenceObject

End a Psych::Nodes::Sequence started with #start_sequence.

Pops off a parent from #parents and sets #parent to the last one. #level and #position are reset according to the last parent.

A Psychgus::Styler should probably never call this.

See Also:

  • #end_parent

Since:

  • 1.0.0


236
237
238
# File 'lib/psychgus/super_sniffer.rb', line 236

def end_sequence()
  end_parent(mapping_value: true)
end

#end_streamObject

End a Psych::Nodes::Stream started with #start_stream.

Pops off a parent from #parents and sets #parent to the last one. #level and #position are reset according to the last parent.

A Psychgus::Styler should probably never call this.

Since:

  • 1.0.0


246
247
248
# File 'lib/psychgus/super_sniffer.rb', line 246

def end_stream()
  end_parent(top_level: true)
end

#start_document(node) ⇒ Object

Start a Psych::Nodes::Document.

Creates a Parent, sets #parent to it, and adds it to #parents. #level and #position are incremented/set accordingly.

A Psychgus::Styler should probably never call this.

Parameters:

  • node (Psych::Nodes::Document)

    the Document to start

See Also:

  • #start_parent

Since:

  • 1.0.0


260
261
262
263
# File 'lib/psychgus/super_sniffer.rb', line 260

def start_document(node)
  start_parent(node,debug_tag: :doc,top_level: true)
  @documents.push(node)
end

#start_mapping(node) ⇒ Object

Start a Psych::Nodes::Mapping.

Creates a Parent, sets #parent to it, and adds it to #parents. #level and #position are incremented/set accordingly.

A Psychgus::Styler should probably never call this.

Parameters:

  • node (Psych::Nodes::Mapping)

    the Mapping to start

See Also:

  • #start_parent

Since:

  • 1.0.0


275
276
277
278
# File 'lib/psychgus/super_sniffer.rb', line 275

def start_mapping(node)
  start_parent(node,debug_tag: :map,child_type: :key)
  @mappings.push(node)
end

#start_sequence(node) ⇒ Object

Start a Psych::Nodes::Sequence.

Creates a Parent, sets #parent to it, and adds it to #parents. #level and #position are incremented/set accordingly.

A Psychgus::Styler should probably never call this.

Parameters:

  • node (Psych::Nodes::Sequence)

    the Sequence to start

See Also:

  • #start_parent

Since:

  • 1.0.0


290
291
292
293
# File 'lib/psychgus/super_sniffer.rb', line 290

def start_sequence(node)
  start_parent(node,debug_tag: :seq)
  @sequences.push(node)
end

#start_stream(node) ⇒ Object

Start a Psych::Nodes::Stream.

Creates a Parent, sets #parent to it, and adds it to #parents. #level and #position are incremented/set accordingly.

A Psychgus::Styler should probably never call this.

Parameters:

  • node (Psych::Nodes::Stream)

    the Stream to start

See Also:

  • #start_parent

Since:

  • 1.0.0


305
306
307
308
# File 'lib/psychgus/super_sniffer.rb', line 305

def start_stream(node)
  start_parent(node,debug_tag: :stream,top_level: true)
  @streams.push(node)
end