Class: Quantity::Quantity

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from CalculationSupport

#==, #coerce, #method_missing

Methods included from Quantifiable

#quantifiable?

Constructor Details

#initialize(calc, params) ⇒ Quantity

Returns a new instance of Quantity.



114
115
116
117
118
# File 'lib/unitmanager/quantity.rb', line 114

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Quantity::CalculationSupport

Instance Attribute Details

#calcObject (readonly)

Returns the value of attribute calc.



112
113
114
# File 'lib/unitmanager/quantity.rb', line 112

def calc
  @calc
end

#unitObject (readonly)

Returns the value of attribute unit.



112
113
114
# File 'lib/unitmanager/quantity.rb', line 112

def unit
  @unit
end

#valueObject (readonly)

Returns the value of attribute value.



112
113
114
# File 'lib/unitmanager/quantity.rb', line 112

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


135
136
137
138
139
# File 'lib/unitmanager/quantity.rb', line 135

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

  @unit[symbol]      
end

#to_quantity(calc) ⇒ Object



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

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