Class: ColorCalculator::Clump::Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/color_calculator/clump/abstract.rb

Direct Known Subclasses

Lab, Lch, Rgb, Xyz

Instance Method Summary collapse

Constructor Details

#initialize(*attributes) ⇒ Abstract



8
9
10
11
12
13
14
15
16
# File 'lib/color_calculator/clump/abstract.rb', line 8

def initialize(*attributes)
  self.class.attributes.each.with_index do |name, index|
    instance_variable_set("@#{name}".to_sym, attributes[index])

    define_singleton_method(name) do
      instance_variable_get("@#{name}".to_sym)
    end
  end
end

Instance Method Details

#==(other) ⇒ Object



24
25
26
27
28
# File 'lib/color_calculator/clump/abstract.rb', line 24

def ==(other)
  raise UnlikeComparisonError if self.class != other.class

  to_h == other.to_h
end

#inspectObject Also known as: to_s



30
31
32
# File 'lib/color_calculator/clump/abstract.rb', line 30

def inspect
  to_h.to_s
end

#to_hObject



18
19
20
21
22
# File 'lib/color_calculator/clump/abstract.rb', line 18

def to_h
  self.class.attributes.reduce({}) do |memo, message|
    memo.merge(message => public_send(message))
  end
end