Sha256: d3c281d750e6b0877adc6e575a9b7e4a1f124c26cc9ac26570af937b3899e21e

Contents?: true

Size: 1.62 KB

Versions: 2

Compression:

Stored size: 1.62 KB

Contents

module QDM
  # Represents an Interval
  class Interval
    attr_reader :low, :high

    # Low and high are required (at minimum).
    def initialize(low, high, lowClosed = true, highClosed = true)
      @low = low
      @high = high
      @lowClosed = lowClosed
      @highClosed = highClosed
    end

    # Converts an object of this instance into a database friendly value.
    def mongoize
      { low: @low, high: @high, lowClosed: @lowClosed, highClosed: @highClosed }
    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.
      # [ low, high ].
      def demongoize(object)
        return nil unless object
        object = object.symbolize_keys
        QDM::Interval.new(object[:low], object[:high], object[:lowClosed], object[:highClosed]) 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::Interval then object.mongoize
        when Hash
          object = object.symbolize_keys
          QDM::Interval.new(object[:low], object[:high], object[:lowClosed], object[:highClosed]).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::Interval then object.mongoize
        else object
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
cqm-models-0.7.3 app/models/qdm/basetypes/interval.rb
cqm-models-0.7.2 app/models/qdm/basetypes/interval.rb