Class: UnitMeasurements::UnitGroupBuilder
- Inherits:
-
Object
- Object
- UnitMeasurements::UnitGroupBuilder
- Defined in:
- lib/unit_measurements/unit_group_builder.rb
Overview
The UnitMeasurements::UnitGroupBuilder
class provides a flexible and configurable way to define units and create unit groups with specific systems and primitive unit.
It provides methods like primitive
, system
, unit
, and si_unit
to define units and their conversions within the unit group.
Instance Attribute Summary collapse
-
#units ⇒ Array<Unit>
readonly
An array to store the units defined using the builder.
Instance Method Summary collapse
-
#build ⇒ UnitGroup
Constructs and returns a
UnitGroup
instance based on the units defined using the builder. -
#initialize ⇒ UnitGroupBuilder
constructor
Initializes a new
UnitGroupBuilder
instance. -
#primitive(primitive) ⇒ Object
Sets the
primitive
unit for the unit group. -
#si_unit(name, value: 1.0, add_binary_prefixes: false, aliases: []) ⇒ Array<Unit>
Builds a set of SI units and adds them to the list of units.
-
#system(system_name, &block) ⇒ Object
Defines the unit system within the unit group and evaluates the provided block in the context of the builder.
-
#unit(name, value: 1.0, aliases: []) ⇒ Unit
Defines a
unit
and adds it to the list of units.
Constructor Details
#initialize ⇒ UnitGroupBuilder
Initializes a new UnitGroupBuilder
instance.
31 32 33 |
# File 'lib/unit_measurements/unit_group_builder.rb', line 31 def initialize @units = [] end |
Instance Attribute Details
#units ⇒ Array<Unit> (readonly)
An array to store the units defined using the builder.
25 26 27 |
# File 'lib/unit_measurements/unit_group_builder.rb', line 25 def units @units end |
Instance Method Details
#build ⇒ UnitGroup
Constructs and returns a UnitGroup
instance based on the units defined using the builder.
84 85 86 |
# File 'lib/unit_measurements/unit_group_builder.rb', line 84 def build UnitGroup.new(@primitive, @units) end |
#primitive(primitive) ⇒ Object
Sets the primitive
unit for the unit group. It raises PrimitiveUnitAlreadySetError
if the primitive unit has already been set for the unit group.
127 128 129 130 131 |
# File 'lib/unit_measurements/unit_group_builder.rb', line 127 def primitive(primitive) raise PrimitiveUnitAlreadySetError if @primitive @primitive = primitive end |
#si_unit(name, value: 1.0, add_binary_prefixes: false, aliases: []) ⇒ Array<Unit>
Builds a set of SI units and adds them to the list of units. This method defines unit defined using name
along with all the SI prefixes.
71 72 73 |
# File 'lib/unit_measurements/unit_group_builder.rb', line 71 def si_unit(name, value: 1.0, add_binary_prefixes: false, aliases: []) @units += build_si_units(name, value: value, add_binary_prefixes: add_binary_prefixes, aliases: aliases) end |
#system(system_name, &block) ⇒ Object
Defines the unit system within the unit group and evaluates the provided block in the context of the builder.
This method is used to group multiple units within the certain unit system, viz., metric, imperial, etc.
106 107 108 109 110 111 |
# File 'lib/unit_measurements/unit_group_builder.rb', line 106 def system(system_name, &block) @system = system_name instance_eval(&block) if block_given? ensure @system = nil end |
#unit(name, value: 1.0, aliases: []) ⇒ Unit
Defines a unit
and adds it to the list of units.
49 50 51 |
# File 'lib/unit_measurements/unit_group_builder.rb', line 49 def unit(name, value: 1.0, aliases: []) @units << build_unit(name, value: value, aliases: aliases) end |