Class: Quantified::Coder

Inherits:
Object
  • Object
show all
Defined in:
lib/quantified/coder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attribute, unit) ⇒ Coder

Returns a new instance of Coder.



5
6
7
8
# File 'lib/quantified/coder.rb', line 5

def initialize(attribute, unit)
  @attribute = attribute
  @unit = unit
end

Instance Attribute Details

#attributeObject (readonly)

Returns the value of attribute attribute.



3
4
5
# File 'lib/quantified/coder.rb', line 3

def attribute
  @attribute
end

#unitObject (readonly)

Returns the value of attribute unit.



3
4
5
# File 'lib/quantified/coder.rb', line 3

def unit
  @unit
end

Instance Method Details

#dump(quantity) ⇒ Object



14
15
16
17
18
19
20
21
# File 'lib/quantified/coder.rb', line 14

def dump(quantity)
  conversion = "to_#{unit}"
  if quantity.respond_to?(conversion)
    quantity.send(conversion)
  else
    quantity
  end
end

#load(amount) ⇒ Object



10
11
12
# File 'lib/quantified/coder.rb', line 10

def load(amount)
  attribute.new(amount, unit) unless amount.nil?
end