Sha256: 77f98cca9b51a519f07667fd66d20f74c9065c949c91eec5951fced6aec2c920

Contents?: true

Size: 1.42 KB

Versions: 3

Compression:

Stored size: 1.42 KB

Contents

# frozen_string_literal: true

module Lite
  module Measurements
    class Length < Lite::Measurements::Base

      include Lite::Measurements::Helpers::ConversionHelper
      include Lite::Measurements::Helpers::ShiftHelper

      CONVERTER ||= 0.0254

      IMPERICAL_UNITS ||= {
        inches: 1.0, feet: 12.0, yards: 36.0, miles: 63_360.0, nautical_miles: 72_913.386
      }.freeze
      METRIC_UNITS ||= {
        micrometers: 0.000001, millimeters: 0.001, centimeters: 0.01, decimeters: 0.1, meters: 1.0,
        dekameters: 10.0, hectometers: 100.0, kilometers: 1_000.0
      }.freeze

      # rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Metrics/LineLength
      def convert(from:, to:)
        assert_all_valid_keys!(from, to, all_keys)

        if equal_units?(from, to)
          amount
        elsif shift_between_imperical_units?(from, to)
          shift_units(amount, type: IMPERICAL_UNITS, from: from, to: to)
        elsif shift_between_metric_units?(from, to)
          shift_units(amount, type: METRIC_UNITS, from: from, to: to)
        elsif convert_to_metric_units?(from, to)
          convert_to_metric_units(amount, from: from, convert_to: :inches, convert_from: :meters, to: to)
        else
          convert_to_imperical_units(amount, from: from, convert_to: :meters, convert_from: :inches, to: to)
        end
      end
      # rubocop:enable Metrics/AbcSize, Metrics/MethodLength, Metrics/LineLength

    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
lite-measurements-1.0.2 lib/lite/measurements/length.rb
lite-measurements-1.0.1 lib/lite/measurements/length.rb
lite-measurements-1.0.0 lib/lite/measurements/length.rb