Sha256: b4536a9030c968158a39528da553ba94b3c43b04e75f5d75b9fc2a2ce34311ad

Contents?: true

Size: 1.43 KB

Versions: 15

Compression:

Stored size: 1.43 KB

Contents

module QDM
  # Represents a Quantity
  class Quantity
    attr_accessor :value, :unit

    # Value and unit are required.
    def initialize(value, unit)
      @value = value
      @unit = unit
    end

    # Converts an object of this instance into a database friendly value.
    def mongoize
      { value: @value, unit: @unit, _type: 'QDM::Quantity' }
    end

    class << self
      # Get the object as it was stored in the database, and instantiate
      # this custom class from it.
      #
      # The array elements in demongoize are the same 5 elements used in mongoize, i.e.
      # [ value, unit ].
      def demongoize(object)
        return nil unless object
        object = object.symbolize_keys
        QDM::Quantity.new(object[:value], object[:unit]) if object.is_a?(Hash)
      end

      # Takes any possible object and converts it to how it would be
      # stored in the database.
      def mongoize(object)
        case object
        when nil then nil
        when QDM::Quantity then object.mongoize
        when Hash
          object = object.symbolize_keys
          QDM::Quantity.new(object[:value], object[:unit]).mongoize
        else object
        end
      end

      # Converts the object that was supplied to a criteria and converts it
      # into a database friendly form.
      def evolve(object)
        case object
        when QDM::Quantity then object.mongoize
        else object
        end
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
cqm-models-2.0.1 app/models/qdm/basetypes/quantity.rb
cqm-models-3.0.0 app/models/qdm/basetypes/quantity.rb
cqm-models-2.0.0 app/models/qdm/basetypes/quantity.rb
cqm-models-1.1.1.0 app/models/qdm/basetypes/quantity.rb
cqm-models-1.0.3 app/models/qdm/basetypes/quantity.rb
cqm-models-1.0.2 app/models/qdm/basetypes/quantity.rb
cqm-models-1.0.1 app/models/qdm/basetypes/quantity.rb
cqm-models-1.0.0 app/models/qdm/basetypes/quantity.rb
cqm-models-0.8.4 app/models/qdm/basetypes/quantity.rb
cqm-models-0.8.3 app/models/qdm/basetypes/quantity.rb
cqm-models-0.8.2 app/models/qdm/basetypes/quantity.rb
cqm-models-0.8.1 app/models/qdm/basetypes/quantity.rb
cqm-models-0.8.0 app/models/qdm/basetypes/quantity.rb
cqm-models-0.7.7 app/models/qdm/basetypes/quantity.rb
cqm-models-0.7.6 app/models/qdm/basetypes/quantity.rb