Sha256: 19f6d1fa269aaa2819af072d943bfd404c59a178c0e9fddd4fd58efcf54c05e1

Contents?: true

Size: 898 Bytes

Versions: 7

Compression:

Stored size: 898 Bytes

Contents

# -*- encoding: utf-8 -*-
# -*- frozen_string_literal: true -*-
# -*- warn_indent: true -*-

module UnitMeasurements
  module UnitMethods
    def unit_for(name)
      unit_name_to_unit(name)
    end

    def unit_for!(name)
      unit = unit_for(name)
      raise UnitError, name unless unit
      unit
    end
    alias_method :[], :unit_for!

    def unit_with_name_and_aliases
      units.each_with_object({}) do |unit, hash|
        unit.names.each { |name| hash[name.to_s] = unit }
      end
    end

    def unit_names
      units.map(&:name).sort
    end

    def unit_names_with_aliases
      units.flat_map(&:names).sort
    end

    def unit_name_to_unit(name)
      unit_with_name_and_aliases[name.to_s]
    end

    def defined?(name)
      unit = unit_for(name)
      unit ? unit.name == name.to_s : false
    end

    def unit_or_alias?(name)
      !!unit_for(name)
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
unit_measurements-3.8.0 lib/unit_measurements/unit_methods.rb
unit_measurements-3.7.0 lib/unit_measurements/unit_methods.rb
unit_measurements-3.6.0 lib/unit_measurements/unit_methods.rb
unit_measurements-3.5.0 lib/unit_measurements/unit_methods.rb
unit_measurements-3.4.1 lib/unit_measurements/unit_methods.rb
unit_measurements-3.3.0 lib/unit_measurements/unit_methods.rb
unit_measurements-3.4.0 lib/unit_measurements/unit_methods.rb