Module: QueryInterface::Client::Model::InstanceMethods
- Defined in:
- lib/query-interface-client/model.rb
Instance Method Summary collapse
- #additional_values ⇒ Object
- #as_json(options = {}) ⇒ Object
- #assign_attributes(data, options = {}) ⇒ Object
- #destroy ⇒ Object
- #dirty? ⇒ Boolean
- #get(path, params = {}) ⇒ Object
- #get_value(name) ⇒ Object
- #initialize(data = {}) ⇒ Object
- #persisted? ⇒ Boolean
- #properties ⇒ Object
- #property_changes ⇒ Object
- #property_values ⇒ Object
- #save(additional_data = {}) ⇒ Object
- #set_value(name, value) ⇒ Object
- #to_attributes(additional_data = {}) ⇒ Object
- #update_attributes(attributes) ⇒ Object
Instance Method Details
#additional_values ⇒ Object
175 176 177 |
# File 'lib/query-interface-client/model.rb', line 175 def additional_values self._additional_values ||= {} end |
#as_json(options = {}) ⇒ Object
199 200 201 202 203 204 |
# File 'lib/query-interface-client/model.rb', line 199 def as_json(={}) 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() end |
#assign_attributes(data, options = {}) ⇒ Object
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 'lib/query-interface-client/model.rb', line 114 def assign_attributes(data, ={}) self.errors.clear() run_conversions = .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.errors.add(name, "Type conversion failed") self.set_value(name, value) else self.set_value(name, converted) end else self.set_value(name, value) end else self.additional_values[name] = value end end end |
#destroy ⇒ Object
210 211 212 213 |
# File 'lib/query-interface-client/model.rb', line 210 def destroy result = self.delete_raw()[:parsed_data] # TODO: handle errors and destroyed flag end |
#dirty? ⇒ Boolean
183 184 185 |
# File 'lib/query-interface-client/model.rb', line 183 def dirty? self.property_changes.size > 0 end |
#get(path, params = {}) ⇒ Object
206 207 208 |
# File 'lib/query-interface-client/model.rb', line 206 def get(path, params={}) self.get_raw(path: path, params: params)[:parsed_data] end |
#get_value(name) ⇒ Object
191 192 193 |
# File 'lib/query-interface-client/model.rb', line 191 def get_value(name) self.property_values[name] end |
#initialize(data = {}) ⇒ Object
102 103 104 105 106 107 108 |
# File 'lib/query-interface-client/model.rb', line 102 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) end |
#persisted? ⇒ Boolean
195 196 197 |
# File 'lib/query-interface-client/model.rb', line 195 def persisted? !!self.id end |
#properties ⇒ Object
110 111 112 |
# File 'lib/query-interface-client/model.rb', line 110 def properties self.class.instance_variable_get(:@properties) end |
#property_changes ⇒ Object
179 180 181 |
# File 'lib/query-interface-client/model.rb', line 179 def property_changes self._property_changes ||= [] end |
#property_values ⇒ Object
171 172 173 |
# File 'lib/query-interface-client/model.rb', line 171 def property_values self._property_values ||= {} end |
#save(additional_data = {}) ⇒ Object
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/query-interface-client/model.rb', line 148 def save(additional_data={}) return false unless (self.errors.empty? && self.valid?) if self.persisted? && self.dirty? result = self.put_raw(params: self.to_attributes(additional_data)) return result[:response].success? elsif !self.persisted? result = self.class.post_raw(params: self.to_attributes(additional_data)) if result[:response].success? self.assign_attributes(result[:parsed_data][:data]) return true else return false end else return true end end |
#set_value(name, value) ⇒ Object
187 188 189 |
# File 'lib/query-interface-client/model.rb', line 187 def set_value(name, value) self.property_values[name] = value end |
#to_attributes(additional_data = {}) ⇒ Object
139 140 141 142 143 144 145 146 |
# File 'lib/query-interface-client/model.rb', line 139 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
166 167 168 169 |
# File 'lib/query-interface-client/model.rb', line 166 def update_attributes(attributes) self.assign_attributes(attributes) self.save end |