Sha256: f2c8f1b64fedfcd4fb06adc2df2456e61dbc6628e10486f23d8a52190c06921f

Contents?: true

Size: 1.78 KB

Versions: 15

Compression:

Stored size: 1.78 KB

Contents

module QDM
  # Represents QDM attribute
  class Attribute
    include Mongoid::Document

    def initialize(options = {})
      super(options)
    end

    # Returns the attribute requested on the datatype.
    def get(attribute)
      send(attribute) if has_attribute?(attribute)
    end

    def mongoize
      json_representation = {}
      attribute_names.each do |field|
        json_representation[field] = send(field).mongoize
      end
      json_representation
    end

    # Include '_type' in any JSON output. This is necessary for deserialization.
    def to_json(options = nil)
      serializable_hash(methods: :_type).to_json(options)
    end

    class << self
      # Get the object as it was stored in the database, and instantiate
      # this custom class from it.
      def demongoize(object)
        return nil unless object

        object = object.symbolize_keys
        if object.is_a?(Hash)
          # This will turn the object into the concrete type eg: facilityLocation
          data_element = QDM.const_get(object[:_type]).new
          data_element.attribute_names.each do |field|
            data_element.send(field + '=', object[field.to_sym])
          end
          data_element
        else object
        end
      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::Attribute then object.mongoize
        when Hash
          object = object.symbolize_keys
          data_element = QDM.const_get(object[:_type]).new
          data_element.attribute_names.each do |field|
            data_element.send(field + '=', object[field.to_sym])
          end
          data_element.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/attributes/attribute.rb
cqm-models-3.1.2 app/models/qdm/attributes/attribute.rb
cqm-models-4.1.1 app/models/qdm/attributes/attribute.rb
cqm-models-4.1.0 app/models/qdm/attributes/attribute.rb
cqm-models-4.0.2 app/models/qdm/attributes/attribute.rb
cqm-models-4.0.1 app/models/qdm/attributes/attribute.rb
cqm-models-4.0.0 app/models/qdm/attributes/attribute.rb
cqm-models-3.1.1 app/models/qdm/attributes/attribute.rb
cqm-models-3.1.0 app/models/qdm/attributes/attribute.rb
cqm-models-3.0.6 app/models/qdm/attributes/attribute.rb
cqm-models-3.0.5 app/models/qdm/attributes/attribute.rb
cqm-models-3.0.4 app/models/qdm/attributes/attribute.rb
cqm-models-3.0.3 app/models/qdm/attributes/attribute.rb
cqm-models-3.0.2 app/models/qdm/attributes/attribute.rb
cqm-models-3.0.1 app/models/qdm/attributes/attribute.rb