Class: DataMapper::Adapters::FilemakerAdapter

Inherits:
AbstractAdapter
  • Object
show all
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

Instance Method Summary collapse

Class Attribute Details

.fmresultset_template_pathObject

Returns the value of attribute fmresultset_template_path.



38
39
40
# File 'lib/dm-filemaker-adapter/adapter.rb', line 38

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



120
121
122
123
124
125
126
127
128
129
# File 'lib/dm-filemaker-adapter/adapter.rb', line 120

def aggregate(query)
  #query.model.last_query = query
  #y query
  _layout = layout(query.model)
  opts = fmp_options(query)
  opts[:template] = self.class.fmresultset_template_path
  prms = fmp_query(query.conditions) #.to_set.first)
  #[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

Examples:

adapter.create(collection)  # => 1

Parameters:

  • resources (Enumerable<Resource>)

    The list of resources (model instances) to create

Returns:

  • (Integer)

    The number of records that were actually saved into the data-store



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/dm-filemaker-adapter/adapter.rb', line 76

def create(resources)
  #resources[0].model.last_query = resources
  counter = 0
  resources.each do |resource|
    fm_params = 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

Examples:

adapter.delete(collection)  # => 1

Parameters:

  • collection (Collection)

    collection of records to be deleted

Returns:

  • (Integer)

    the number of records deleted



174
175
176
177
178
179
180
181
# File 'lib/dm-filemaker-adapter/adapter.rb', line 174

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.



188
189
190
191
# File 'lib/dm-filemaker-adapter/adapter.rb', line 188

def layout(model)
  #Rfm.layout(model.storage_name, options.symbolize_keys)   #query.repository.adapter.options.symbolize_keys)
  model.layout
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

Examples:

adapter.read(query)  # => [ { 'name' => 'Dan Kubb' } ]

Parameters:

  • query (Query)

    the query to match resources in the datastore

Returns:

  • (Enumerable<Hash>)

    an array of hashes to become resources



106
107
108
109
110
111
112
113
114
115
116
# File 'lib/dm-filemaker-adapter/adapter.rb', line 106

def read(query)
  #query.model.last_query = query
  #y query
  _layout = layout(query.model)
  opts = fmp_options(query)
  opts[:template] = self.class.fmresultset_template_path
  prms = fmp_query(query.conditions) #.to_set.first)
  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

Examples:

adapter.update(attributes, collection)  # => 1

Parameters:

  • attributes (Hash(Property => Object))

    hash of attribute values to set, keyed by Property

  • collection (Collection)

    collection of records to be updated

Returns:

  • (Integer)

    the number of records updated



147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/dm-filemaker-adapter/adapter.rb', line 147

def update(attributes, collection)
  #collection[0].model.last_query = [attributes, collection]
  fm_params = 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