Sha256: becb8ac82b67d0432929f43bbca257b141a39dd66c49b4d183cc3091c62e87cc

Contents?: true

Size: 1.69 KB

Versions: 1

Compression:

Stored size: 1.69 KB

Contents

module Ecom
  module Core
    class MeasurementUnit < ApplicationRecord
      METRIC = 'Metric'.freeze
      IMPERIAL = 'Imperial'.freeze
      SYSTEM_OF_MEASURMENT = [METRIC, IMPERIAL].freeze

      # commonly used physical quantities in construction
      LENGTH = 'Length'.freeze
      MASS = 'Mass'.freeze
      TIME = 'Time'.freeze
      CURRENT = 'Current'.freeze
      TEMPRATURE = 'Temprature'.freeze

      AREA = 'Area'.freeze
      VOLUME = 'Volume'.freeze
      ENERGY = 'Energy'.freeze
      DENSITY = 'DENSITY'.freeze

      PHYSICAL_QUANTITIES = [LENGTH, MASS, TIME, CURRENT, TEMPRATURE, AREA, VOLUME, ENERGY, DENSITY].freeze

      validates :name, :abbreviation, :physical_quantity,
                :conversion_factor, :system_of_measurement, presence: true

      validates :is_si_unit, inclusion: { in: [true, false] }

      validates :system_of_measurement, inclusion: SYSTEM_OF_MEASURMENT
      validates :physical_quantity, inclusion: PHYSICAL_QUANTITIES

      validate :si_unit_with_conversion_factor
      validates_uniqueness_of :is_si_unit, scope: %i[physical_quantity system_of_measurement],
                                           if: -> { is_si_unit == true },
                                           message: 'There exist an si unit of measurement for the '\
                                            'given physical quantity and system of measurement'

      def si_unit_with_conversion_factor
        if is_si_unit && conversion_factor != 1
          errors.add(:base, 'Conversion factor for an SI unit has to be 1')
        elsif !is_si_unit && conversion_factor == 1
          errors.add(:base, 'Only SI units can have a conversion factor 1')
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ecom_core-1.3.14 app/models/ecom/core/measurement_unit.rb