Class: Moo::Model::Colour

Inherits:
Object show all
Defined in:
lib/moo/model/colour.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|_self| ... } ⇒ Colour

Returns a new instance of Colour.

Yields:

  • (_self)

Yield Parameters:



7
8
9
# File 'lib/moo/model/colour.rb', line 7

def initialize
  yield self if block_given?
end

Instance Attribute Details

#bObject

Returns the value of attribute b.



5
6
7
# File 'lib/moo/model/colour.rb', line 5

def b
  @b
end

#cObject

Returns the value of attribute c.



5
6
7
# File 'lib/moo/model/colour.rb', line 5

def c
  @c
end

#gObject

Returns the value of attribute g.



5
6
7
# File 'lib/moo/model/colour.rb', line 5

def g
  @g
end

#kObject

Returns the value of attribute k.



5
6
7
# File 'lib/moo/model/colour.rb', line 5

def k
  @k
end

#mObject

Returns the value of attribute m.



5
6
7
# File 'lib/moo/model/colour.rb', line 5

def m
  @m
end

#rObject

Returns the value of attribute r.



5
6
7
# File 'lib/moo/model/colour.rb', line 5

def r
  @r
end

#typeObject

Returns the value of attribute type.



5
6
7
# File 'lib/moo/model/colour.rb', line 5

def type
  @type
end

#yObject

Returns the value of attribute y.



5
6
7
# File 'lib/moo/model/colour.rb', line 5

def y
  @y
end

Instance Method Details

#from_json(json) ⇒ Object



77
78
79
80
81
82
83
84
85
86
# File 'lib/moo/model/colour.rb', line 77

def from_json json
  hash = JSON.parse json, :symbolize_names => true
  keys = [:type]
  if hash[:type] == 'RGB'
    keys << :r << :g << :b
  elsif hash[:type] == 'CMYK'
    keys << :c << :m << :y << :k
  end
  keys.each { |k| send (k.to_s + '=').to_sym, hash[k] }
end

#to_hashObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/moo/model/colour.rb', line 58

def to_hash
  if @type == 'RGB'
    return {
      :type => 'RGB',
      :r    => @r,
      :g    => @g,
      :b    => @b
    }
  elsif @type == 'CMYK'
    return {
      :type => 'CMYK',
      :c    => @c,
      :m    => @m,
      :y    => @y,
      :k    => @k
    }
  end
end

#to_jsonObject



54
55
56
# File 'lib/moo/model/colour.rb', line 54

def to_json
  self.to_hash.to_json
end