Sha256: 4ec37780983bcb758e3ee3b9fa712627910221b1a422089c9b3f127320a48e92

Contents?: true

Size: 1.66 KB

Versions: 3

Compression:

Stored size: 1.66 KB

Contents

# frozen_string_literal: true

module Lite
  module Measurements
    module Helpers
      module ConversionHelper

        private

        def all_keys
          [imperical_keys, metric_keys].flatten
        end

        # rubocop:disable Metrics/LineLength
        def convert_to_imperical_units(units, from: nil, convert_to:, convert_from:, to: nil)
          units = shift_units(units, type: klass::METRIC_UNITS, from: from, to: convert_to)
          shift_units(units / klass::CONVERTER, type: klass::IMPERICAL_UNITS, from: convert_from, to: to)
        end

        def convert_to_metric_units(units, from: nil, convert_to:, convert_from:, to: nil)
          units = shift_units(units, type: klass::IMPERICAL_UNITS, from: from, to: convert_to)
          shift_units(units * klass::CONVERTER, type: klass::METRIC_UNITS, from: convert_from, to: to)
        end
        # rubocop:enable Metrics/LineLength

        def convert_to_imperical_units?(from, to)
          metric_keys.include?(from) && imperical_keys.include?(to)
        end

        def convert_to_metric_units?(from, to)
          imperical_keys.include?(from) && metric_keys.include?(to)
        end

        def klass
          self.class
        end

        def imperical_keys
          @imperical_keys ||= klass::IMPERICAL_UNITS.keys
        end

        def metric_keys
          @metric_keys ||= klass::METRIC_UNITS.keys
        end

        def shift_between_imperical_units?(from, to)
          imperical_keys.include?(from) && imperical_keys.include?(to)
        end

        def shift_between_metric_units?(from, to)
          metric_keys.include?(from) && metric_keys.include?(to)
        end

      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

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