Class: Pastel::Delegator Private

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/pastel/delegator.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Wrapes the DecoratorChain to allow for easy resolution of string coloring.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resolver, chain) ⇒ Delegator

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Create Delegator

Used internally by Pastel

Parameters:


38
39
40
41
# File 'lib/pastel/delegator.rb', line 38

def initialize(resolver, chain)
  @resolver = resolver
  @chain = chain
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object (protected)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Handles color method calls


89
90
91
92
93
94
95
96
97
98
99
# File 'lib/pastel/delegator.rb', line 89

def method_missing(method_name, *args, &block)
  new_chain = chain.add(method_name)
  delegator = self.class.wrap(resolver, new_chain)
  if args.empty? && method_name.to_sym != :detach
    delegator
  else
    strings = args.dup
    strings << evaluate_block(&block) if block_given?
    resolver.resolve(new_chain, strings.join)
  end
end

Class Method Details

.wrap(resolver, chain = DecoratorChain.empty) ⇒ Object

Wrap resolver and chain


25
26
27
# File 'lib/pastel/delegator.rb', line 25

def self.wrap(resolver, chain = DecoratorChain.empty)
  new(resolver, chain)
end

Instance Method Details

#==(other) ⇒ Boolean

Compare delegated objects for equivalence of attributes

Returns:

  • (Boolean)

57
58
59
# File 'lib/pastel/delegator.rb', line 57

def ==(other)
  other.is_a?(self.class) && chain == other.chain
end

#eql?(other) ⇒ Boolean

Compare delegated objects for equality of attributes

Returns:

  • (Boolean)

48
49
50
# File 'lib/pastel/delegator.rb', line 48

def eql?(other)
  instance_of?(other.class) && chain.eql?(other.chain)
end

#hashNumeric

Hash for this instance and its attributes

Returns:

  • (Numeric)

76
77
78
# File 'lib/pastel/delegator.rb', line 76

def hash
  [self.class, chain].hash
end

#inspectString Also known as: to_s

Object string representation

Returns:

  • (String)

66
67
68
# File 'lib/pastel/delegator.rb', line 66

def inspect
  "#<Pastel styles=#{chain.map(&:to_s)}>"
end