Class: UnitMeasurements::UnitGroupBuilder

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initializeUnitGroupBuilder

Initializes a new UnitGroupBuilder instance.

Author:

Since:

  • 1.0.0



31
32
33
# File 'lib/unit_measurements/unit_group_builder.rb', line 31

def initialize
  @units = []
end

Instance Attribute Details

#unitsArray<Unit> (readonly)

An array to store the units defined using the builder.

Returns:

  • (Array<Unit>)

    An array of Unit instances.

Author:

Since:

  • 1.0.0



25
26
27
# File 'lib/unit_measurements/unit_group_builder.rb', line 25

def units
  @units
end

Instance Method Details

#buildUnitGroup

Constructs and returns a UnitGroup instance based on the units defined using the builder.

Returns:

  • (UnitGroup)

    An instance of UnitGroup containing units and their conversions.

See Also:

Author:

Since:

  • 1.0.0



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.

Examples:

primitive "m"

Parameters:

  • primitive (String|Symbol)

    The name of the primitive unit.

Raises:

See Also:

Author:

Since:

  • 4.0.0



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.

Examples:

si_unit "m", aliases: ["meter", "metre", "meters", "metres"]

Parameters:

  • name (String|Symbol)

    The name of the unit.

  • value (Numeric|String) (defaults to: 1.0)

    The conversion value of the unit.

  • add_binary_prefixes (TrueClass|FalseClass) (defaults to: false)

    Whether the unit supports binary SI prefixes along with decimal SI prefixes.

  • aliases (Array<String|Symbol>, optional) (defaults to: [])

    An array of alternative names for the unit.

Returns:

  • (Array<Unit>)

    An array of Unit instances.

See Also:

  • #build_si_units

Author:

Since:

  • 1.0.0



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.

Examples:

Defining an imperial system with inches and feet:

system :imperial do
  unit "in", value: "25.4 mm", aliases: ['"', "inch", "inches"]
  unit "ft", value: "12 in", aliases: ["'", "foot", "feet"]
end

Parameters:

  • system_name (String|Symbol)

    The name of the unit system in which the units are to be grouped.

  • block

    A block of code to be executed in the context of the builder.

Author:

Since:

  • 4.0.0



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.

Examples:

unit "in", value: "25.4 mm", aliases: ['"', "inch", "inches"]

Parameters:

  • name (String|Symbol)

    The name of the unit.

  • value (Numeric|String) (defaults to: 1.0)

    The conversion value of the unit.

  • aliases (Array<String|Symbol>, optional) (defaults to: [])

    An array of alternative names for the unit.

Returns:

  • (Unit)

    An instance of Unit.

See Also:

  • #build_unit

Author:

Since:

  • 1.0.0



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