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(attributes) ⇒ 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_data(data) ⇒ Object
- #set_value(name, value) ⇒ Object
- #to_attributes(additional_data = {}) ⇒ Object
- #update_attributes(attributes) ⇒ Object
Instance Method Details
#additional_values ⇒ Object
166 167 168 |
# File 'lib/query-interface-client/model.rb', line 166 def additional_values self._additional_values ||= {} end |
#as_json(options = {}) ⇒ Object
190 191 192 193 194 195 |
# File 'lib/query-interface-client/model.rb', line 190 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(attributes) ⇒ Object
153 154 155 156 157 158 159 160 |
# File 'lib/query-interface-client/model.rb', line 153 def assign_attributes(attributes) attributes.each do |name, value| method = "#{name}=".to_sym if self.respond_to?(method) self.send(method, value) end end end |
#destroy ⇒ Object
201 202 203 204 |
# File 'lib/query-interface-client/model.rb', line 201 def destroy result = self.delete_raw()[:parsed_data] # TODO: handle errors and destroyed flag end |
#dirty? ⇒ Boolean
174 175 176 |
# File 'lib/query-interface-client/model.rb', line 174 def dirty? self.property_changes.size > 0 end |
#get(path, params = {}) ⇒ Object
197 198 199 |
# File 'lib/query-interface-client/model.rb', line 197 def get(path, params={}) self.get_raw(path: path, params: params)[:parsed_data] end |
#get_value(name) ⇒ Object
182 183 184 |
# File 'lib/query-interface-client/model.rb', line 182 def get_value(name) self.property_values[name] end |
#initialize(data = {}) ⇒ Object
96 97 98 |
# File 'lib/query-interface-client/model.rb', line 96 def initialize(data={}) self.set_data(data) end |
#persisted? ⇒ Boolean
186 187 188 |
# File 'lib/query-interface-client/model.rb', line 186 def persisted? !!self.id end |
#properties ⇒ Object
100 101 102 |
# File 'lib/query-interface-client/model.rb', line 100 def properties self.class.instance_variable_get(:@properties) end |
#property_changes ⇒ Object
170 171 172 |
# File 'lib/query-interface-client/model.rb', line 170 def property_changes self._property_changes ||= [] end |
#property_values ⇒ Object
162 163 164 |
# File 'lib/query-interface-client/model.rb', line 162 def property_values self._property_values ||= {} end |
#save(additional_data = {}) ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/query-interface-client/model.rb', line 131 def save(additional_data={}) 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.set_data(result[:parsed_data][:data]) return true else return false end else return true end end |
#set_data(data) ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/query-interface-client/model.rb', line 104 def set_data(data) data.each do |name, value| name = name.to_sym if self.properties.has_key?(name) property = self.properties[name] self.set_value(name, property.run_set_conversion(value)) else self.additional_values[name] = value end end # Initialize missing fields to whatever their default is (self.properties.keys - data.keys.map(&:to_sym)).each do |name| property = self.properties[name] default = property.default self.set_value(name, default.is_a?(Enumerable) ? default.dup : default) end end |
#set_value(name, value) ⇒ Object
178 179 180 |
# File 'lib/query-interface-client/model.rb', line 178 def set_value(name, value) self.property_values[name] = value end |
#to_attributes(additional_data = {}) ⇒ Object
122 123 124 125 126 127 128 129 |
# File 'lib/query-interface-client/model.rb', line 122 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
148 149 150 151 |
# File 'lib/query-interface-client/model.rb', line 148 def update_attributes(attributes) self.assign_attributes(attributes) self.save end |