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



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

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

#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