Class: UnitMeasurements::Measurement Abstract
- Inherits:
-
Object
- Object
- UnitMeasurements::Measurement
- Extended by:
- Forwardable
- Includes:
- Arithmetic, Comparison, Conversion, Formatter, Math
- Defined in:
- lib/unit_measurements/measurement.rb
Overview
The UnitMeasurements::Measurement
is the abstract class and serves as superclass for all the unit groups. It includes several modules that provide mathematical operations, comparison, conversion, formatting, and other functionalities.
This class provides a comprehensive set of methods and functionality for working with measurements in different units. It includes robust error handling and supports conversion between units. Additionally, it ensures that measurements are consistently represented.
You should not directly initialize a Measurement
instance. Instead, create specialized measurement types by subclassing Measurement
and providing specific units and conversions through the build
method defined in the UnitMeasurements
module.
Constant Summary collapse
- CONVERSION_STRING_REGEXP =
Regular expression to match conversion strings.
/(.+?)\s?(?:\s+(?:in|to|as)\s+(.+)|\z)/i.freeze
Constants included from Formatter
Instance Attribute Summary collapse
-
#quantity ⇒ Numeric
readonly
Returns the
quantity
of themeasurement
. -
#unit ⇒ Unit
readonly
The unit associated with the measurement.
Class Method Summary collapse
-
.parse(input) ⇒ Measurement
Parses an input string and returns a
Measurement
instance depending on the input string.
Instance Method Summary collapse
-
#convert_to(target_unit) ⇒ Measurement
(also: #to, #in, #as)
Converts the measurement to a
target_unit
and returns new instance of the measurement. -
#convert_to!(target_unit) ⇒ Measurement
(also: #to!, #in!, #as!)
Converts the measurement to a
target_unit
and updates the current instance. -
#initialize(quantity, unit) ⇒ Measurement
constructor
Initializes a new instance of
Measurement
with a specifiedquantity
andunit
. -
#inspect(dump: false) ⇒ Object
Returns an object representation of the
Measurement
. -
#to_s ⇒ String
Returns a string representation of the
Measurement
.
Methods included from Math
Methods included from Formatter
Methods included from Conversion
#to_c, #to_d, #to_f, #to_i, #to_r
Methods included from Comparison
Methods included from Arithmetic
#*, #**, #+, #-, #-@, #/, #nonzero?
Constructor Details
#initialize(quantity, unit) ⇒ Measurement
Initializes a new instance of Measurement
with a specified quantity
and unit
.
This method is intended to be overridden by subclasses and serves as a placeholder for common initialization logic. It raises an error if called directly on the abstract Measurement
class.
112 113 114 115 116 117 118 |
# File 'lib/unit_measurements/measurement.rb', line 112 def initialize(quantity, unit) raise BaseError, "Quantity cannot be blank." if quantity.blank? raise BaseError, "Unit cannot be blank." if unit.blank? @quantity = convert_quantity(quantity) @unit = unit_from_unit_or_name!(unit) end |
Instance Attribute Details
#quantity ⇒ Numeric (readonly)
Returns the quantity
of the measurement
.
47 48 49 |
# File 'lib/unit_measurements/measurement.rb', line 47 def quantity @quantity end |
#unit ⇒ Unit (readonly)
The unit associated with the measurement.
55 56 57 |
# File 'lib/unit_measurements/measurement.rb', line 55 def unit @unit end |
Class Method Details
.parse(input) ⇒ Measurement
Parses an input string and returns a Measurement
instance depending on the input string. This method first normalizes the input
internally, using the Normalizer
before parsing it using the Parser
.
If only the source unit is provided, it returns a new Measurement
instance with the quantity in the source unit.If both source and target units are provided in the input string, it returns a new Measurement
instance with the quantity converted to the target unit.
306 307 308 309 310 311 |
# File 'lib/unit_measurements/measurement.rb', line 306 def parse(input) input = Normalizer.normalize(input) source, target = input.match(CONVERSION_STRING_REGEXP)&.captures target ? _parse(source).convert_to(target) : _parse(source) end |
Instance Method Details
#convert_to(target_unit) ⇒ Measurement Also known as: to, in, as
Converts the measurement to a target_unit
and returns new instance of the measurement.
140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/unit_measurements/measurement.rb', line 140 def convert_to(target_unit) target_unit = if target_unit.to_s.eql?("primitive") self.class.unit_group.primitive else unit_from_unit_or_name!(target_unit) end return self if target_unit == unit conversion_factor = (unit.conversion_factor / target_unit.conversion_factor) self.class.new((quantity * conversion_factor), target_unit) end |
#convert_to!(target_unit) ⇒ Measurement Also known as: to!, in!, as!
Converts the measurement to a target_unit
and updates the current instance.
171 172 173 174 175 176 |
# File 'lib/unit_measurements/measurement.rb', line 171 def convert_to!(target_unit) measurement = convert_to(target_unit) @quantity, @unit = measurement.quantity, measurement.unit self end |
#inspect(dump: false) ⇒ Object
Returns an object representation of the Measurement
.
189 190 191 |
# File 'lib/unit_measurements/measurement.rb', line 189 def inspect(dump: false) dump ? super() : to_s end |
#to_s ⇒ String
Returns a string representation of the Measurement
.
199 200 201 |
# File 'lib/unit_measurements/measurement.rb', line 199 def to_s "#{quantity} #{unit}" end |