Class: Falu::Swatch

Inherits:
Object
  • Object
show all
Defined in:
lib/falu/swatch.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(color, count = 0) ⇒ Swatch

Returns a new instance of Swatch.



18
19
20
21
# File 'lib/falu/swatch.rb', line 18

def initialize(color, count=0)
  @color = color.is_a?(Falu::Color) ? color : Falu::Color.new(color)
  @count = count.to_i
end

Instance Attribute Details

#colorObject (readonly)

Returns the value of attribute color.



3
4
5
# File 'lib/falu/swatch.rb', line 3

def color
  @color
end

#countObject (readonly)

Returns the value of attribute count.



3
4
5
# File 'lib/falu/swatch.rb', line 3

def count
  @count
end

Class Method Details

.dump(swatch) ⇒ Object



8
9
10
# File 'lib/falu/swatch.rb', line 8

def dump(swatch)
  swatch.as_json
end

.load(json) ⇒ Object



12
13
14
15
# File 'lib/falu/swatch.rb', line 12

def load(json)
  json.symbolize_keys!
  new(json[:color].values.first, json[:count])
end

Instance Method Details

#+(cnt) ⇒ Object



35
36
37
# File 'lib/falu/swatch.rb', line 35

def +(cnt)
  self.class.new(color, (count + cnt.to_i))
end

#-(cnt) ⇒ Object



39
40
41
# File 'lib/falu/swatch.rb', line 39

def -(cnt)
  self.class.new(color, (count - cnt.to_i))
end

#<=>(other) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/falu/swatch.rb', line 23

def <=>(other)
  if count == other.count
    color <=> color
  else
    count <=> other.count
  end
end

#==(other) ⇒ Object



31
32
33
# File 'lib/falu/swatch.rb', line 31

def ==(other)
  to_s == other.to_s
end

#as_json(options = {}) ⇒ Object



59
60
61
62
# File 'lib/falu/swatch.rb', line 59

def as_json(options={})
  #{color: color.as_json, count: count}
  to_a
end

#to_aObject



51
52
53
# File 'lib/falu/swatch.rb', line 51

def to_a
  [to_s, to_i]
end

#to_hObject



55
56
57
# File 'lib/falu/swatch.rb', line 55

def to_h
  {color: to_s, count: to_i}
end

#to_iObject



43
44
45
# File 'lib/falu/swatch.rb', line 43

def to_i
  count
end

#to_sObject



47
48
49
# File 'lib/falu/swatch.rb', line 47

def to_s
  color.to_s
end