Class: Trophonius::Model
- Inherits:
-
Object
- Object
- Trophonius::Model
- Defined in:
- lib/trophonius_model.rb
Overview
This class will retrieve the records from the FileMaker database and build a RecordSet filled with Record objects. One Record object represents a record in FileMaker.
Instance Attribute Summary collapse
-
#configuration ⇒ Object
readonly
Returns the value of attribute configuration.
-
#current_query ⇒ Object
Returns the value of attribute current_query.
Class Method Summary collapse
-
.all(sort: {}) ⇒ RecordSet
Retrieve the first 10000000 records from FileMaker from the context of the Model.
-
.build_result(result) ⇒ Record
Builds the resulting Record.
-
.config(configuration) ⇒ Object
Sets up the configuration for the model.
-
.create(fieldData) ⇒ Record
Creates and saves a record in FileMaker.
-
.create_translations ⇒ Hash
creates Rails -> FileMaker field translations by requesting the first record.
-
.delete(record_id) ⇒ Boolean
Deletes a record from FileMaker.
-
.edit(record_id, fieldData) ⇒ Boolean
Edits a record in FileMaker.
-
.find(record_id) ⇒ Record
Finds and returns a Record corresponding to the record_id.
-
.find_by(fieldData) ⇒ Record
Finds and returns the first Record containing fitting the find request.
-
.first ⇒ Record
Retrieve the first record from FileMaker from the context of the Model.
-
.layout_name ⇒ String
Returns the FileMaker layout this Model corresponds to.
- .method_missing(method, *args, &block) ⇒ Object
-
.non_modifiable_fields ⇒ [Array]
Returns the fields that FileMaker won’t allow us to modify.
-
.paginate(page, limit) ⇒ Trophonius::Model
Limits the found record set.
-
.run_script(script: '', scriptparameter: '') ⇒ String
Runs a FileMaker script from the context of the Model.
-
.translations ⇒ Hash
Returns the translations of the fields.
-
.where(fieldData) ⇒ Trophonius::Model
Finds all records in FileMaker corresponding to the requested query.
Instance Method Summary collapse
-
#initialize(config:) ⇒ Model
constructor
A new instance of Model.
- #method_missing(method, *args, &block) ⇒ Object
-
#where(fieldData) ⇒ Trophonius::Model
Finds all records in FileMaker corresponding to the requested query This method is created to enable where chaining.
Constructor Details
#initialize(config:) ⇒ Model
Returns a new instance of Model.
14 15 16 17 18 |
# File 'lib/trophonius_model.rb', line 14 def initialize(config:) @configuration = config @offset = '' @limit = '' end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/trophonius_model.rb', line 88 def method_missing(method, *args, &block) if @current_query.respond_to?(method) args << self @current_query.send(method, args) elsif @current_query.response.respond_to?(method) ret_val = @current_query.run_query(method, *args, &block) @limit = '' @offset = '' return ret_val end end |
Instance Attribute Details
#configuration ⇒ Object (readonly)
Returns the value of attribute configuration.
11 12 13 |
# File 'lib/trophonius_model.rb', line 11 def configuration @configuration end |
#current_query ⇒ Object
Returns the value of attribute current_query.
12 13 14 |
# File 'lib/trophonius_model.rb', line 12 def current_query @current_query end |
Class Method Details
.all(sort: {}) ⇒ RecordSet
Retrieve the first 10000000 records from FileMaker from the context of the Model.
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 |
# File 'lib/trophonius_model.rb', line 388 def self.all(sort: {}) results = Request.retrieve_all(layout_name, sort) count = results['response']['scriptResult'].to_i unless @limit.empty? || @offset.empty? url = URI( "http#{Trophonius.config.ssl == true ? 's' : ''}://#{Trophonius.config.host}/fmi/data/v1/databases/#{ Trophonius.config.database }/layouts/#{layout_name}/records?_offset=#{@offset}&_limit=#{@limit}" ) else url = URI( "http#{Trophonius.config.ssl == true ? 's' : ''}://#{Trophonius.config.host}/fmi/data/v1/databases/#{ Trophonius.config.database }/layouts/#{layout_name}/records?_limit=#{count == 0 ? 1_000_000 : count}" ) end @limit = '' @offset = '' results = Request.make_request(url, "Bearer #{Request.get_token}", 'get', '{}') if results['messages'][0]['code'] != '0' Error.throw_error(results['messages'][0]['code']) else r_results = results['response']['data'] ret_val = RecordSet.new(self.layout_name, self.non_modifiable_fields) r_results.each do |r| hash = build_result(r) ret_val << hash end ret_val.result_count = count return ret_val end end |
.build_result(result) ⇒ Record
Builds the resulting Record
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 |
# File 'lib/trophonius_model.rb', line 295 def self.build_result(result) hash = Trophonius::Record.new hash.record_id = result['recordId'] hash.layout_name = layout_name result['fieldData'].keys.each do |key| # unless key[/\s/] || key[/\W/] @configuration.translations.merge!( { "#{ActiveSupport::Inflector.parameterize(ActiveSupport::Inflector.underscore(key.to_s), separator: '_').downcase}" => "#{key}" } ) hash.send(:define_singleton_method, ActiveSupport::Inflector.parameterize(ActiveSupport::Inflector.underscore(key.to_s), separator: '_')) do hash[key] end unless non_modifiable_fields&.include?(key) @configuration.all_fields.merge!( ActiveSupport::Inflector.parameterize(ActiveSupport::Inflector.underscore(key.to_s), separator: '_').downcase => ActiveSupport::Inflector.parameterize(ActiveSupport::Inflector.underscore(key.to_s), separator: '_') ) hash.send( :define_singleton_method, "#{ActiveSupport::Inflector.parameterize(ActiveSupport::Inflector.underscore(key.to_s), separator: '_')}=" ) do |new_val| hash[key] = new_val hash.modifiable_fields[key] = new_val hash.modified_fields[key] = new_val end end # end hash.merge!({ key => result['fieldData'][key] }) hash.modifiable_fields.merge!({ key => result['fieldData'][key] }) unless non_modifiable_fields&.include?(key) end result['portalData'].keys.each do |key| unless key[/\s/] || key[/\W/] hash.send(:define_singleton_method, ActiveSupport::Inflector.parameterize(ActiveSupport::Inflector.underscore(key.to_s), separator: '_')) do hash[key] end end result['portalData'][key].each_with_index do |inner_hash| inner_hash.keys.each do |inner_key| inner_method = ActiveSupport::Inflector.parameterize(ActiveSupport::Inflector.underscore(inner_key.gsub(/\w+::/, '').to_s), separator: '_') unless inner_method[/\s/] || inner_method[/\W/] inner_hash.send(:define_singleton_method, inner_method.to_s) { inner_hash[inner_key] } inner_hash.send(:define_singleton_method, 'record_id') { inner_hash['recordId'] } end end end hash.merge!({ key => result['portalData'][key] }) end return hash end |
.config(configuration) ⇒ Object
Sets up the configuration for the model.
25 26 27 28 29 30 31 32 33 |
# File 'lib/trophonius_model.rb', line 25 def self.config(configuration) @configuration ||= Configuration.new @configuration.layout_name = configuration[:layout_name] @configuration.non_modifiable_fields = configuration[:non_modifiable_fields] @configuration.all_fields = {} @configuration.translations = {} @offset = '' @limit = '' end |
.create(fieldData) ⇒ Record
Creates and saves a record in FileMaker
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/trophonius_model.rb', line 131 def self.create(fieldData) url = URI( "http#{Trophonius.config.ssl == true ? 's' : ''}://#{Trophonius.config.host}/fmi/data/v1/databases/#{Trophonius.config.database}/layouts/#{ layout_name }/records" ) new_field_data = {} create_translations if @configuration.translations.keys.empty? fieldData.keys.each do |k| if @configuration.translations.keys.include?(k.to_s) new_field_data.merge!({ "#{@configuration.translations[k.to_s]}" => fieldData[k] }) else new_field_data.merge!({ "#{k}" => fieldData[k] }) end end body = "{\"fieldData\": #{new_field_data.to_json}}" response = Request.make_request(url, "Bearer #{Request.get_token}", 'post', 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', (new_field_data.keys.map(&:downcase) - ret_val.keys.map(&:downcase)).flatten.join(', '), layout_name) end end Error.throw_error(response['messages'][0]['code']) else url = URI( "http#{Trophonius.config.ssl == true ? 's' : ''}://#{Trophonius.config.host}/fmi/data/v1/databases/#{ Trophonius.config.database }/layouts/#{layout_name}/records/#{response['response']['recordId']}" ) ret_val = build_result(Request.make_request(url, "Bearer #{Request.get_token}", 'get', '{}')['response']['data'][0]) ret_val.send(:define_singleton_method, 'result_count') { 1 } return ret_val end end |
.create_translations ⇒ Hash
creates Rails -> FileMaker field translations by requesting the first record
76 77 78 79 |
# File 'lib/trophonius_model.rb', line 76 def self.create_translations self.first @configuration.translations end |
.delete(record_id) ⇒ Boolean
Deletes a record from FileMaker
245 246 247 248 249 250 251 252 253 254 255 256 257 258 |
# File 'lib/trophonius_model.rb', line 245 def self.delete(record_id) url = URI( "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', '{}') if response['messages'][0]['code'] != '0' Error.throw_error(response['messages'][0]['code']) else return true end end |
.edit(record_id, fieldData) ⇒ Boolean
Edits a record in FileMaker
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 |
# File 'lib/trophonius_model.rb', line 268 def self.edit(record_id, fieldData) url = URI( "http#{Trophonius.config.ssl == true ? 's' : ''}://#{Trophonius.config.host}/fmi/data/v1/databases/#{Trophonius.config.database}/layouts/#{ layout_name }/records/#{record_id}" ) new_field_data = {} create_translations if @configuration.translations.keys.empty? fieldData.keys.each do |k| if @configuration.translations.keys.include?(k.to_s) new_field_data.merge!({ "#{@configuration.translations[k.to_s]}" => fieldData[k] }) else new_field_data.merge!({ "#{k}" => fieldData[k] }) end end body = "{\"fieldData\": #{new_field_data.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 |
.find(record_id) ⇒ Record
Finds and returns a Record corresponding to the record_id
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'lib/trophonius_model.rb', line 222 def self.find(record_id) url = URI( "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}", 'get', '{}') if response['messages'][0]['code'] != '0' Error.throw_error(response['messages'][0]['code'], record_id) else ret_val = build_result(response['response']['data'][0]) ret_val.send(:define_singleton_method, 'result_count') { 1 } return ret_val end end |
.find_by(fieldData) ⇒ Record
Finds and returns the first Record containing fitting the find request
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
# File 'lib/trophonius_model.rb', line 181 def self.find_by(fieldData) url = URI( "http#{Trophonius.config.ssl == true ? 's' : ''}://#{Trophonius.config.host}/fmi/data/v1/databases/#{Trophonius.config.database}/layouts/#{ self.layout_name }/_find?_limit=1" ) new_field_data = {} create_translations if @configuration.translations.keys.empty? fieldData.keys.each do |k| if @configuration.translations.keys.include?(k.to_s) new_field_data.merge!({ "#{@configuration.translations[k.to_s]}" => fieldData[k] }) else new_field_data.merge!({ "#{k}" => fieldData[k] }) end end body = { query: [new_field_data], limit: '100000' }.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' return RecordSet.new(self.layout_name, self.non_modifiable_fields) end Error.throw_error(response['messages'][0]['code']) else r_results = response['response']['data'] ret_val = RecordSet.new(self.layout_name, self.non_modifiable_fields) r_results.each do |r| hash = build_result(r) ret_val << hash end return ret_val.first end end |
.first ⇒ Record
Retrieve the first record from FileMaker from the context of the Model.
350 351 352 353 354 355 356 357 358 359 360 |
# File 'lib/trophonius_model.rb', line 350 def self.first results = Request.retrieve_first(layout_name) if results['messages'][0]['code'] != '0' Error.throw_error(results['messages'][0]['code']) else r_results = results['response']['data'] ret_val = r_results.empty? ? Trophonius::Record.new : build_result(r_results[0]) ret_val.send(:define_singleton_method, 'result_count') { r_results.empty? ? 0 : 1 } return ret_val end end |
.layout_name ⇒ String
Returns the FileMaker layout this Model corresponds to
52 53 54 |
# File 'lib/trophonius_model.rb', line 52 def self.layout_name @configuration.layout_name end |
.method_missing(method, *args, &block) ⇒ Object
81 82 83 84 85 86 |
# File 'lib/trophonius_model.rb', line 81 def self.method_missing(method, *args, &block) new_instance = Trophonius::Model.new(config: @configuration) new_instance.current_query = Trophonius::Query.new(trophonius_model: self, limit: @limit, offset: @offset) args << new_instance new_instance.current_query.send(method, args) if new_instance.current_query.respond_to?(method) end |
.non_modifiable_fields ⇒ [Array]
Returns the fields that FileMaker won’t allow us to modify
60 61 62 |
# File 'lib/trophonius_model.rb', line 60 def self.non_modifiable_fields @configuration.non_modifiable_fields end |
.paginate(page, limit) ⇒ Trophonius::Model
Limits the found record set.
42 43 44 45 46 |
# File 'lib/trophonius_model.rb', line 42 def self.paginate(page, limit) @offset = ((page * limit - limit) + 1).to_s @limit = limit.to_s self end |
.run_script(script: '', scriptparameter: '') ⇒ String
Runs a FileMaker script from the context of the Model.
370 371 372 373 374 375 376 377 378 379 380 |
# File 'lib/trophonius_model.rb', line 370 def self.run_script(script: '', scriptparameter: '') result = Request.run_script(script, scriptparameter, layout_name) if result['messages'][0]['code'] != '0' Error.throw_error(result['messages'][0]['code']) elsif result['response']['scriptResult'] == '403' Error.throw_error(403) else ret_val = result['response']['scriptResult'] return ret_val end end |
.translations ⇒ Hash
Returns the translations of the fields
68 69 70 |
# File 'lib/trophonius_model.rb', line 68 def self.translations @configuration.translations end |
.where(fieldData) ⇒ Trophonius::Model
Finds all records in FileMaker corresponding to the requested query
105 106 107 108 109 110 |
# File 'lib/trophonius_model.rb', line 105 def self.where(fieldData) new_instance = Trophonius::Model.new(config: @configuration) new_instance.current_query = Trophonius::Query.new(trophonius_model: self, limit: @limit, offset: @offset) new_instance.current_query.build_query[0].merge!(fieldData) new_instance end |
Instance Method Details
#where(fieldData) ⇒ Trophonius::Model
Finds all records in FileMaker corresponding to the requested query This method is created to enable where chaining
119 120 121 122 |
# File 'lib/trophonius_model.rb', line 119 def where(fieldData) @current_query.build_query[0].merge!(fieldData) self end |