Module: Mustermann::Visualizer::PatternExtension

Defined in:
lib/mustermann/visualizer/pattern_extension.rb

Overview

Mixin that will be added to Pattern.

Instance Method Summary collapse

Instance Method Details

#color_inspect(base_color = nil, **theme) ⇒ String

Returns ANSI colorized version of Pattern#inspect.

Returns:

  • (String)

    ANSI colorized version of Pattern#inspect



53
54
55
56
57
# File 'lib/mustermann/visualizer/pattern_extension.rb', line 53

def color_inspect(base_color = nil, **theme)
  base_color ||= Highlight::DEFAULT_THEME[:base01]
  template = is_a?(Composite) ? "*#<%p:(*%s*)>*" : "*#<%p:*%s*>*"
  Hansi.render(template, self.class, to_ansi(inspect: true, **theme), {"*" => base_color})
end

#inspectObject

If invoked directly by IRB, same as #color_inspect, otherwise same as Pattern#inspect.



48
49
50
# File 'lib/mustermann/visualizer/pattern_extension.rb', line 48

def inspect
  caller_locations.first.base_label == '<module:IRB>' ? color_inspect : super
end

#pretty_print(q) ⇒ Object

If invoked directly by IRB, same as #color_inspect, otherwise same as Object#pretty_print.



60
61
62
63
64
65
66
# File 'lib/mustermann/visualizer/pattern_extension.rb', line 60

def pretty_print(q)
  if q.class.name.to_s[/[^:]+$/] == "ColorPrinter"
    q.text(color_inspect, inspect.length)
  else
    super
  end
end

#to_ansi(inspect: nil, **theme) ⇒ String

Returns ANSI colorized version of the pattern.

Examples:

puts Mustermann.new("/:page").to_ansi

Returns:

  • (String)

    ANSI colorized version of the pattern.



13
14
15
# File 'lib/mustermann/visualizer/pattern_extension.rb', line 13

def to_ansi(inspect: nil, **theme)
  Visualizer.highlight(self, **theme).to_ansi(inspect: inspect)
end

#to_html(inspect: nil, tag: :span, class_prefix: "mustermann_", css: :inline, **theme) ⇒ String

Returns HTML version of the pattern.

Examples:

puts Mustermann.new("/:page").to_html

Returns:

  • (String)

    HTML version of the pattern.



21
22
23
# File 'lib/mustermann/visualizer/pattern_extension.rb', line 21

def to_html(inspect: nil, tag: :span, class_prefix: "mustermann_", css: :inline, **theme)
  Visualizer.highlight(self, **theme).to_html(inspect: inspect, tag: tag, class_prefix: class_prefix, css: css)
end

#to_sString

If invoked directly by puts: ANSI colorized version of the pattern. If invoked by anything else: String version of the pattern.

Examples:

require 'mustermann/visualizer'
pattern = Mustermann.new('/:page')
puts pattern        # will have color
puts pattern.to_s   # will not have color

Returns:

  • (String)

    non-colorized or colorized version of the pattern



43
44
45
# File 'lib/mustermann/visualizer/pattern_extension.rb', line 43

def to_s
  caller_locations.first.label == 'puts' ? to_ansi : super
end

#to_treeString

Returns tree version of the pattern.

Examples:

puts Mustermann.new("/:page").to_tree

Returns:

  • (String)

    tree version of the pattern.



29
30
31
# File 'lib/mustermann/visualizer/pattern_extension.rb', line 29

def to_tree
  Visualizer.tree(self).to_s
end