Sha256: 5d78655914561ca27a509729be889c18d67c27f91146d00d2a8ff7fda6431c41

Contents?: true

Size: 1007 Bytes

Versions: 1

Compression:

Stored size: 1007 Bytes

Contents

module QDM
  # Represents a QDM/CQL Date
  class Date
    attr_accessor :year, :month, :day

    def initialize(year = nil, month = nil, day = nil)
      @year = year
      @month = month
      @day = day
    end

    # Converts an object of this instance into a database friendly value.
    def mongoize
      "#{format('%04d', year)}-#{format('%02d', month)}-#{format('%02d', day)}"
    end

    class << self
      # Get the string as it was stored in the database, and instantiate
      # this custom class from it.
      def demongoize(date_str)
        return nil unless date_str
        year = date_str[0..3].to_i
        month = date_str[5..6].to_i
        day = date_str[8..10].to_i
        QDM::Date.new(year, month, day)
      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::Date then object.mongoize
        else object
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cqm-models-3.0.0 app/models/qdm/basetypes/date.rb