Class: QDM::Attribute

Inherits:
Object
  • Object
show all
Includes:
Mongoid::Document
Defined in:
app/models/qdm/attributes/attribute.rb

Overview

Represents QDM attribute

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Attribute

Returns a new instance of Attribute.



6
7
8
# File 'app/models/qdm/attributes/attribute.rb', line 6

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

Class Method Details

.demongoize(object) ⇒ Object

Get the object as it was stored in the database, and instantiate this custom class from it.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'app/models/qdm/attributes/attribute.rb', line 31

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

.mongoize(object) ⇒ Object

Takes any possible object and converts it to how it would be stored in the database.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'app/models/qdm/attributes/attribute.rb', line 48

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

Instance Method Details

#get(attribute) ⇒ Object

Returns the attribute requested on the datatype.



11
12
13
# File 'app/models/qdm/attributes/attribute.rb', line 11

def get(attribute)
  send(attribute) if has_attribute?(attribute)
end

#mongoizeObject



15
16
17
18
19
20
21
# File 'app/models/qdm/attributes/attribute.rb', line 15

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

#to_json(options = nil) ⇒ Object

Include ‘_type’ in any JSON output. This is necessary for deserialization.



24
25
26
# File 'app/models/qdm/attributes/attribute.rb', line 24

def to_json(options = nil)
  serializable_hash(methods: :_type).to_json(options)
end