Class: Unit::SimpleUnit
- Inherits:
-
Object
- Object
- Unit::SimpleUnit
- Includes:
- Comparable
- Defined in:
- lib/unitmanager/simple_unit.rb
Overview
Implementation of what is called ‘simple unit of measure’. This is a linear unit which is optionally based on some other simple unit with linear coefficient. Example: :cm is based on :mm with coefficient equal 10. In other words X cm = 10 * Y mm.
Constant Summary collapse
- @@EMPTY_UNIT =
SimpleUnit.new()
Instance Attribute Summary collapse
-
#based_on ⇒ Object
readonly
Returns the value of attribute based_on.
-
#coefficient ⇒ Object
readonly
Returns the value of attribute coefficient.
-
#unit ⇒ Object
readonly
Returns the value of attribute unit.
Instance Method Summary collapse
-
#==(other) ⇒ Object
Two Simple units are equal if they have same symbol, based on the same unit with the same coefficient.
-
#[](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(args = {}) ⇒ SimpleUnit
constructor
A new instance of SimpleUnit.
- #to_base(value) ⇒ Object
Methods included from Comparable
Constructor Details
#initialize(args = {}) ⇒ SimpleUnit
Returns a new instance of SimpleUnit.
39 40 41 42 43 44 |
# File 'lib/unitmanager/simple_unit.rb', line 39 def initialize (args = {}) @unit = args[:unit] @based_on = args[:based_on] || nil @coefficient = args[:coefficient] || 1 @unit_sym = {:dividends => [@unit], :divisors => [], :string => @unit.to_s} end |
Instance Attribute Details
#based_on ⇒ Object (readonly)
Returns the value of attribute based_on.
35 36 37 |
# File 'lib/unitmanager/simple_unit.rb', line 35 def based_on @based_on end |
#coefficient ⇒ Object (readonly)
Returns the value of attribute coefficient.
35 36 37 |
# File 'lib/unitmanager/simple_unit.rb', line 35 def coefficient @coefficient end |
#unit ⇒ Object (readonly)
Returns the value of attribute unit.
35 36 37 |
# File 'lib/unitmanager/simple_unit.rb', line 35 def unit @unit end |
Instance Method Details
#==(other) ⇒ Object
Two Simple units are equal if they have same symbol, based on the same unit with the same coefficient
72 73 74 75 76 77 |
# File 'lib/unitmanager/simple_unit.rb', line 72 def ==(other) return false if other == nil return false unless other.kind_of?(self.class) unit == other.unit && based_on == other.based_on && coefficient == other.coefficient 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.
:string - string representation of the unit of measure.
88 89 90 |
# File 'lib/unitmanager/simple_unit.rb', line 88 def [] (symbol) @unit_sym[symbol] end |
#dividend ⇒ Object
62 63 64 |
# File 'lib/unitmanager/simple_unit.rb', line 62 def dividend @unit end |
#divisor ⇒ Object
66 67 68 |
# File 'lib/unitmanager/simple_unit.rb', line 66 def divisor @@EMPTY_UNIT end |
#eql?(other) ⇒ Boolean
79 80 81 |
# File 'lib/unitmanager/simple_unit.rb', line 79 def eql?(other) self == other end |
#from_base(value) ⇒ Object
51 52 53 54 |
# File 'lib/unitmanager/simple_unit.rb', line 51 def from_base(value) value = @based_on.from_base(value) if derived? value /= @coefficient end |
#h_base ⇒ Object
Returns unit representing hierarchy base for the given unit. The Hierarchy is the “based on” chain.
58 59 60 |
# File 'lib/unitmanager/simple_unit.rb', line 58 def h_base derived? ? @based_on.h_base : self end |
#to_base(value) ⇒ Object
46 47 48 49 |
# File 'lib/unitmanager/simple_unit.rb', line 46 def to_base(value) value = @based_on.to_base(value) if derived? value *= @coefficient end |