Class: Unit::ComposedUnit
- Inherits:
-
Object
- Object
- Unit::ComposedUnit
- Extended by:
- ExecControl
- Includes:
- Comparable
- Defined in:
- lib/unitmanager/unit_composition.rb
Overview
Implementation of what is called ‘composed unit of measure’. This is a result of performing multiplication or division of quantities with simple units. Example: 1 lb per in or 70 mi per hour
Instance Attribute Summary collapse
-
#coefficient ⇒ Object
readonly
Returns the value of attribute coefficient.
-
#dividends ⇒ Object
readonly
Returns the value of attribute dividends.
-
#divisors ⇒ Object
readonly
Returns the value of attribute divisors.
Instance Method Summary collapse
-
#==(other) ⇒ Object
Two composed units are equal if their coefficients are the same and units contain same set of dividends and divisors.
-
#[](symbol) ⇒ Object
This method allows to retrieve symbolic portions of the unit definition.
- #dividend ⇒ Object
- #divisor ⇒ Object
- #eql?(other) ⇒ Boolean
- #from_base(value) ⇒ Object
-
#h_base ⇒ Object
Returns unit representing hierarchy base for the given unit.
-
#initialize(params) ⇒ ComposedUnit
constructor
A new instance of ComposedUnit.
- #reverse ⇒ Object
- #to_base(value) ⇒ Object
Methods included from Comparable
Constructor Details
#initialize(params) ⇒ ComposedUnit
Returns a new instance of ComposedUnit.
15 16 17 18 19 20 21 |
# File 'lib/unitmanager/unit_composition.rb', line 15 def initialize(params) params = Optimizer.process(params) @coefficient = params[:coefficient] || 1 @dividends = params[:dividends] || [] @divisors = params[:divisors] || [] end |
Instance Attribute Details
#coefficient ⇒ Object (readonly)
Returns the value of attribute coefficient.
13 14 15 |
# File 'lib/unitmanager/unit_composition.rb', line 13 def coefficient @coefficient end |
#dividends ⇒ Object (readonly)
Returns the value of attribute dividends.
13 14 15 |
# File 'lib/unitmanager/unit_composition.rb', line 13 def dividends @dividends end |
#divisors ⇒ Object (readonly)
Returns the value of attribute divisors.
13 14 15 |
# File 'lib/unitmanager/unit_composition.rb', line 13 def divisors @divisors end |
Instance Method Details
#==(other) ⇒ Object
Two composed units are equal if their coefficients are the same and units contain same set of dividends and divisors.
57 58 59 60 61 62 63 64 65 66 |
# File 'lib/unitmanager/unit_composition.rb', line 57 def ==(other) return false if other == nil return false unless other.kind_of?(self.class) result = (coefficient == other.coefficient) && dividends.same?(other.dividends) && divisors.same?(other.divisors) return result end |
#[](symbol) ⇒ Object
This method allows to retrieve symbolic portions of the unit definition. Supported values are: :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.
79 80 81 |
# File 'lib/unitmanager/unit_composition.rb', line 79 def [] (symbol) unit_sym[symbol] end |
#dividend ⇒ Object
39 40 41 |
# File 'lib/unitmanager/unit_composition.rb', line 39 def dividend ComposedUnit.new({:dividends => @dividends}) end |
#divisor ⇒ Object
43 44 45 |
# File 'lib/unitmanager/unit_composition.rb', line 43 def divisor ComposedUnit.new({:divisors => @divisors}) end |
#eql?(other) ⇒ Boolean
68 69 70 |
# File 'lib/unitmanager/unit_composition.rb', line 68 def eql?(other) self == other end |
#from_base(value) ⇒ Object
27 28 29 |
# File 'lib/unitmanager/unit_composition.rb', line 27 def from_base(value) value * from_base_coef end |
#h_base ⇒ Object
Returns unit representing hierarchy base for the given unit. The Hierarchy is the “based on” chain.
33 34 35 36 37 |
# File 'lib/unitmanager/unit_composition.rb', line 33 def h_base ComposedUnit.new({ :coefficient => 1.0, :dividends => Optimizer.base(@dividends), :divisors => Optimizer.base(@divisors) }) end |
#reverse ⇒ Object
47 48 49 50 51 52 53 |
# File 'lib/unitmanager/unit_composition.rb', line 47 def reverse ComposedUnit.new({ :coefficient => 1.0 / coefficient, :dividends => @divisors.collect{|unit| unit }, :divisors => @dividends.collect{|unit| unit } }) end |
#to_base(value) ⇒ Object
23 24 25 |
# File 'lib/unitmanager/unit_composition.rb', line 23 def to_base(value) value * to_base_coef end |