Sha256: 36d51899c45e3ba1ff45e00ad1674d94ffd5ab783f802a1f5ffb7564e3f5fcd0

Contents?: true

Size: 676 Bytes

Versions: 2

Compression:

Stored size: 676 Bytes

Contents

class Measured::UnitSystemBuilder
  def initialize
    @units = []
  end

  def unit(unit_name, aliases: [], value: nil)
    @units << build_unit(unit_name, aliases: aliases, value: value)
    nil
  end

  def build
    Measured::UnitSystem.new(@units)
  end

  private

  def build_unit(name, aliases: [], value: nil)
    unit = Measured::Unit.new(name, aliases: aliases, value: value)
    check_for_duplicate_unit_names!(unit)
    unit
  end

  def check_for_duplicate_unit_names!(unit)
    names = @units.flat_map(&:names)
    if names.any? { |name| unit.names.include?(name) }
      raise Measured::UnitError, "Unit #{unit.name} has already been added."
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
measured-2.0.0 lib/measured/unit_system_builder.rb
measured-2.0.0.pre4 lib/measured/unit_system_builder.rb