Sha256: 32200e8ef3b081d3a099496c81ff0864cd4c1b80b3d9785370755cc1d1dba561

Contents?: true

Size: 1.72 KB

Versions: 29

Compression:

Stored size: 1.72 KB

Contents

# frozen_string_literal: true

module Vedeu

  module Colours

    # Validates the value as a valid colour string in HTML/CSS format,
    # e.g. '#123456'.
    #
    # @api private
    #
    class Validator

      include Vedeu::Common

      # @param value [String]
      # @return [Boolean]
      def self.valid?(value)
        new(value).valid?
      end

      # @param value [String]
      # @return [Vedeu::Colours::Validator]
      def initialize(value)
        @value = value
      end

      # Returns a boolean indicating whether the colour provided is a
      # valid named colour.
      #
      # @return [Boolean]
      def named?
        Vedeu.esc.valid_name?(value)
      end

      # Returns a boolean indicating whether the value is within the
      # range of valid terminal numbered colours.
      #
      # @return [Boolean]
      def numbered?
        numeric?(value) && value >= 0 && value <= 255
      end

      # Returns a boolean indicated whether the colour is a valid
      # HTML/CSS colour.
      #
      # @return [Boolean]
      def rgb?
        return true if value =~ /^#([A-Fa-f0-9]{6})$/

        false
      end

      # @return [Boolean]
      def valid?
        background? || colour? || foreground? || rgb? || named? || numbered?
      end

      protected

      # @!attribute [r] value
      # @return [String]
      attr_reader :value

      private

      # @return [Boolean]
      def background?
        value.is_a?(Vedeu::Colours::Background)
      end

      # @return [Boolean]
      def colour?
        value.is_a?(Vedeu::Colours::Colour)
      end

      # @return [Boolean]
      def foreground?
        value.is_a?(Vedeu::Colours::Foreground)
      end

    end # Validator

  end # Colours

end # Vedeu

Version data entries

29 entries across 29 versions & 1 rubygems

Version Path
vedeu-0.8.32 lib/vedeu/colours/validator.rb
vedeu-0.8.31 lib/vedeu/colours/validator.rb
vedeu-0.8.30 lib/vedeu/colours/validator.rb
vedeu-0.8.29 lib/vedeu/colours/validator.rb
vedeu-0.8.28 lib/vedeu/colours/validator.rb
vedeu-0.8.27 lib/vedeu/colours/validator.rb
vedeu-0.8.26 lib/vedeu/colours/validator.rb
vedeu-0.8.25 lib/vedeu/colours/validator.rb
vedeu-0.8.24 lib/vedeu/colours/validator.rb
vedeu-0.8.23 lib/vedeu/colours/validator.rb
vedeu-0.8.22 lib/vedeu/colours/validator.rb
vedeu-0.8.21 lib/vedeu/colours/validator.rb
vedeu-0.8.20 lib/vedeu/colours/validator.rb
vedeu-0.8.19 lib/vedeu/colours/validator.rb
vedeu-0.8.18 lib/vedeu/colours/validator.rb
vedeu-0.8.17 lib/vedeu/colours/validator.rb
vedeu-0.8.16 lib/vedeu/colours/validator.rb
vedeu-0.8.15 lib/vedeu/colours/validator.rb
vedeu-0.8.14 lib/vedeu/colours/validator.rb
vedeu-0.8.13 lib/vedeu/colours/validator.rb