Class: UnitMeasurements::Unit
- Inherits:
-
Object
- Object
- UnitMeasurements::Unit
- Defined in:
- lib/unit_measurements/unit.rb
Overview
The UnitMeasurements::Unit
class represents a unit of measurement and provides methods to interact with its properties and conversion factors.
Instance Attribute Summary collapse
-
#aliases ⇒ Set<String>
readonly
A set of alternative names for the unit.
-
#name ⇒ String
readonly
The name of the unit.
-
#system ⇒ String
readonly
The system to which the unit belongs (e.g., “metric”, “imperial”).
-
#unit_group ⇒ UnitGroup
readonly
The unit group to which the unit belongs.
-
#value ⇒ String|Numeric|Array<Numeric, String>
readonly
The conversion value of the unit.
Instance Method Summary collapse
-
#conversion_factor ⇒ Numeric
Calculates the conversion factor for the unit.
-
#initialize(name, value:, aliases:, system:, unit_group: nil) ⇒ Unit
constructor
Initializes a new
Unit
instance. -
#inspect ⇒ Object
Returns an object representation of the unit, including its aliases if present.
-
#names ⇒ Array<String>
Returns an array containing the name of the unit and its aliases, sorted alphabetically.
-
#to_s ⇒ String
Returns the name of the unit as a string.
-
#with(name: nil, value: nil, aliases: nil, system: nil, unit_group: nil) ⇒ Unit
Returns a new
Unit
instance with specified attributes.
Constructor Details
#initialize(name, value:, aliases:, system:, unit_group: nil) ⇒ Unit
Initializes a new Unit
instance.
66 67 68 69 70 71 72 |
# File 'lib/unit_measurements/unit.rb', line 66 def initialize(name, value:, aliases:, system:, unit_group: nil) @name = name.to_s.freeze @value = value @aliases = Set.new(aliases.map(&:to_s).sort.map(&:freeze)).freeze @system = system.to_s.freeze @unit_group = unit_group end |
Instance Attribute Details
#aliases ⇒ Set<String> (readonly)
A set of alternative names for the unit.
38 39 40 |
# File 'lib/unit_measurements/unit.rb', line 38 def aliases @aliases end |
#name ⇒ String (readonly)
The name of the unit.
20 21 22 |
# File 'lib/unit_measurements/unit.rb', line 20 def name @name end |
#system ⇒ String (readonly)
The system to which the unit belongs (e.g., “metric”, “imperial”).
46 47 48 |
# File 'lib/unit_measurements/unit.rb', line 46 def system @system end |
#unit_group ⇒ UnitGroup (readonly)
The unit group to which the unit belongs.
54 55 56 |
# File 'lib/unit_measurements/unit.rb', line 54 def unit_group @unit_group end |
#value ⇒ String|Numeric|Array<Numeric, String> (readonly)
The conversion value of the unit. It can be a numeric value or a string in the form of a number followed by a unit name (e.g., “10 m”).
30 31 32 |
# File 'lib/unit_measurements/unit.rb', line 30 def value @value end |
Instance Method Details
#conversion_factor ⇒ Numeric
Calculates the conversion factor for the unit. This method is recursively invoked to calculate the conversion factor of the unit, if unit conversion value
is specified with respect to the other unit.
This method uses parse_value
method to extract the conversion value and the unit.
148 149 150 151 152 153 154 155 |
# File 'lib/unit_measurements/unit.rb', line 148 def conversion_factor return value if value.is_a?(Numeric) measurement_value, measurement_unit = parse_value(value) conversion_factor = unit_group.unit_for!(measurement_unit).conversion_factor conversion_factor * measurement_value end |
#inspect ⇒ Object
Returns an object representation of the unit, including its aliases if present.
131 132 133 134 |
# File 'lib/unit_measurements/unit.rb', line 131 def inspect aliases = "(#{@aliases.join(", ")})" if @aliases.any? "#<#{self.class.name}: #{name} #{aliases}>" end |
#names ⇒ Array<String>
Returns an array containing the name of the unit and its aliases, sorted alphabetically.
107 108 109 |
# File 'lib/unit_measurements/unit.rb', line 107 def names (aliases + [name]).sort.freeze end |
#to_s ⇒ String
Returns the name of the unit as a string.
121 122 123 |
# File 'lib/unit_measurements/unit.rb', line 121 def to_s name end |
#with(name: nil, value: nil, aliases: nil, system: nil, unit_group: nil) ⇒ Unit
Returns a new Unit
instance with specified attributes.
86 87 88 89 90 91 92 93 94 |
# File 'lib/unit_measurements/unit.rb', line 86 def with(name: nil, value: nil, aliases: nil, system: nil, unit_group: nil) self.class.new( (name || self.name), value: (value || self.value), aliases: (aliases || self.aliases), system: (system || self.system), unit_group: (unit_group || self.unit_group) ) end |