Module: Psychgus::Stylables::HierarchyStylable

Includes:
Psychgus::Styler
Included in:
Psychgus::Stylers::HierarchyStyler
Defined in:
lib/psychgus/stylables.rb

Overview

A visual hierarchy writer of the levels.

This is useful for determining the correct level/position when writing a Psychgus::Styler.

The default IO is StringIO, but can specify a different one.

See Psychgus.hierarchy for more details.

See Also:

Since:

  • 1.2.0

Constant Summary

Constants included from Psychgus::Styler

Psychgus::Styler::EMPTY

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Psychgus::Styler

#style_alias, #style_document, #style_mapping, #style_scalar, #style_sequence, #style_stream

Instance Attribute Details

#ioIO

Returns the IO to write to; defaults to StringIO.

Returns:

  • (IO)

    the IO to write to; defaults to StringIO

Since:

  • 1.2.0



140
141
142
# File 'lib/psychgus/stylables.rb', line 140

def io
  @io
end

#verbosetrue, false

Returns whether to be more verbose (e.g., write child info).

Returns:

  • (true, false)

    whether to be more verbose (e.g., write child info)

Since:

  • 1.2.0



141
142
143
# File 'lib/psychgus/stylables.rb', line 141

def verbose
  @verbose
end

Instance Method Details

#initialize(io: StringIO.new(), verbose: false, **kargs) ⇒ Object

Parameters:

  • io (IO) (defaults to: StringIO.new())

    the IO to write to

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

    whether to be more verbose (e.g., write child info)

  • kargs (Hash)

    capture extra keyword args, so no error for undefined args

Since:

  • 1.2.0



146
147
148
149
# File 'lib/psychgus/stylables.rb', line 146

def initialize(io: StringIO.new(),verbose: false,**kargs)
  @io = io
  @verbose = verbose
end

#style(sniffer, node) ⇒ Object

Write the hierarchy of node to #io.

See Also:

Since:

  • 1.2.0



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/psychgus/stylables.rb', line 154

def style(sniffer,node)
  @io.print (' ' * (sniffer.level - 1))
  
  name = node.respond_to?(:value) ? node.value : node.class.name
  parent = sniffer.parent
  
  @io.print "(#{sniffer.level}:#{sniffer.position}):#{name} - "
  
  if @verbose
    @io.print parent
  else
    @io.print "<#{parent.debug_tag}:(#{parent.level}:#{parent.position})>"
  end
  
  @io.puts
end

#to_sString

Convert #io to a String if possible (e.g., StringIO).

Returns:

  • (String)

    the IO String result or just #io as a String

Since:

  • 1.2.0



174
175
176
# File 'lib/psychgus/stylables.rb', line 174

def to_s()
  return @io.respond_to?(:string) ? @io.string : @io.to_s()
end