Module: HighLine::BuiltinStyles::ClassMethods

Defined in:
lib/highline/builtin_styles.rb

Overview

BuiltinStyles class methods to be extended.

Constant Summary collapse

RGB_COLOR_PATTERN =

Regexp to match against RGB style constant names.

/^(ON_)?(RGB_)([A-F0-9]{6})(_STYLE)?$/

Instance Method Summary collapse

Instance Method Details

#const_missing(name) ⇒ Object

const_missing callback for automatically respond to builtin constants (without explicitly defining them)

Parameters:

  • name (Symbol)

    missing constant name

Raises:

  • (NameError)


103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/highline/builtin_styles.rb', line 103

def const_missing(name)
  raise NameError, "Bad color or uninitialized constant #{name}" unless
    name.to_s =~ RGB_COLOR_PATTERN

  on = Regexp.last_match(1)
  suffix = Regexp.last_match(4)

  code_name = if suffix
                Regexp.last_match(1).to_s +
                  Regexp.last_match(2) +
                  Regexp.last_match(3)
              else
                name.to_s
              end

  style_name = code_name + "_STYLE"
  style = Style.rgb(Regexp.last_match(3))
  style = style.on if on

  const_set(style_name, style)
  const_set(code_name, style.code)

  suffix ? style : style.code
end