Sha256: 6c55faf1c247d5aa26f2eeeac43049ffdaa5828e3b6aa907249e660551bbd6e7

Contents?: true

Size: 1.93 KB

Versions: 7

Compression:

Stored size: 1.93 KB

Contents

require 'whirled_peas/utils/ansi'

module WhirledPeas
  module Settings
    # An abstract class that encapsulates colors for a specific use case
    class Color
      # Validate the `color` argument is either (1) nil, (2) a valid Color constant in
      # this class or (3) a symbol that maps to valid Color constant. E.g. if there
      # is a RED constant in an implementing class, then :red or :bright_red are
      # valid values for `color`
      #
      # @param color [Color|Symbol]
      # @return [Color|Symbol] the value passed in if valid, otherwise an ArgumentError
      #   is raised.
      def self.validate!(color)
        return if color.nil?
        return color if color.is_a?(self)
        if color.is_a?(Symbol)
          match = color.to_s.match(/^(bright_)?(\w+)$/)
          color_class = self.const_get(match[2].upcase)
          if color_class.is_a?(self)
            if !match[1]
              return color_class
            elsif !color_class.bright?
              return color_class.bright
            end
          end
        end
        error_message = "Unsupported #{self.name.split('::').last}: #{color.inspect}"
        raise ArgumentError, error_message
      rescue NameError
        error_message = "Unsupported #{self.name.split('::').last}: #{color.inspect}"
        raise ArgumentError, error_message
      end

      def initialize(code, bright=false)
        @code = code
        @bright = bright
      end

      def bright?
        @bright
      end

      def bright
        bright? ? self : self.class.new(@code + Utils::Ansi::BRIGHT_OFFSET, true)
      end

      def hash
        [@code, @bright].hash
      end

      def ==(other)
        other.is_a?(self.class) && self.hash == other.hash
      end
      alias_method :eq?, :==

      def to_s
        @code.to_s
      end

      def inspect
        "#{self.class.name.split('::').last}(code=#{@code}, bright=#{@bright})"
      end
    end
    private_constant :Color
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
whirled_peas-0.11.0 lib/whirled_peas/settings/color.rb
whirled_peas-0.10.0 lib/whirled_peas/settings/color.rb
whirled_peas-0.9.1 lib/whirled_peas/settings/color.rb
whirled_peas-0.9.0 lib/whirled_peas/settings/color.rb
whirled_peas-0.8.0 lib/whirled_peas/settings/color.rb
whirled_peas-0.7.1 lib/whirled_peas/settings/color.rb
whirled_peas-0.7.0 lib/whirled_peas/settings/color.rb