Class: DataMapper::Adapters::FilemakerAdapter
- Inherits:
-
AbstractAdapter
- Object
- AbstractAdapter
- DataMapper::Adapters::FilemakerAdapter
- Defined in:
- lib/dm-filemaker-adapter/adapter.rb
Defined Under Namespace
Modules: ModelMethods, ResourceMethods
Constant Summary collapse
- VERSION =
DataMapper::FilemakerAdapter::VERSION
Class Attribute Summary collapse
-
.fmresultset_template_path ⇒ Object
Returns the value of attribute fmresultset_template_path.
Instance Method Summary collapse
-
#aggregate(query) ⇒ Object
Takes a query and returns number of matched records.
-
#create(resources) ⇒ Integer
Persists one or many new resources.
-
#delete(collection) ⇒ Integer
Deletes one or many existing resources.
-
#layout(model) ⇒ Object
Create fmp layout object from model object.
- #prepare_fmp_attributes(attributes, *args) ⇒ Object
-
#read(query) ⇒ Enumerable<Hash>
Reads one or many resources from a datastore.
-
#update(attributes, collection) ⇒ Integer
Updates one or many existing resources.
Class Attribute Details
.fmresultset_template_path ⇒ Object
Returns the value of attribute fmresultset_template_path.
9 10 11 |
# File 'lib/dm-filemaker-adapter/adapter.rb', line 9 def fmresultset_template_path @fmresultset_template_path end |
Instance Method Details
#aggregate(query) ⇒ Object
Takes a query and returns number of matched records. An empty query will return the total record count
75 76 77 78 79 80 81 82 83 84 |
# File 'lib/dm-filemaker-adapter/adapter.rb', line 75 def aggregate(query) query.model.last_query = query #y query _layout = layout(query.model) opts = query. opts[:template] = self.class.fmresultset_template_path prms = fmp_query(query.conditions) #[prms.empty? ? _layout.all(:max_records=>0).foundset_count : _layout.count(prms)] [prms.empty? ? _layout.view.total_count : _layout.count(prms)] end |
#create(resources) ⇒ Integer
Persists one or many new resources
Adapters provide specific implementation of this method
29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/dm-filemaker-adapter/adapter.rb', line 29 def create(resources) resources[0].model.last_query = resources counter = 0 resources.each do |resource| fm_params = prepare_fmp_attributes(resource.dirty_attributes) rslt = layout(resource.model).create(fm_params, :template=>self.class.fmresultset_template_path) merge_fmp_response(resource, rslt[0]) counter +=1 end counter end |
#delete(collection) ⇒ Integer
Deletes one or many existing resources
Adapters provide specific implementation of this method
129 130 131 132 133 134 135 136 |
# File 'lib/dm-filemaker-adapter/adapter.rb', line 129 def delete(collection) counter = 0 collection.each do |resource| rslt = layout(resource.model).delete(resource.record_id, :template=>self.class.fmresultset_template_path) counter +=1 end counter end |
#layout(model) ⇒ Object
Create fmp layout object from model object.
143 144 145 146 |
# File 'lib/dm-filemaker-adapter/adapter.rb', line 143 def layout(model) #Rfm.layout(model.storage_name, options.symbolize_keys) #query.repository.adapter.options.symbolize_keys) model.layout end |
#prepare_fmp_attributes(attributes, *args) ⇒ Object
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 173 174 175 |
# File 'lib/dm-filemaker-adapter/adapter.rb', line 148 def prepare_fmp_attributes(attributes, *args) = args.last.is_a?(Hash) ? args.pop : {} prepend, append = [:prepend], [:append] fm_attributes = {} #puts "PREPARE FMP ATTRIBUTES" #y attributes attributes_as_fields(attributes).each do |key, val| #puts "EACH ATTRIBUTE class #{val.class}" #puts "EACH ATTRIBUTE value #{val}" new_val = val && [val.is_a?(Fixnum) ? val : val.dup].flatten.inject([]) do |r, v| #puts "INJECTING v" #puts v new_v = v.respond_to?(:_to_fm) ? v._to_fm : v #puts "CONVERTING VAL #{new_val} TO STRING" new_v = new_v.to_s #puts "PREPENDING #{new_v} with '#{prepend}'" new_v.prepend prepend if prepend rescue nil new_v.append append if append rescue nil r << new_v end #puts "NEW_VAL" #puts new_val fm_attributes[key] = (new_val && new_val.size < 2) ? new_val[0] : new_val end #puts "FM_ATTRIBUTES" #puts fm_attributes fm_attributes end |
#read(query) ⇒ Enumerable<Hash>
Reads one or many resources from a datastore
Adapters provide specific implementation of this method
def read(query)
raise NotImplementedError, "#{self.class}#read not implemented"
end
59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/dm-filemaker-adapter/adapter.rb', line 59 def read(query) query.model.last_query = query #y query _layout = layout(query.model) opts = query. #puts "FMP OPTIONS #{opts.inspect}" opts[:template] = self.class.fmresultset_template_path prms = query.to_fmp_query #puts "ADAPTER#read fmp_query built: #{prms.inspect}" rslt = prms.empty? ? _layout.all(opts) : _layout.find(prms, opts) rslt.dup.each_with_index(){|r, i| rslt[i] = r.to_h} rslt end |
#update(attributes, collection) ⇒ Integer
Updates one or many existing resources
Adapters provide specific implementation of this method
102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/dm-filemaker-adapter/adapter.rb', line 102 def update(attributes, collection) collection[0].model.last_query = [attributes, collection] fm_params = prepare_fmp_attributes(attributes) counter = 0 collection.each do |resource| rslt = layout(resource.model).edit(resource.record_id, fm_params, :template=>self.class.fmresultset_template_path) merge_fmp_response(resource, rslt[0]) resource.persistence_state = DataMapper::Resource::PersistenceState::Clean.new resource counter +=1 end counter end |