Module: UnitMeasurements

Defined in:
lib/unit_measurements/base.rb,
lib/unit_measurements/math.rb,
lib/unit_measurements/unit.rb,
lib/unit_measurements/cache.rb,
lib/unit_measurements/parser.rb,
lib/unit_measurements/version.rb,
lib/unit_measurements/formatter.rb,
lib/unit_measurements/arithmetic.rb,
lib/unit_measurements/comparison.rb,
lib/unit_measurements/conversion.rb,
lib/unit_measurements/normalizer.rb,
lib/unit_measurements/unit_group.rb,
lib/unit_measurements/measurement.rb,
lib/unit_measurements/configuration.rb,
lib/unit_measurements/errors/unit_error.rb,
lib/unit_measurements/errors/parse_error.rb,
lib/unit_measurements/unit_group_builder.rb,
lib/unit_measurements/extras/numeric_methods.rb,
lib/unit_measurements/errors/blank_unit_error.rb,
lib/unit_measurements/extras/conversion_methods.rb,
lib/unit_measurements/errors/blank_quantity_error.rb,
lib/unit_measurements/errors/unit_already_defined_error.rb,
lib/unit_measurements/errors/missing_primitive_unit_error.rb,
lib/unit_measurements/errors/primitive_unit_already_set_error.rb

Overview

-*- warn_indent: true -*-

Defined Under Namespace

Modules: Arithmetic, Comparison, Conversion, ConversionMethods, Formatter, Math, NumericMethods Classes: BaseError, BlankQuantityError, BlankUnitError, Cache, Configuration, Measurement, MissingPrimitiveUnitError, Normalizer, ParseError, Parser, PrimitiveUnitAlreadySetError, Unit, UnitAlreadyDefinedError, UnitError, UnitGroup, UnitGroupBuilder

Constant Summary collapse

VERSION =

Current stable version.

"5.17.0"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configurationConfiguration

Returns an instance of Configuration with the values of desired configurable options of unit_measurements. If instance is not present, it initializes a new instance of Configuration.

Returns:

Author:

Since:

  • 5.3.0



112
113
114
# File 'lib/unit_measurements/base.rb', line 112

def configuration
  @configuration ||= Configuration.new
end

Class Method Details

.build {|builder| ... } ⇒ Class

Creates a new unit group based on the provided block of instructions.

The build method allows you to define and create a custom unit group with units and their conversions. It takes a block of instructions as an argument, which is evaluated by an instance of UnitGroupBuilder.

Within the block, you can use various methods provided by UnitGroupBuilder to define units, group them into unit system, and set primitive unit of the unit group. These methods include primitive, system, si_unit, and unit.

The resulting unit group is encapsulated in a new subclass of Measurement. This subclass will have access to the defined units and their conversions through the unit_group class attribute.

This method provides a powerful way to create specialized unit groups tailored to specific measurement domains.

Examples:

UnitMeasurements.build do
  primitive "m"

  system :metric do
    si_unit "m", aliases: ["meter", "metre", "meters", "metres"]
  end

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

  cache "length.json"
end

Yields:

  • (builder)

    A block that defines the units to be added to the unit group. The block takes a UnitGroupBuilder instance as a parameter.

Yield Parameters:

Yield Returns:

  • (UnitGroup)

    Returns an instance of UnitGroup containing the units and their conversions defined within the block.

Returns:

  • (Class)

    A new subclass of Measurement with the defined units and conversions.

See Also:

Author:

Since:

  • 1.0.0



91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/unit_measurements/base.rb', line 91

def build(&block)
  builder = UnitGroupBuilder.new
  builder.instance_eval(&block)

  Class.new(Measurement) do
    class << self
      attr_reader :unit_group
    end

    @unit_group = builder.build
  end
end

.configure {|configuration| ... } ⇒ Object

Configures options of the UnitMeasurements module using a block. It yields the current Configuration instance for updating default values of options by new values specified within a block.

Examples:

UnitMeasurements.configure do |config|
  config.use_cache = false
end

Yields:

Yield Parameters:

  • configuration (Configuration)

    An instance of Configuration with the new values of options.

Yield Returns:

Author:

Since:

  • 5.3.0



147
148
149
# File 'lib/unit_measurements/base.rb', line 147

def configure
  yield configuration
end

.resetConfiguration

Reset the configuration to its default state.

Examples:

UnitMeasurements.reset

Returns:

Author:

Since:

  • 5.3.0



125
126
127
# File 'lib/unit_measurements/base.rb', line 125

def reset
  @configuration = Configuration.new
end