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

  • #documents # Array of Psych::Nodes::Document processed so far

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

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

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

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

  • #streams # Array of Psych::Nodes::Stream processed so far

#parent is the current Parent of the node being processed, which is an empty Parent for the first node (not nil).

#parents are all of the (grand)Parent(s) for the current node, which is an Array that just contains an empty Parent 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 - <root:(0:0)>
(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



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/psychgus/super_sniffer.rb', line 170

def initialize()
  @aliases = []
  @documents = []
  @level = 0
  @mappings = []
  @nodes = []
  @parent = nil
  @parents = []
  @position = 0
  @scalars = []
  @sequences = []
  @streams = []
  
  # Do not pass in "top_level: true"
  start_parent(nil,debug_tag: :root)
end

Instance Attribute Details

#aliasesArray<Psych::Nodes::Alias> (readonly)

Returns the aliases processed so far.

Returns:

  • (Array<Psych::Nodes::Alias>)

    the aliases processed so far

Since:

  • 1.0.0



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

def aliases
  @aliases
end

#documentsArray<Psych::Nodes::Document> (readonly)

Returns the documents processed so far.

Returns:

  • (Array<Psych::Nodes::Document>)

    the documents processed so far

Since:

  • 1.0.0



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

def documents
  @documents
end

#levelInteger (readonly)

Returns the current level.

Returns:

  • (Integer)

    the current level

Since:

  • 1.0.0



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

def level
  @level
end

#mappingsArray<Psych::Nodes::Mapping> (readonly)

Returns the mappings processed so far.

Returns:

  • (Array<Psych::Nodes::Mapping>)

    the mappings processed so far

Since:

  • 1.0.0



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

def mappings
  @mappings
end

#nodesArray<Psych::Nodes::Node> (readonly)

Returns the nodes processed so far.

Returns:

  • (Array<Psych::Nodes::Node>)

    the nodes processed so far

Since:

  • 1.0.0



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

def nodes
  @nodes
end

#parentParent (readonly)

Returns the current parent.

Returns:

  • (Parent)

    the current parent

Since:

  • 1.0.0



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

def parent
  @parent
end

#parentsArray<Parent> (readonly)

Returns the current (grand)parents.

Returns:

  • (Array<Parent>)

    the current (grand)parents

Since:

  • 1.0.0



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

def parents
  @parents
end

#positionInteger (readonly)

Returns the current position.

Returns:

  • (Integer)

    the current position

Since:

  • 1.0.0



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

def position
  @position
end

#scalarsArray<Psych::Nodes::Scalar> (readonly)

Returns the scalars processed so far.

Returns:

  • (Array<Psych::Nodes::Scalar>)

    the scalars processed so far

Since:

  • 1.0.0



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

def scalars
  @scalars
end

#sequencesArray<Psych::Nodes::Sequence> (readonly)

Returns the sequences processed so far.

Returns:

  • (Array<Psych::Nodes::Sequence>)

    the sequences processed so far

Since:

  • 1.0.0



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

def sequences
  @sequences
end

#streamsArray<Psych::Nodes::Stream> (readonly)

Returns the streams processed so far.

Returns:

  • (Array<Psych::Nodes::Stream>)

    the streams processed so far

Since:

  • 1.0.0



167
168
169
# File 'lib/psychgus/super_sniffer.rb', line 167

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:

Since:

  • 1.0.0



194
195
196
197
# File 'lib/psychgus/super_sniffer.rb', line 194

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

#add_child(node) ⇒ Object (protected)

Add a non-parent node.

This will increment #position accordingly, and if the child is a Key to a Mapping, create a fake “Parent”.

Parameters:

  • node (Psych::Nodes::Node)

    the non-parent Node to add

See Also:

Since:

  • 1.0.0



325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
# File 'lib/psychgus/super_sniffer.rb', line 325

def add_child(node)
  if !@parent.nil?()    # Fake a "parent" if necessary

    case @parent.child_type
    when :key
      start_mapping_key(node)
      return
    when :value
      end_mapping_value()
      return
    else
      @parent.child_position += 1
    end
  end
  
  @position += 1
  
  @nodes.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:

Since:

  • 1.0.0



206
207
208
209
# File 'lib/psychgus/super_sniffer.rb', line 206

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



217
218
219
# File 'lib/psychgus/super_sniffer.rb', line 217

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:

Since:

  • 1.0.0



229
230
231
# File 'lib/psychgus/super_sniffer.rb', line 229

def end_mapping()
  end_parent(mapping_value: true)
end

#end_mapping_valueObject (protected)

End a fake “Parent” that is a Key/Value to a Mapping.

See Also:

Since:

  • 1.0.0



348
349
350
351
352
# File 'lib/psychgus/super_sniffer.rb', line 348

def end_mapping_value()
  end_parent() # Do not pass in "mapping_value: true" and/or "top_level: true"
  
  @parent.child_type = :key unless @parent.nil?()
end

#end_parent(mapping_value: false, top_level: false) ⇒ Object (protected)

End a Parent.

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

Parameters:

  • mapping_value (true, false) (defaults to: false)

    true if parent can be the value of a Mapping's key

  • top_level (true, false) (defaults to: false)

    true if a top-level parent (i.e., encapsulating the main data)

Since:

  • 1.0.0



361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
# File 'lib/psychgus/super_sniffer.rb', line 361

def end_parent(mapping_value: false,top_level: false)
  @parents.pop()
  @parent = @parents.last
  
  @level = top_level ? 1 : (@level - 1)
  
  if !@parent.nil?()
    @parent.child_position += 1
    @position = @parent.child_position
    
    # add_child() will not be called again, so end a fake "parent" manually with a fake "value"
    # - This is necessary for any parents that can be the value of a map's key (e.g., Sequence)
    end_mapping_value() if mapping_value && !@parent.child_type.nil?()
  end
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:

Since:

  • 1.0.0



241
242
243
# File 'lib/psychgus/super_sniffer.rb', line 241

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



251
252
253
# File 'lib/psychgus/super_sniffer.rb', line 251

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:

Since:

  • 1.0.0



265
266
267
268
# File 'lib/psychgus/super_sniffer.rb', line 265

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:

Since:

  • 1.0.0



280
281
282
283
# File 'lib/psychgus/super_sniffer.rb', line 280

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

#start_mapping_key(node) ⇒ Object (protected)

Start a fake “Parent” that is a Key/Value to a Mapping.

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

Parameters:

  • node (Psych::Nodes::Node)

    the Node to start

See Also:

Since:

  • 1.0.0



385
386
387
388
389
390
391
392
393
394
395
396
397
398
# File 'lib/psychgus/super_sniffer.rb', line 385

def start_mapping_key(node)
  debug_tag = nil
  
  # Value must be first because Scalar also has an anchor
  if node.respond_to?(:value)
    debug_tag = node.value
  elsif node.respond_to?(:anchor)
    debug_tag = node.anchor
  end
  
  debug_tag = :noface if debug_tag.nil?()
  
  start_parent(node,debug_tag: debug_tag,child_type: :value)
end

#start_parent(node, top_level: false, **extra) ⇒ Object (protected)

Start a Parent.

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

Parameters:

  • node (Psych::Nodes::Node)

    the parent Node to start

  • top_level (true, false) (defaults to: false)

    true if a top-level parent (i.e., encapsulating the main data)

  • extra (Hash)

    the extra keyword args to pass to Psychgus::SuperSniffer::Parent#initialize

See Also:

Since:

  • 1.0.0



410
411
412
413
414
415
416
417
418
419
420
421
422
423
# File 'lib/psychgus/super_sniffer.rb', line 410

def start_parent(node,top_level: false,**extra)
  @parent = Parent.new(self,node,**extra)
  
  @parents.push(@parent)
  @nodes.push(node) unless node.nil?()
  
  if top_level
    @level = 1
    @position = @parent.position
  else
    @level += 1
    @position = 1
  end
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:

Since:

  • 1.0.0



295
296
297
298
# File 'lib/psychgus/super_sniffer.rb', line 295

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:

Since:

  • 1.0.0



310
311
312
313
# File 'lib/psychgus/super_sniffer.rb', line 310

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