Class: Unit::ComposedUnit
- 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.
- #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
Methods inherited from BaseUnit
Constructor Details
#initialize(params) ⇒ ComposedUnit
Returns a new instance of ComposedUnit.
28 29 30 31 32 33 34 |
# File 'lib/unitmanager/unit_composition.rb', line 28 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.
26 27 28 |
# File 'lib/unitmanager/unit_composition.rb', line 26 def coefficient @coefficient end |
#dividends ⇒ Object (readonly)
Returns the value of attribute dividends.
26 27 28 |
# File 'lib/unitmanager/unit_composition.rb', line 26 def dividends @dividends end |
#divisors ⇒ Object (readonly)
Returns the value of attribute divisors.
26 27 28 |
# File 'lib/unitmanager/unit_composition.rb', line 26 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.
70 71 72 73 74 75 76 77 78 79 |
# File 'lib/unitmanager/unit_composition.rb', line 70 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 |
#dividend ⇒ Object
52 53 54 |
# File 'lib/unitmanager/unit_composition.rb', line 52 def dividend ComposedUnit.new({:dividends => @dividends}) end |
#divisor ⇒ Object
56 57 58 |
# File 'lib/unitmanager/unit_composition.rb', line 56 def divisor ComposedUnit.new({:divisors => @divisors}) end |
#eql?(other) ⇒ Boolean
81 82 83 |
# File 'lib/unitmanager/unit_composition.rb', line 81 def eql?(other) self == other end |
#from_base(value) ⇒ Object
40 41 42 |
# File 'lib/unitmanager/unit_composition.rb', line 40 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.
46 47 48 49 50 |
# File 'lib/unitmanager/unit_composition.rb', line 46 def h_base ComposedUnit.new({ :coefficient => 1.0, :dividends => Optimizer.base(@dividends), :divisors => Optimizer.base(@divisors) }) end |
#reverse ⇒ Object
60 61 62 63 64 65 66 |
# File 'lib/unitmanager/unit_composition.rb', line 60 def reverse ComposedUnit.new({ :coefficient => 1.0 / coefficient, :dividends => @divisors.collect{|unit| unit }, :divisors => @dividends.collect{|unit| unit } }) end |
#to_base(value) ⇒ Object
36 37 38 |
# File 'lib/unitmanager/unit_composition.rb', line 36 def to_base(value) value * to_base_coef end |