Class: UnitMeasurements::UnitGroup
- Inherits:
-
Object
- Object
- UnitMeasurements::UnitGroup
- Defined in:
- lib/unit_measurements/unit_group.rb
Overview
The UnitMeasurements::UnitGroup
class provides a collection of units with methods to retrieve units by name, check if a unit is defined, and much more.
It serves as a container for organizing and working with units within the unit group.
Instance Attribute Summary collapse
-
#primitive ⇒ Unit
readonly
The primitive unit of the unit group.
-
#units ⇒ Array<Unit>
readonly
An array of units within the unit group.
Instance Method Summary collapse
-
#defined?(name) ⇒ TrueClass|FalseClass
Checks if a unit with a given name is defined within the unit group.
-
#initialize(primitive, units) ⇒ UnitGroup
constructor
Initializes a new
UnitGroup
instance. -
#unit_for(name) ⇒ Unit|NilClass
Returns the unit instance for a given unit name.
-
#unit_for!(name) ⇒ Unit
(also: #[])
This method works same as
unit_for
but it raisesUnitError
if the unit is not defined within the unit group. -
#unit_names ⇒ Array<String>
Returns an array of names of all the units defined within the unit group, sorted alphabetically.
-
#unit_names_with_aliases ⇒ Array<String>
Returns an array of names and aliases of all the units defined within the unit group, sorted alphabetically.
-
#unit_or_alias?(name) ⇒ TrueClass|FalseClass
Checks if a given name corresponds to a defined unit or an alias of any defined unit.
-
#units_for(system_name) ⇒ Array<Unit>
Returns an array of units associated with a specified
unit_system
. -
#units_for!(system_name) ⇒ Array<Unit>
This method works same as
units_for
method but it raises an error if there are no units associated with thesystem_name
.
Constructor Details
#initialize(primitive, units) ⇒ UnitGroup
Initializes a new UnitGroup
instance.
46 47 48 49 |
# File 'lib/unit_measurements/unit_group.rb', line 46 def initialize(primitive, units) @units = units.map { |unit| unit.with(unit_group: self) } @primitive = unit_for!(primitive) if primitive end |
Instance Attribute Details
#primitive ⇒ Unit (readonly)
The primitive unit of the unit group.
25 26 27 |
# File 'lib/unit_measurements/unit_group.rb', line 25 def primitive @primitive end |
#units ⇒ Array<Unit> (readonly)
An array of units within the unit group.
37 38 39 |
# File 'lib/unit_measurements/unit_group.rb', line 37 def units @units end |
Instance Method Details
#defined?(name) ⇒ TrueClass|FalseClass
Checks if a unit with a given name is defined within the unit group.
145 146 147 148 149 |
# File 'lib/unit_measurements/unit_group.rb', line 145 def defined?(name) unit = unit_for(name) unit ? unit.name == name.to_s : false end |
#unit_for(name) ⇒ Unit|NilClass
Returns the unit instance for a given unit name. It returns nil
if unit is not defined within the unit group.
67 68 69 |
# File 'lib/unit_measurements/unit_group.rb', line 67 def unit_for(name) unit_name_to_unit(name) end |
#unit_for!(name) ⇒ Unit Also known as: []
This method works same as unit_for
but it raises UnitError
if the unit is not defined within the unit group.
91 92 93 94 95 96 |
# File 'lib/unit_measurements/unit_group.rb', line 91 def unit_for!(name) unit = unit_for(name) raise UnitError, name unless unit unit end |
#unit_names ⇒ Array<String>
Returns an array of names of all the units defined within the unit group, sorted alphabetically.
110 111 112 |
# File 'lib/unit_measurements/unit_group.rb', line 110 def unit_names units.map(&:name).sort end |
#unit_names_with_aliases ⇒ Array<String>
Returns an array of names and aliases of all the units defined within the unit group, sorted alphabetically.
125 126 127 |
# File 'lib/unit_measurements/unit_group.rb', line 125 def unit_names_with_aliases units.flat_map(&:names).sort end |
#unit_or_alias?(name) ⇒ TrueClass|FalseClass
Checks if a given name corresponds to a defined unit or an alias of any defined unit.
168 169 170 |
# File 'lib/unit_measurements/unit_group.rb', line 168 def unit_or_alias?(name) !!unit_for(name) end |
#units_for(system_name) ⇒ Array<Unit>
Returns an array of units associated with a specified unit_system
.
This method takes a unit system name as an argument and filters the units in the unit group to return only those units that belong to the specified unit system. It then returns an array containing these filtered units. If there are no units associated with unit system, it returns empty array.
197 198 199 |
# File 'lib/unit_measurements/unit_group.rb', line 197 def units_for(system_name) units.select { |unit| unit.system.to_s == system_name.to_s } end |
#units_for!(system_name) ⇒ Array<Unit>
This method works same as units_for
method but it raises an error if there are no units associated with the system_name
.
226 227 228 229 230 231 232 233 234 |
# File 'lib/unit_measurements/unit_group.rb', line 226 def units_for!(system_name) system_units = units_for(system_name) unless system_units.any? raise BaseError, "Invalid unit system '#{system_name}' within the unit group." end system_units end |