Class: Yummi::Colorizers::PatternColorizer

Inherits:
Object
  • Object
show all
Includes:
Yummi::Colorizer
Defined in:
lib/yummi/colorizers.rb

Overview

A colorizer for strings that follows a pattern. This colorizer is useful for log files.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Yummi::Colorizer

#color_for

Constructor Details

#initialize(mappings = nil) ⇒ PatternColorizer

Returns a new instance of PatternColorizer.



237
238
239
240
# File 'lib/yummi/colorizers.rb', line 237

def initialize(mappings = nil)
  @patterns = []
  map mappings if mappings
end

Instance Attribute Details

#level=(value) ⇒ Object (writeonly)

Sets the attribute level

Parameters:

  • value

    the value to set the attribute level to.



235
236
237
# File 'lib/yummi/colorizers.rb', line 235

def level=(value)
  @level = value
end

Instance Method Details

#call(ctx) ⇒ Object Also known as: colorize



274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/yummi/colorizers.rb', line 274

def call(ctx)
  ctx = Yummi::Context::new(ctx) unless ctx.is_a? Context
  text = ctx.value.to_s
  @patterns.each do |config|
    config[:patterns].each_key do |regex|
      if regex.match(text)
        return match(text, config)
      end
    end
  end
  return text.colorize(@last_color) if @last_color
  text
end

#map(params) ⇒ Object

Maps a set of patterns to colors.

Args

A hash containing the following keys:

* prefix: a pattern prefix
* suffix: a pattern suffix
* options: option flags to use
* mode: the mode to use:
    - all  : colorize the entire text
    - grep : colorize only the matched text
* patterns: a pattern => color hash

If a string is passed instead of a hash, a file containing a YAML configuration will be loaded. If the string doesn’t represent a file, the following patterns will be used to find it:

* $HOME/.yummi/patterns/PATTERN.yaml
* $YUMMI_GEM/yummy/patterns/PATTERN.yaml


264
265
266
267
268
269
270
271
272
# File 'lib/yummi/colorizers.rb', line 264

def map(params)
  if params.is_a? Array
    params.each { |p| map p }
  elsif params.is_a? String or params.is_a? Symbol
    map Yummi::Helpers::load_resource(params, :from => :patterns)
  else
    config params
  end
end