Sha256: 1d7104516e370996cda44af3d307b8bafc94beb6b9b735e2fb13462ac4e962f8

Contents?: true

Size: 1.51 KB

Versions: 15

Compression:

Stored size: 1.51 KB

Contents

module QDM
  # Represents a Ratio
  class Ratio
    attr_accessor :numerator, :denominator

    # Numerator and denominator are required
    def initialize(numerator, denominator)
      @numerator = numerator
      @denominator = denominator
    end

    # Converts an object of this instance into a database friendly value.
    def mongoize
      { numerator: @numerator, denominator: @denominator, _type: 'QDM::Ratio' }
    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 2 elements used in mongoize, i.e.
      # [ numerator, denominator ].
      def demongoize(object)
        return nil unless object

        object = object.symbolize_keys
        QDM::Ratio.new(object[:numerator], object[:denominator]) 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::Ratio then object.mongoize
        when Hash
          object = object.symbolize_keys
          QDM::Ratio.new(object[:numerator], object[:denominator]).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::Ratio then object.mongoize
        else object
        end
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
cqm-models-4.2.0 app/models/qdm/basetypes/ratio.rb
cqm-models-3.1.2 app/models/qdm/basetypes/ratio.rb
cqm-models-4.1.1 app/models/qdm/basetypes/ratio.rb
cqm-models-4.1.0 app/models/qdm/basetypes/ratio.rb
cqm-models-4.0.2 app/models/qdm/basetypes/ratio.rb
cqm-models-4.0.1 app/models/qdm/basetypes/ratio.rb
cqm-models-4.0.0 app/models/qdm/basetypes/ratio.rb
cqm-models-3.1.1 app/models/qdm/basetypes/ratio.rb
cqm-models-3.1.0 app/models/qdm/basetypes/ratio.rb
cqm-models-3.0.6 app/models/qdm/basetypes/ratio.rb
cqm-models-3.0.5 app/models/qdm/basetypes/ratio.rb
cqm-models-3.0.4 app/models/qdm/basetypes/ratio.rb
cqm-models-3.0.3 app/models/qdm/basetypes/ratio.rb
cqm-models-3.0.2 app/models/qdm/basetypes/ratio.rb
cqm-models-3.0.1 app/models/qdm/basetypes/ratio.rb