Sha256: 89a42a1b9f485ddfe43e866c7fea8e5854733e65b2591541a06b2cb71f9d0781

Contents?: true

Size: 1.69 KB

Versions: 15

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, :abbrivation, :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

15 entries across 15 versions & 1 rubygems

Version Path
ecom_core-1.3.13 app/models/ecom/core/measurement_unit.rb
ecom_core-1.3.12 app/models/ecom/core/measurement_unit.rb
ecom_core-1.3.11 app/models/ecom/core/measurement_unit.rb
ecom_core-1.3.10 app/models/ecom/core/measurement_unit.rb
ecom_core-1.3.9 app/models/ecom/core/measurement_unit.rb
ecom_core-1.3.8 app/models/ecom/core/measurement_unit.rb
ecom_core-1.3.7 app/models/ecom/core/measurement_unit.rb
ecom_core-1.3.6 app/models/ecom/core/measurement_unit.rb
ecom_core-1.3.5 app/models/ecom/core/measurement_unit.rb
ecom_core-1.3.4 app/models/ecom/core/measurement_unit.rb
ecom_core-1.3.3 app/models/ecom/core/measurement_unit.rb
ecom_core-1.3.2 app/models/ecom/core/measurement_unit.rb
ecom_core-1.3.1 app/models/ecom/core/measurement_unit.rb
ecom_core-1.3.0 app/models/ecom/core/measurement_unit.rb
ecom_core-1.2.36 app/models/ecom/core/measurement_unit.rb