Module: Chromate::Formattable

Defined in:
lib/chromate/formattable.rb

Overview

The ‘Formattable` module allows any class easy access to Chromate formatting, so long as it provides an `effects` method, which is expected to return an `EffectSet`.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.format_attr(name, question_name = name.to_s + '?') ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Introduce a format attribute. Each format attribute comes with two methods, which generally differ only by addition of a ‘?’ character ( although this behavior can be overriden). The first method sets the format attribute with the given name, and the second queries whether it is set.

Parameters:

  • name (Symbol)

    the name of an attribute

  • question_name (Symbol) (defaults to: name.to_s + '?')

    the name of the method to query whether the attribute is set



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/chromate/formattable.rb', line 20

def self.format_attr(name, question_name = name.to_s + '?')
  if EFFECTS.include?(name)
    module_eval(<<-EOF
      define_method :#{name} do
        effects << Effect[:#{name}]
        self
      end

      define_method :#{question_name} do
        effects.include?(Effect[:#{name}])
      end
    EOF
    )
  else
    raise ArgumentError, "no such effect: #{name}"
  end
end

Instance Method Details

#bg(what) ⇒ Formattable

Set the background and return self.

Parameters:

  • what (Object)

    an object describing the background color (see Color::[])

Returns:



59
60
61
62
# File 'lib/chromate/formattable.rb', line 59

def bg(what)
  effects << Color[what].as_bg
  self
end

#fg(what) ⇒ Formattable

Set the foreground and return self.

Parameters:

  • what (Object)

    an object describing the foreground color (see Color::[])

Returns:



48
49
50
51
# File 'lib/chromate/formattable.rb', line 48

def fg(what)
  effects << Color[what]
  self
end

#to_sString

Returns:



65
66
67
# File 'lib/chromate/formattable.rb', line 65

def to_s
  effects + self + OFF
end