Class: Trophonius::Record
- Inherits:
-
Hash
- Object
- Hash
- Trophonius::Record
- Defined in:
- lib/trophonius_record.rb
Overview
This class will hold a singular record
A Record is contained in a RecordSet and has methods to retrieve data from the fields inside the Record-hash
Instance Attribute Summary collapse
-
#layout_name ⇒ Object
Returns the value of attribute layout_name.
-
#model_name ⇒ Object
Returns the value of attribute model_name.
-
#modifiable_fields ⇒ Object
Returns the value of attribute modifiable_fields.
-
#modified_fields ⇒ Object
Returns the value of attribute modified_fields.
-
#record_id ⇒ Object
Returns the value of attribute record_id.
Instance Method Summary collapse
- #[]=(field, new_val) ⇒ Object
-
#delete ⇒ True
Deletes the corresponding record from FileMaker Throws a FileMaker error if save failed.
-
#initialize ⇒ Record
constructor
Initializes a new Record.
- #method_missing(method, *args, &block) ⇒ Object
-
#save ⇒ True
Saves the last changes made to the Record to FileMaker.
-
#update(fieldData) ⇒ True
Changes and saves the corresponding record in FileMaker Throws a FileMaker error if save failed.
-
#upload(container_name:, container_repetition: 1, file:) ⇒ True
Uploads a file to a container field of the record Throws a FileMaker error if upload failed.
Constructor Details
#initialize ⇒ Record
Initializes a new Record
15 16 17 18 |
# File 'lib/trophonius_record.rb', line 15 def initialize @modifiable_fields = {} @modified_fields = {} end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 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 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/trophonius_record.rb', line 25 def method_missing(method, *args, &block) if ActiveSupport::Inflector.pluralize(method).to_s == method.to_s model = ActiveSupport::Inflector.constantize(ActiveSupport::Inflector.classify(ActiveSupport::Inflector.singularize(method))) pk_model = ActiveSupport::Inflector.constantize(ActiveSupport::Inflector.classify(ActiveSupport::Inflector.singularize(model_name))) if model.belongs_to_relations[ActiveSupport::Inflector.parameterize(model_name).to_sym] relation = model.belongs_to_relations[ActiveSupport::Inflector.parameterize(model_name).to_sym] layout = model.layout_name model.create_translations if model.translations.keys.empty? url = URI( URI.escape( "http#{Trophonius.config.ssl == true ? 's' : ''}://#{Trophonius.config.host}/fmi/data/v1/databases/#{ Trophonius.config.database }/layouts/#{layout}/_find" ) ) if model.translations.key?(relation[:foreign_key]) foreign_key_field = model.translations[relation[:foreign_key]].to_s else foreign_key_field = relation[:foreign_key].to_s end if pk_model.translations.key?(relation[:primary_key]) primary_key_field = pk_model.translations[relation[:primary_key]].to_s else primary_key_field = relation[:primary_key].to_s end body = { query: [{ foreign_key_field => self[primary_key_field].to_s }], limit: 100_000 }.to_json response = Request.make_request(url, "Bearer #{Request.get_token}", 'post', body) if response['messages'][0]['code'] != '0' if response['messages'][0]['code'] == '101' || response['messages'][0]['code'] == '401' resp = RecordSet.new(layout, model.non_modifiable_fields) return resp else if response['messages'][0]['code'] == '102' results = Request.retrieve_first(layout) if results['messages'][0]['code'] != '0' Error.throw_error('102') else r_results = results['response']['data'] ret_val = r_results.empty? ? Error.throw_error('102') : r_results[0]['fieldData'] query_keys = [foreign_key_field] Error.throw_error('102', (query_keys - ret_val.keys.map(&:downcase)).flatten.join(', '), layout) end end Error.throw_error(response['messages'][0]['code']) end else r_results = response['response']['data'] ret_val = RecordSet.new(layout, model.non_modifiable_fields) r_results.each do |r| hash = model.build_result(r) ret_val << hash end @response = ret_val return @response end end elsif ActiveSupport::Inflector.constantize(ActiveSupport::Inflector.classify(ActiveSupport::Inflector.singularize(method))).respond_to?('first') fk_model = ActiveSupport::Inflector.constantize(ActiveSupport::Inflector.classify(ActiveSupport::Inflector.singularize(model_name))) pk_model = ActiveSupport::Inflector.constantize(ActiveSupport::Inflector.classify(ActiveSupport::Inflector.singularize(method))) if pk_model.has_many_relations[ActiveSupport::Inflector.parameterize(ActiveSupport::Inflector.pluralize(model_name)).to_sym] relation = pk_model.has_many_relations[ActiveSupport::Inflector.parameterize(ActiveSupport::Inflector.pluralize(model_name)).to_sym] layout = pk_model.layout_name pk_model.create_translations if pk_model.translations.keys.empty? url = URI( URI.escape( "http#{Trophonius.config.ssl == true ? 's' : ''}://#{Trophonius.config.host}/fmi/data/v1/databases/#{ Trophonius.config.database }/layouts/#{layout}/_find" ) ) if fk_model.translations.key?(relation[:foreign_key]) foreign_key_field = fk_model.translations[relation[:foreign_key]].to_s else foreign_key_field = relation[:foreign_key].to_s end if pk_model.translations.key?(relation[:primary_key]) primary_key_field = pk_model.translations[relation[:primary_key]].to_s else primary_key_field = relation[:primary_key].to_s end body = { query: [{ primary_key_field => self[foreign_key_field].to_s }], limit: 1 }.to_json response = Request.make_request(url, "Bearer #{Request.get_token}", 'post', body) if response['messages'][0]['code'] != '0' if response['messages'][0]['code'] == '101' || response['messages'][0]['code'] == '401' resp = RecordSet.new(layout, pk_model.non_modifiable_fields) return resp else if response['messages'][0]['code'] == '102' results = Request.retrieve_first(layout) if results['messages'][0]['code'] != '0' Error.throw_error('102') else r_results = results['response']['data'] ret_val = r_results.empty? ? Error.throw_error('102') : r_results[0]['fieldData'] query_keys = [primary_key_field] Error.throw_error('102', (query_keys - ret_val.keys.map(&:downcase)).flatten.join(', '), layout) end end Error.throw_error(response['messages'][0]['code']) end else r_results = response['response']['data'] ret_val = RecordSet.new(layout, pk_model.non_modifiable_fields) r_results.each do |r| hash = pk_model.build_result(r) ret_val << hash end @response = ret_val return @response.first end end else super end end |
Instance Attribute Details
#layout_name ⇒ Object
Returns the value of attribute layout_name.
11 12 13 |
# File 'lib/trophonius_record.rb', line 11 def layout_name @layout_name end |
#model_name ⇒ Object
Returns the value of attribute model_name.
11 12 13 |
# File 'lib/trophonius_record.rb', line 11 def model_name @model_name end |
#modifiable_fields ⇒ Object
Returns the value of attribute modifiable_fields.
11 12 13 |
# File 'lib/trophonius_record.rb', line 11 def modifiable_fields @modifiable_fields end |
#modified_fields ⇒ Object
Returns the value of attribute modified_fields.
11 12 13 |
# File 'lib/trophonius_record.rb', line 11 def modified_fields @modified_fields end |
#record_id ⇒ Object
Returns the value of attribute record_id.
11 12 13 |
# File 'lib/trophonius_record.rb', line 11 def record_id @record_id end |
Instance Method Details
#[]=(field, new_val) ⇒ Object
20 21 22 23 |
# File 'lib/trophonius_record.rb', line 20 def []=(field, new_val) modifiable_fields[field] = new_val super end |
#delete ⇒ True
Deletes the corresponding record from FileMaker Throws a FileMaker error if save failed
179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/trophonius_record.rb', line 179 def delete url = URI( URI.escape( "http#{Trophonius.config.ssl == true ? 's' : ''}://#{Trophonius.config.host}/fmi/data/v1/databases/#{ Trophonius.config.database }/layouts/#{layout_name}/records/#{record_id}" ) ) response = Request.make_request(url, "Bearer #{Request.get_token}", 'delete', '{}') response['messages'][0]['code'] != '0' ? Error.throw_error(response['messages'][0]['code']) : true end |
#save ⇒ True
Saves the last changes made to the Record to FileMaker. Throws a FileMaker error if save failed
160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/trophonius_record.rb', line 160 def save url = URI( URI.escape( "http#{Trophonius.config.ssl == true ? 's' : ''}://#{Trophonius.config.host}/fmi/data/v1/databases/#{ Trophonius.config.database }/layouts/#{layout_name}/records/#{record_id}" ) ) body = "{\"fieldData\": #{modified_fields.to_json}}" response = Request.make_request(url, "Bearer #{Request.get_token}", 'patch', body) response['messages'][0]['code'] != '0' ? Error.throw_error(response['messages'][0]['code']) : true end |
#update(fieldData) ⇒ True
Changes and saves the corresponding record in FileMaker Throws a FileMaker error if save failed
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
# File 'lib/trophonius_record.rb', line 199 def update(fieldData) url = URI( URI.escape( "http#{Trophonius.config.ssl == true ? 's' : ''}://#{Trophonius.config.host}/fmi/data/v1/databases/#{ Trophonius.config.database }/layouts/#{layout_name}/records/#{record_id}" ) ) fieldData.keys.each { |field| modifiable_fields[field] = fieldData[field] } body = "{\"fieldData\": #{fieldData.to_json}}" response = Request.make_request(url, "Bearer #{Request.get_token}", 'patch', body) if response['messages'][0]['code'] != '0' if response['messages'][0]['code'] == '102' results = Request.retrieve_first(layout_name) if results['messages'][0]['code'] != '0' Error.throw_error('102') else r_results = results['response']['data'] ret_val = r_results.empty? ? Error.throw_error('102') : r_results[0]['fieldData'] Error.throw_error('102', (fieldData.keys.map(&:downcase) - ret_val.keys.map(&:downcase)).flatten.join(', '), layout_name) end end Error.throw_error(response['messages'][0]['code']) else true end end |
#upload(container_name:, container_repetition: 1, file:) ⇒ True
Uploads a file to a container field of the record Throws a FileMaker error if upload failed
237 238 239 240 241 242 243 244 245 246 247 248 249 |
# File 'lib/trophonius_record.rb', line 237 def upload(container_name:, container_repetition: 1, file:) url = URI( URI.escape( "http#{Trophonius.config.ssl == true ? 's' : ''}://#{Trophonius.config.host}/fmi/data/v1/databases/#{ Trophonius.config.database }/layouts/#{layout_name}/records/#{record_id}/containers/#{container_name}/#{container_repetition}" ) ) response = Request.upload_file_request(url, "Bearer #{Request.get_token}", file) response['messages'][0]['code'] != '0' ? Error.throw_error(response['messages'][0]['code']) : true end |