Module: QueryInterface::Client::Model::InstanceMethods

Defined in:
lib/query-interface-client/model.rb

Instance Method Summary collapse

Instance Method Details

#additional_valuesObject



173
174
175
# File 'lib/query-interface-client/model.rb', line 173

def additional_values
  self._additional_values ||= {}
end

#as_json(options = {}) ⇒ Object



208
209
210
211
212
213
# File 'lib/query-interface-client/model.rb', line 208

def as_json(options={})
  prop_vals = self.properties.map do |name, property|
    [name, property.run_get_conversion(self.get_value(name))]
  end.to_h
  prop_vals.merge(self.additional_values).as_json(options)
end

#assign_attributes(data, options = {}) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/query-interface-client/model.rb', line 112

def assign_attributes(data, options={})
  run_conversions = options.delete(:run_conversions) {true}
  data.each do |name, value|
    name = name.to_sym
    if self.properties.has_key?(name)
      property = self.properties[name]
      if run_conversions
        begin
          converted = property.run_set_conversion(value)
        rescue
          self.set_value(name, value)
          self.errors.add(name, "Type conversion failed")
        else
          self.set_value(name, converted)
        end
      else
        self.set_value(name, value)
      end
    else
      self.additional_values[name] = value
    end
  end
end

#destroyObject



219
220
221
222
223
# File 'lib/query-interface-client/model.rb', line 219

def destroy
  result = self.delete_raw()[:parsed_data]
  assign_errors(result)
  self._destroyed = !self.errors.any?
end

#destroyed?Boolean

Returns:

  • (Boolean)


177
178
179
# File 'lib/query-interface-client/model.rb', line 177

def destroyed?
  self._destroyed ||= false
end

#dirty?Boolean

Returns:

  • (Boolean)


191
192
193
# File 'lib/query-interface-client/model.rb', line 191

def dirty?
  self.property_changes.size > 0
end

#get(path, params = {}) ⇒ Object



215
216
217
# File 'lib/query-interface-client/model.rb', line 215

def get(path, params={})
  self.get_raw(path: path, params: params)[:parsed_data]
end

#get_value(name) ⇒ Object



200
201
202
# File 'lib/query-interface-client/model.rb', line 200

def get_value(name)
  self.property_values[name]
end

#initialize(data = {}) ⇒ Object



99
100
101
102
103
104
105
106
# File 'lib/query-interface-client/model.rb', line 99

def initialize(data={})
  self.properties.each do |name, property|
    default = self.instance_exec(&property.default)
    self.set_value(name, default)
  end
  self.assign_attributes(data, run_conversions: false)
  self._original_values = self._property_values.deep_dup
end

#persisted?Boolean

Returns:

  • (Boolean)


204
205
206
# File 'lib/query-interface-client/model.rb', line 204

def persisted?
  !!self.id
end

#propertiesObject



108
109
110
# File 'lib/query-interface-client/model.rb', line 108

def properties
  self.class.instance_variable_get(:@properties)
end

#property_changesObject



181
182
183
184
185
186
187
188
189
# File 'lib/query-interface-client/model.rb', line 181

def property_changes
  self.properties.map do |name, property|
    if self.get_value(name) != self._original_values[name]
      [name, {original: self._original_values[name], new: self.get_value(name)}]
    else
      next
    end
  end.compact.to_h
end

#property_valuesObject



169
170
171
# File 'lib/query-interface-client/model.rb', line 169

def property_values
  self._property_values ||= {}
end

#save(additional_data = {}) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/query-interface-client/model.rb', line 145

def save(additional_data={})
  return false unless (self.errors.empty? && self.valid?)
  return true if self.persisted? && !self.dirty?

  if self.persisted?
    result = self.put_raw(params: self.to_attributes(additional_data))
  else
    result = self.class.post_raw(params: self.to_attributes(additional_data))
    if result[:response].success?
      self.assign_attributes(result[:parsed_data][:data])
    end
  end
  assign_errors(result)
  status = result[:response].success?
  self._original_values = self._property_values.deep_dup if status

  status
end

#set_value(name, value) ⇒ Object



195
196
197
198
# File 'lib/query-interface-client/model.rb', line 195

def set_value(name, value)
  self.errors.delete(name)
  self.property_values[name] = value
end

#to_attributes(additional_data = {}) ⇒ Object



136
137
138
139
140
141
142
143
# File 'lib/query-interface-client/model.rb', line 136

def to_attributes(additional_data={})
  data = {}
  properties = self.properties.map do |name, property|
    [name, property.run_get_conversion(self.get_value(name))] if (property.update && !property.primary)
  end.compact.to_h
  data[self.class.name.underscore.to_sym] = properties.merge(additional_data)
  data
end

#update_attributes(attributes) ⇒ Object



164
165
166
167
# File 'lib/query-interface-client/model.rb', line 164

def update_attributes(attributes)
  self.assign_attributes(attributes)
  self.save
end