Sha256: e37ce4fac61ba327299eb756608f44020e1374114842bc2290f029243f203c69

Contents?: true

Size: 1.55 KB

Versions: 8

Compression:

Stored size: 1.55 KB

Contents

# frozen_string_literal: true

module Vedeu

  module Coercers

    # Converts a given value into a {Vedeu::Colours::Colour} if
    # possible.
    #
    # @api private
    #
    class Colour

      include Vedeu::Common

      # @param (see #initialize)
      # @return (see #coerce)
      def self.coerce(value)
        new(value).coerce
      end

      # @param value [void]
      # @return [Vedeu::Coercers::Colour]
      def initialize(value)
        @value = value
      end

      # @raise [Vedeu::Error::Fatal] When the value cannot be coerced.
      # @return [void]
      def coerce
        if absent?(value)
          Vedeu::Colours::Colour.new

        elsif background?
          Vedeu::Colours::Colour.new(background: value)

        elsif colour?
          value

        elsif foreground?
          Vedeu::Colours::Colour.new(foreground: value)

        elsif hash?(value)
          attributes = Vedeu::Coercers::ColourAttributes.coerce(value)
          Vedeu::Colours::Colour.new(attributes)

        else
          fail Vedeu::Error::Fatal, 'Vedeu cannot coerce this colour.'

        end
      end

      protected

      # @!attribute [r] value
      # @return [void]
      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 # Colour

  end # Coercers

end # Vedeu

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
vedeu-0.8.8 lib/vedeu/coercers/colour.rb
vedeu-0.8.7 lib/vedeu/coercers/colour.rb
vedeu-0.8.6 lib/vedeu/coercers/colour.rb
vedeu-0.8.5 lib/vedeu/coercers/colour.rb
vedeu-0.8.4 lib/vedeu/coercers/colour.rb
vedeu-0.8.3 lib/vedeu/coercers/colour.rb
vedeu-0.8.2 lib/vedeu/coercers/colour.rb
vedeu-0.8.1 lib/vedeu/coercers/colour.rb