module HighLine::StringExtensions

HighLine extensions for String class. Included by HighLine::String.

Constants

STYLE_METHOD_NAME_PATTERN

Public Class Methods

define_builtin_style_methods(base) click to toggle source

At include time, it defines all basic builtin styles. @param base [Class, Module] base Class/Module

# File lib/highline/string_extensions.rb, line 95
def self.define_builtin_style_methods(base)
  HighLine::COLORS.each do |color|
    color = color.downcase
    base.class_eval <<-METHOD_DEFINITION
      undef :#{color} if method_defined? :#{color}
      def #{color}
        color(:#{color})
      end
    METHOD_DEFINITION

    base.class_eval <<-METHOD_DEFINITION
      undef :on_#{color} if method_defined? :on_#{color}
      def on_#{color}
        on(:#{color})
      end
    METHOD_DEFINITION

    HighLine::STYLES.each do |style|
      style = style.downcase
      base.class_eval <<-METHOD_DEFINITION
        undef :#{style} if method_defined? :#{style}
        def #{style}
          color(:#{style})
        end
      METHOD_DEFINITION
    end
  end
end
define_style_support_methods(base) click to toggle source

At include time, it defines all basic style support method like color, on, uncolor, rgb, on_rgb and the method_missing callback @return [void] @param base [Class, Module] the base class/module

# File lib/highline/string_extensions.rb, line 28
def self.define_style_support_methods(base)
  base.class_eval do
    undef :color if method_defined? :color
    undef :foreground if method_defined? :foreground
    def color(*args)
      self.class.new(HighLine.color(self, *args))
    end
    alias_method :foreground, :color

    undef :on if method_defined? :on
    def on(arg)
      color(("on_" + arg.to_s).to_sym)
    end

    undef :uncolor if method_defined? :uncolor
    def uncolor
      self.class.new(HighLine.uncolor(self))
    end

    undef :rgb if method_defined? :rgb
    def rgb(*colors)
      color_code = setup_color_code(*colors)
      color("rgb_#{color_code}".to_sym)
    end

    undef :on_rgb if method_defined? :on_rgb
    def on_rgb(*colors)
      color_code = setup_color_code(*colors)
      color("on_rgb_#{color_code}".to_sym)
    end

    # @todo Chain existing method_missing?
    undef :method_missing if method_defined? :method_missing
    def method_missing(method, *_args)
      if method.to_s =~ STYLE_METHOD_NAME_PATTERN
        color(method)
      else
        super
      end
    end

    undef :respond_to_missing if method_defined? :respond_to_missing
    def respond_to_missing?(method_name, include_private = false)
      method_name.to_s =~ STYLE_METHOD_NAME_PATTERN || super
    end

    private

    def setup_color_code(*colors)
      color_code = colors.map do |color|
        if color.is_a?(Numeric)
          format("%02x", color)
        else
          color.to_s
        end
      end.join

      raise "Bad RGB color #{colors.inspect}" unless
        color_code =~ /^[a-fA-F0-9]{6}/

      color_code
    end
  end
end
included(base) click to toggle source

Included hook. Actions to take when being included. @param base [Class, Module] base class

# File lib/highline/string_extensions.rb, line 17
def self.included(base)
  define_builtin_style_methods(base)
  define_style_support_methods(base)
end

Public Instance Methods

color(*args) click to toggle source
# File lib/highline/string_extensions.rb, line 32
def color(*args)
  self.class.new(HighLine.color(self, *args))
end
method_missing(method, *_args) click to toggle source
Calls superclass method
# File lib/highline/string_extensions.rb, line 61
def method_missing(method, *_args)
  if method.to_s =~ STYLE_METHOD_NAME_PATTERN
    color(method)
  else
    super
  end
end
on(arg) click to toggle source
# File lib/highline/string_extensions.rb, line 38
def on(arg)
  color(("on_" + arg.to_s).to_sym)
end
on_rgb(*colors) click to toggle source
# File lib/highline/string_extensions.rb, line 54
def on_rgb(*colors)
  color_code = setup_color_code(*colors)
  color("on_rgb_#{color_code}".to_sym)
end
respond_to_missing?(method_name, include_private = false) click to toggle source
Calls superclass method
# File lib/highline/string_extensions.rb, line 70
def respond_to_missing?(method_name, include_private = false)
  method_name.to_s =~ STYLE_METHOD_NAME_PATTERN || super
end
rgb(*colors) click to toggle source
# File lib/highline/string_extensions.rb, line 48
def rgb(*colors)
  color_code = setup_color_code(*colors)
  color("rgb_#{color_code}".to_sym)
end
setup_color_code(*colors) click to toggle source
# File lib/highline/string_extensions.rb, line 76
def setup_color_code(*colors)
  color_code = colors.map do |color|
    if color.is_a?(Numeric)
      format("%02x", color)
    else
      color.to_s
    end
  end.join

  raise "Bad RGB color #{colors.inspect}" unless
    color_code =~ /^[a-fA-F0-9]{6}/

  color_code
end
uncolor() click to toggle source
# File lib/highline/string_extensions.rb, line 43
def uncolor
  self.class.new(HighLine.uncolor(self))
end