class Psychgus::StyledTreeBuilder

Use this wherever Psych::TreeBuilder would have been used, to enable styling.

@author Jonathan Bradley Whited @since 1.0.0

@see Psychgus.parser Psychgus.parser @see Psychgus.dump_stream Psychgus.dump_stream @see Psych::TreeBuilder

Attributes

deref_aliases[RW]

@return [true,false] whether to dereference aliases; output the actual value instead of the alias

deref_aliases?[RW]

@return [true,false] whether to dereference aliases; output the actual value instead of the alias

sniffer[R]

@return [SuperSniffer] the {SuperSniffer} being used to sniff the YAML nodes, level, etc.

stylers[R]

@return [Array<Stylers>] the {Styler}(s) being used to style the YAML nodes

Public Class Methods

new(*stylers,deref_aliases: false,**options) click to toggle source

Initialize this class with {Styler}(s).

@param stylers [Styler] {Styler}(s) to use for styling this TreeBuilder @param deref_aliases [true,false] whether to dereference aliases; output the actual value

instead of the alias
Calls superclass method
# File lib/psychgus/styled_tree_builder.rb, line 44
def initialize(*stylers,deref_aliases: false,**options)
  super()

  @deref_aliases = deref_aliases
  @sniffer = SuperSniffer.new
  @stylers = []

  add_styler(*stylers)
end

Public Instance Methods

add_styler(*stylers) click to toggle source

Add {Styler}(s) onto the end of the data structure.

@param stylers [Styler] {Styler}(s) to add

@return [self] this class

# File lib/psychgus/styled_tree_builder.rb, line 59
def add_styler(*stylers)
  @stylers.push(*stylers)

  return self
end
alias(*) click to toggle source

Calls super, styler(s), and sniffer.

@see Psych::TreeBuilder#alias @see Styler#style @see Styler#style_alias @see SuperSniffer#add_alias

@return [Psych::Nodes::Alias] the alias node created

Calls superclass method
# File lib/psychgus/styled_tree_builder.rb, line 73
def alias(*)
  node = super

  @stylers.each do |styler|
    styler.style(sniffer,node)
    styler.style_alias(sniffer,node)
  end

  @sniffer.add_alias(node)

  return node
end
end_document(*) click to toggle source

Calls super and sniffer.

@see Psych::TreeBuilder#end_document @see SuperSniffer#end_document

Calls superclass method
# File lib/psychgus/styled_tree_builder.rb, line 90
def end_document(*)
  result = super

  @sniffer.end_document

  return result
end
end_mapping(*) click to toggle source

Calls super and sniffer.

@see Psych::TreeBuilder#end_mapping @see SuperSniffer#end_mapping

Calls superclass method
# File lib/psychgus/styled_tree_builder.rb, line 102
def end_mapping(*)
  result = super

  @sniffer.end_mapping

  return result
end
end_sequence(*) click to toggle source

Calls super and sniffer.

@see Psych::TreeBuilder#end_sequence @see SuperSniffer#end_sequence

Calls superclass method
# File lib/psychgus/styled_tree_builder.rb, line 114
def end_sequence(*)
  result = super

  @sniffer.end_sequence

  return result
end
end_stream(*) click to toggle source

Calls super and sniffer.

@see Psych::TreeBuilder#end_stream @see SuperSniffer#end_stream

Calls superclass method
# File lib/psychgus/styled_tree_builder.rb, line 126
def end_stream(*)
  result = super

  @sniffer.end_stream

  return result
end
insert_styler(index,*stylers) click to toggle source

Insert {Styler}(s) at index into the data structure.

@param stylers [Styler] {Styler}(s) to insert

@return [self] this class

# File lib/psychgus/styled_tree_builder.rb, line 139
def insert_styler(index,*stylers)
  @stylers.insert(index,*stylers)

  return self
end
pop_styler(count=1) click to toggle source

Remove the last {Styler}(s) from the data structure.

