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



51
52
53
54
# File 'lib/mustermann/visualizer/pattern_extension.rb', line 51

def color_inspect(base_color = nil, **theme)
  base_color ||= Highlight::DEFAULT_THEME[:base01]
  Hansi.render("*#<%p:*%s*>*", 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.



46
47
48
# File 'lib/mustermann/visualizer/pattern_extension.rb', line 46

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.



57
58
59
60
61
62
63
# File 'lib/mustermann/visualizer/pattern_extension.rb', line 57

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

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

Returns ANSI colorized version of the pattern.

Examples:

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

Returns:

  • (String)

    ANSI colorized version of the pattern.



11
12
13
# File 'lib/mustermann/visualizer/pattern_extension.rb', line 11

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

#to_html(inspect: false, 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.



19
20
21
# File 'lib/mustermann/visualizer/pattern_extension.rb', line 19

def to_html(inspect: false, 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



41
42
43
# File 'lib/mustermann/visualizer/pattern_extension.rb', line 41

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.



27
28
29
# File 'lib/mustermann/visualizer/pattern_extension.rb', line 27

def to_tree
  Visualizer.tree(self).to_s
end