Sha256: 47fcd6cb1ac5f820db9c3c723ed97dc2af2ff27fe9295bd6bc3910dfacf5a6a4

Contents?: true

Size: 1.62 KB

Versions: 5

Compression:

Stored size: 1.62 KB

Contents

# frozen_string_literal: true

module Pastel
  # Collects a list of decorators for styling a string
  #
  # @api private
  class DecoratorChain
    include Enumerable

    # Create an empty decorator chain
    #
    # @return [DecoratorChain]
    #
    # @api public
    def self.empty
      @empty ||= self.new
    end

    # Create a decorator chain
    #
    # @api public
    def initialize(decorators = [].freeze)
      @decorators = decorators
    end

    # Add decorator
    #
    # @param [String] decorator
    #
    # @api public
    def add(decorator)
      if decorators.include?(decorator)
        self.class.new(decorators)
      else
        self.class.new(decorators + [decorator])
      end
    end

    # Iterate over list of decorators
    #
    # @api public
    def each(&block)
      decorators.each(&block)
    end

    # Compare colors for equality of attributes
    #
    # @return [Boolean]
    #
    # @api public
    def eql?(other)
      instance_of?(other.class) && decorators.eql?(other.decorators)
    end

    # Compare colors for equivalence of attributes
    #
    # @return [Boolean]
    #
    # @api public
    def ==(other)
      other.is_a?(self.class) && decorators == other.decorators
    end

    # Inspect this instance attributes
    #
    # @return [String]
    #
    # @api public
    def inspect
      "#<#{self.class.name} decorators=#{decorators.inspect}>"
    end

    # Hash for this instance and its attributes
    #
    # @return [Numeric]
    #
    # @api public
    def hash
      [self.class, decorators].hash
    end

    protected

    attr_reader :decorators
  end # DecoratorChain
end # Patel

Version data entries

5 entries across 5 versions & 2 rubygems

Version Path
lotrd-0.1.9 vendor/cache/ruby/2.6.0/gems/pastel-0.8.0/lib/pastel/decorator_chain.rb
lotrd-0.1.8 vendor/cache/ruby/2.6.0/gems/pastel-0.8.0/lib/pastel/decorator_chain.rb
lotrd-0.1.6 vendor/cache/ruby/2.6.0/gems/pastel-0.8.0/lib/pastel/decorator_chain.rb
lotrd-0.1.5 vendor/cache/ruby/2.6.0/gems/pastel-0.8.0/lib/pastel/decorator_chain.rb
pastel-0.8.0 lib/pastel/decorator_chain.rb