@param count [Integer] the optional amount of tail elements to pop

@return [Styler,Array<Styler>,nil] the last {Styler}(s), or if empty or count==0, nil

# File lib/psychgus/styled_tree_builder.rb, line 150
def pop_styler(count=1)
  return nil if count == 0
  return @stylers.pop if count == 1

  return @stylers.pop(count)
end
remove_styler(styler,&block) click to toggle source

Remove the {Styler} that matches styler from the data structure.

An optional block can return a default value if not found.

@param styler [Styler] the {Styler} to find and remove @param block [Proc] an optional block to call when styler is not found

@return [Styler,nil] the last {Styler}, or if not found, nil or the result of block

# File lib/psychgus/styled_tree_builder.rb, line 165
def remove_styler(styler,&block)
  return @stylers.delete(styler,&block)
end
remove_styler_at(index) click to toggle source

Remove the {Styler} at index from the data structure.

@param index [Integer] the index of the {Styler} to remove

@return [Styler,nil] the {Styler} removed or nil if empty

# File lib/psychgus/styled_tree_builder.rb, line 174
def remove_styler_at(index)
  return @stylers.delete_at(index)
end
scalar(*) click to toggle source

Calls super, styler(s), and sniffer.

@see Psych::TreeBuilder#scalar @see Styler#style @see Styler#style_scalar @see SuperSniffer#add_scalar

@return [Psych::Nodes::Scalar] the scalar node created

Calls superclass method
# File lib/psychgus/styled_tree_builder.rb, line 186
def scalar(*)
  node = super

  @stylers.each do |styler|
    styler.style(sniffer,node)
    styler.style_scalar(sniffer,node)
  end

  @sniffer.add_scalar(node)

  return node
end
start_document(*) click to toggle source

Calls super, styler(s), and sniffer.

@see Psych::TreeBuilder#start_document @see Styler#style @see Styler#style_document @see SuperSniffer#start_document

@return [Psych::Nodes::Document] the document node created

Calls superclass method
# File lib/psychgus/styled_tree_builder.rb, line 207
def start_document(*)
  node = super

  @stylers.each do |styler|
    styler.style(sniffer,node)
    styler.style_document(sniffer,node)
  end

  @sniffer.start_document(node)

  return node
end
start_mapping(*) click to toggle source

Calls super, styler(s), and sniffer.

@see Psych::TreeBuilder#start_mapping @see Styler#style @see Styler#style_mapping @see SuperSniffer#start_mapping

@return [Psych::Nodes::Mapping] the mapping node created

Calls superclass method
# File lib/psychgus/styled_tree_builder.rb, line 228
def start_mapping(*)
  node = super

  @stylers.each do |styler|
    styler.style(sniffer,node)
    styler.style_mapping(sniffer,node)
  end

  @sniffer.start_mapping(node)

  return node
end
start_sequence(*) click to toggle source

Calls super, styler(s), and sniffer.

@see Psych::TreeBuilder#start_sequence @see Styler#style @see Styler#style_sequence @see SuperSniffer#start_sequence

@return [Psych::Nodes::Sequence] the sequence node created

Calls superclass method
# File lib/psychgus/styled_tree_builder.rb, line 249
def start_sequence(*)
  node = super

  @stylers.each do |styler|
    styler.style(sniffer,node)
    styler.style_sequence(sniffer,node)
  end

  @sniffer.start_sequence(node)

  return node
end
start_stream(*) click to toggle source

Calls super, styler(s), and sniffer.

@see Psych::TreeBuilder#start_stream @see Styler#style @see Styler#style_stream @see SuperSniffer#start_stream

@return [Psych::Nodes::Stream] the stream node created

Calls superclass method
# File lib/psychgus/styled_tree_builder.rb, line 270
def start_stream(*)
  node = super

  @stylers.each do |styler|
    styler.style(sniffer,node)
    styler.style_stream(sniffer,node)
  end

  @sniffer.start_stream(node)

  return node
end