Class: QDM::DataElement

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

Overview

Represents QDM datatype (parent class of all generated QDM datatype models)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ DataElement

Returns a new instance of DataElement.



19
20
21
22
23
# File 'app/models/qdm/basetypes/data_element.rb', line 19

def initialize(options = {})
  super(options)
  # default id to the mongo ObjectId for this DataElement if it isnt already defined
  self.id = _id.to_s unless id?
end

Class Method Details

.demongoize(object) ⇒ Object

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



147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'app/models/qdm/basetypes/data_element.rb', line 147

def demongoize(object)
  return nil unless object

  object = object.symbolize_keys
  if object.is_a?(Hash)
    data_element = QDM.const_get(object[:_type])
    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.



163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'app/models/qdm/basetypes/data_element.rb', line 163

def mongoize(object)
  case object
  when nil then nil
  when QDM::DataElement then object.mongoize
  when Hash
    object = object.symbolize_keys
    data_element = QDM.const_get(object[:_type])
    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

#code_system_pairsObject

Returns all of the codes on this datatype in a way that is typical to the CQL execution engine.



32
33
34
35
36
# File 'app/models/qdm/basetypes/data_element.rb', line 32

def code_system_pairs
  codes.collect do |code|
    { code: code.code, system: code.system }
  end
end

#codesObject

Helper method that returns the codes on this data element as QDM::Code objects.



40
41
42
# File 'app/models/qdm/basetypes/data_element.rb', line 40

def codes
  dataElementCodes.collect { |code| QDM::Code.demongoize(code) }
end

#get(attribute) ⇒ Object

Returns the attribute requested on the datatype.



26
27
28
# File 'app/models/qdm/basetypes/data_element.rb', line 26

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

#mongoizeObject



44
45
46
47
48
49
50
# File 'app/models/qdm/basetypes/data_element.rb', line 44

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

#shift_dates(seconds) ⇒ Object

Shift all fields that deal with dates by the given value. Given value should be in seconds. Positive values shift forward, negative values shift backwards.



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'app/models/qdm/basetypes/data_element.rb', line 60

def shift_dates(seconds)
  fields.keys.each do |field|
    send(field + '=', (send(field).to_time + seconds.seconds).to_datetime) if send(field).is_a? DateTime
    send(field + '=', send(field).shift_dates(seconds)) if (send(field).is_a? Interval) || (send(field).is_a? DataElement)

    # Special case for facility locations
    if field == 'facilityLocations'
      send(field).each do |facility_location|
        shift_facility_location_dates(facility_location, seconds)
      end
    elsif field == 'facilityLocation'
      facility_location = send(field)
      unless facility_location.nil?
        shift_facility_location_dates(facility_location, seconds)
        send(field + '=', facility_location)
      end
    end
  end
end

#shift_facility_location_dates(facility_location, seconds) ⇒ Object



80
81
82
83
# File 'app/models/qdm/basetypes/data_element.rb', line 80

def shift_facility_location_dates(facility_location, seconds)
  facility_location['locationPeriod'][:low] = (facility_location['locationPeriod'][:low].to_time + seconds).to_datetime
  facility_location['locationPeriod'][:high] = (facility_location['locationPeriod'][:high].to_time + seconds).to_datetime
end

#shift_facility_location_years(facility_location, year_shift) ⇒ Object



139
140
141
# File 'app/models/qdm/basetypes/data_element.rb', line 139

def shift_facility_location_years(facility_location, year_shift)
  facility_location.locationPeriod = facility_location.locationPeriod.shift_years(year_shift)
end

#shift_years(year_shift) ⇒ Object

The necessary reason for this function is to avoid a problem when shifting past a year that is a leap year. February 29th dates are handled by moving back to the 28th in non leap years



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'app/models/qdm/basetypes/data_element.rb', line 88

def shift_years(year_shift)
  fields.keys.each do |field|
    if send(field).is_a?(QDM::Date) || send(field).is_a?(DateTime)
      # Do not shift Time values. They are stored as DateTimes with year 0.
      next if send(field).year.zero?
      raise RangeError, 'Year was shifted after 9999 or before 0001' if send(field).year + year_shift > 9999 || send(field).year + year_shift < 1

      if send(field).month == 2 && send(field).day == 29 && !::Date.leap?(year_shift + send(field).year)
        if send(field).is_a?(QDM::Date)
          shifted_date = send(field)
          shifted_date.year = shifted_date.year + year_shift
          shifted_date.day = 28
          send(field + '=', shifted_date)
        else
          send(field + '=', send(field).change(year: year_shift + send(field).year, day: 28))
        end
      elsif send(field).is_a?(QDM::Date)
        shifted_date = send(field)
        shifted_date.year = shifted_date.year + year_shift
        send(field + '=', shifted_date)
      else
        send(field + '=', send(field).change(year: year_shift + send(field).year))
      end
    end

    if send(field).is_a? FacilityLocation
      facility_location = send(field)
      unless facility_location.nil?
        shift_facility_location_years(facility_location, year_shift)
        send(field + '=', facility_location)
      end
    end

    if field == 'facilityLocations'
      facilityLocations = send(field)
      unless facilityLocations.nil?
        shiftedFacilityLocations = []
        facilityLocations.each do |location|
          # Need to convert to a QDM::FacilityLocation because it is being passed in as a Hash
          facilityLocation = QDM::FacilityLocation.new(location)
          shift_facility_location_years(facilityLocation, year_shift)
          shiftedFacilityLocations << facilityLocation
        end
        send(field + '=', shiftedFacilityLocations)
      end
    end

    send(field + '=', send(field).shift_years(year_shift)) if (send(field).is_a? Interval) || (send(field).is_a? DataElement)
  end
end

#to_json(options = nil) ⇒ Object

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



53
54
55
# File 'app/models/qdm/basetypes/data_element.rb', line 53

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