Class: Psychgus::StyledTreeBuilder

Inherits:
Psych::TreeBuilder
  • Object
show all
Defined in:
lib/psychgus/styled_tree_builder.rb

Overview

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

See Also:

Author:

  • Jonathan Bradley Whited (@esotericpig)

Since:

  • 1.0.0

Direct Known Subclasses

StyledDocumentStream

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*stylers, deref_aliases: false, **options) ⇒ StyledTreeBuilder

Initialize this class with Psychgus::Styler(s).

Parameters:

  • stylers (Styler)

    Psychgus::Styler(s) to use for styling this TreeBuilder

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

    whether to dereference aliases; output the actual value instead of the alias

Since:

  • 1.0.0



55
56
57
58
59
60
61
62
63
# File 'lib/psychgus/styled_tree_builder.rb', line 55

def initialize(*stylers,deref_aliases: false,**options)
  super()
  
  @deref_aliases = deref_aliases
  @sniffer = SuperSniffer.new()
  @stylers = []
  
  add_styler(*stylers)
end

Instance Attribute Details

#deref_aliasestrue, false Also known as: deref_aliases?

Returns whether to dereference aliases; output the actual value instead of the alias.

Returns:

  • (true, false)

    whether to dereference aliases; output the actual value instead of the alias

Since:

  • 1.0.0



41
42
43
# File 'lib/psychgus/styled_tree_builder.rb', line 41

def deref_aliases
  @deref_aliases
end

#snifferSuperSniffer (readonly)

Returns the Psychgus::SuperSniffer being used to sniff the YAML nodes, level, etc.

Returns:

Since:

  • 1.0.0



45
46
47
# File 'lib/psychgus/styled_tree_builder.rb', line 45

def sniffer
  @sniffer
end

#stylersArray<Stylers> (readonly)

Returns the Psychgus::Styler(s) being used to style the YAML nodes.

Returns:

Since:

  • 1.0.0



48
49
50
# File 'lib/psychgus/styled_tree_builder.rb', line 48

def stylers
  @stylers
end

Instance Method Details

#add_styler(*stylers) ⇒ self

Add Psychgus::Styler(s) onto the end of the data structure.

Parameters:

Returns:

  • (self)

    this class

Since:

  • 1.0.0



70
71
72
73
74
# File 'lib/psychgus/styled_tree_builder.rb', line 70

def add_styler(*stylers)
  @stylers.push(*stylers)
  
  return self
end

#aliasPsych::Nodes::Alias

Calls super, styler(s), and sniffer.

Returns:

  • (Psych::Nodes::Alias)

    the alias node created

See Also:

Since:

  • 1.0.0



84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/psychgus/styled_tree_builder.rb', line 84

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_documentObject

Calls super and sniffer.

See Also:

Since:

  • 1.0.0



101
102
103
104
105
106
107
# File 'lib/psychgus/styled_tree_builder.rb', line 101

def end_document(*)
  result = super
  
  @sniffer.end_document()
  
  return result
end

#end_mappingObject

Calls super and sniffer.

See Also:

Since:

  • 1.0.0



113
114
115
116
117
118
119
# File 'lib/psychgus/styled_tree_builder.rb', line 113

def end_mapping(*)
  result = super
  
  @sniffer.end_mapping()
  
  return result
end

#end_sequenceObject

Calls super and sniffer.

See Also:

Since:

  • 1.0.0



125
126
127
128
129
130
131
# File 'lib/psychgus/styled_tree_builder.rb', line 125

def end_sequence(*)
  result = super
  
  @sniffer.end_sequence()
  
  return result
end

#end_streamObject

Calls super and sniffer.

See Also:

Since:

  • 1.0.0



137
138
139
140
141
142
143
# File 'lib/psychgus/styled_tree_builder.rb', line 137

def end_stream(*)
  result = super
  
  @sniffer.end_stream()
  
  return result
end

#insert_styler(index, *stylers) ⇒ self

Insert Psychgus::Styler(s) at index into the data structure.

Parameters:

Returns:

  • (self)

    this class

Since:

  • 1.0.0



150
151
152
153
154
# File 'lib/psychgus/styled_tree_builder.rb', line 150

def insert_styler(index,*stylers)
  @stylers.insert(index,*stylers)
  
  return self
end

#pop_styler(count = 1) ⇒ Styler, ...

Remove the last Psychgus::Styler(s) from the data structure.

Parameters:

  • count (Integer) (defaults to: 1)

    the optional amount of tail elements to pop

Returns:

Since:

  • 1.0.0



161
162
163
164
165
166
# File 'lib/psychgus/styled_tree_builder.rb', line 161

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) ⇒ Styler?

Remove the Psychgus::Styler that matches styler from the data structure.

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

Parameters:

  • styler (Styler)

    the Psychgus::Styler to find and remove

  • block (Proc)

    an optional block to call when styler is not found

Returns:

Since:

  • 1.0.0



176
177
178
# File 'lib/psychgus/styled_tree_builder.rb', line 176

def remove_styler(styler,&block)
  return @stylers.delete(styler,&block)
end

#remove_styler_at(index) ⇒ Styler?

Remove the Psychgus::Styler at index from the data structure.

Parameters:

Returns:

Since:

  • 1.0.0



185
186
187
# File 'lib/psychgus/styled_tree_builder.rb', line 185

def remove_styler_at(index)
  return @stylers.delete_at(index)
end

#scalarPsych::Nodes::Scalar

Calls super, styler(s), and sniffer.

Returns:

  • (Psych::Nodes::Scalar)

    the scalar node created

See Also:

Since:

  • 1.0.0



197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/psychgus/styled_tree_builder.rb', line 197

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_documentPsych::Nodes::Document

Calls super, styler(s), and sniffer.

Returns:

  • (Psych::Nodes::Document)

    the document node created

See Also:

Since:

  • 1.0.0



218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/psychgus/styled_tree_builder.rb', line 218

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_mappingPsych::Nodes::Mapping

Calls super, styler(s), and sniffer.

Returns:

  • (Psych::Nodes::Mapping)

    the mapping node created

See Also:

Since:

  • 1.0.0



239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/psychgus/styled_tree_builder.rb', line 239

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_sequencePsych::Nodes::Sequence

Calls super, styler(s), and sniffer.

Returns:

  • (Psych::Nodes::Sequence)

    the sequence node created

See Also:

Since:

  • 1.0.0



260
261
262
263
264
265
266
267
268
269
270
271
# File 'lib/psychgus/styled_tree_builder.rb', line 260

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_streamPsych::Nodes::Stream

Calls super, styler(s), and sniffer.

Returns:

  • (Psych::Nodes::Stream)

    the stream node created

See Also:

Since:

  • 1.0.0



281
282
283
284
285
286
287
288
289
290
291
292
# File 'lib/psychgus/styled_tree_builder.rb', line 281

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