Class: Quantity::Quantity

Inherits:
Object
  • Object
show all
Includes:
Calculable
Defined in:
lib/unitmanager/quantity.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Calculable

#*, #+, #-, #/, #==, #coerce

Methods included from Quantifiable

#quantifiable?

Constructor Details

#initialize(calc, params) ⇒ Quantity

Returns a new instance of Quantity.



123
124
125
126
127
# File 'lib/unitmanager/quantity.rb', line 123

def initialize(calc, params)
  @calc = calc
  @unit = params[:unit] || Unit::ComposedUnit.new({})
  @value = params[:value] || 0.0
end

Instance Attribute Details

#calcObject (readonly)

Returns the value of attribute calc.



121
122
123
# File 'lib/unitmanager/quantity.rb', line 121

def calc
  @calc
end

#unitObject (readonly)

Returns the value of attribute unit.



121
122
123
# File 'lib/unitmanager/quantity.rb', line 121

def unit
  @unit
end

#valueObject (readonly)

Returns the value of attribute value.



121
122
123
# File 'lib/unitmanager/quantity.rb', line 121

def value
  @value
end

Instance Method Details

#[](symbol) ⇒ Object

:dividends - returns an array of symbols representing dividend part of

             unit definition.
:divisors -  returns an array of symbols representing divisor part of 
             unit definition.
:string - string representation of the unit of measure. 
:value - returns quantity value


144
145
146
147
148
# File 'lib/unitmanager/quantity.rb', line 144

def [] (symbol)
  return value if symbol == :value

  @unit[symbol]      
end

#to_quantity(calc) ⇒ Object



129
130
131
132
133
# File 'lib/unitmanager/quantity.rb', line 129

def to_quantity(calc)
  raise "Incompatible Argument." + 
      "Both operands must be created by the same calculator" unless @calc == calc
  self
end