module Filemaker::Model::ClassMethods

Public Instance Methods

database(db) click to toggle source
# File lib/filemaker/model.rb, line 91
def database(db)
  self.db = db
  self.registry_name ||= 'default' unless lay.blank?
  register
end
default_per_page() click to toggle source
# File lib/filemaker/model.rb, line 118
def default_per_page
  per_page
end
fm_fields() click to toggle source

Make use of -view to return an array of [name, data_type] for this model from FileMaker.

@return [Array] array of [name, data_type]

# File lib/filemaker/model.rb, line 126
def fm_fields
  api.view.fields.values.map { |field| [field.name, field.data_type] }
end
layout(lay) click to toggle source
# File lib/filemaker/model.rb, line 97
def layout(lay)
  self.lay = lay
  self.registry_name ||= 'default' unless db.blank?
  register
end
paginates_per(value) click to toggle source

A chance for the model to set it's per_page.

# File lib/filemaker/model.rb, line 114
def paginates_per(value)
  self.per_page = value.to_i
end
register() click to toggle source
# File lib/filemaker/model.rb, line 108
def register
  self.server = Filemaker.registry[registry_name]
  self.api = server.db[db][lay] if server && db && lay
end
registry(name) click to toggle source
# File lib/filemaker/model.rb, line 103
def registry(name)
  self.registry_name = (name || 'default').to_s
  register
end
with_model_fields_for_creation(criterion) click to toggle source
# File lib/filemaker/model.rb, line 158
def with_model_fields_for_creation(criterion)
  accepted_fields = {}

  criterion.each_pair do |key, value|
    field = find_field_by_name(key)

    # Do not process nil value
    next unless field && value

    # We do not serialize at this point, as we are still in Ruby-land.
    # Filemaker::Server will help us serialize into FileMaker format.
    if value.is_a?(Array)
      field.max_repeat.times do |idx|
        index = idx + 1
        repeated_fm_name = "#{field.fm_name}(#{index})"
        accepted_fields[repeated_fm_name] = field.serialize_for_update(value[idx])
      end
    else
      accepted_fields[field.fm_name] = field.serialize_for_update(value)
    end
  end

  accepted_fields
end
with_model_fields_for_query(criterion) click to toggle source

Filter out any fields that do not match model's fields.

A testing story to tell: when working on `in` query, we have value that is an array. Without the test and expectation setup, debugging the output will take far longer to realise. This reinforce the belief that TDD is in fact a valuable thing to do.

# File lib/filemaker/model.rb, line 136
def with_model_fields_for_query(criterion)
  accepted_fields = {}

  criterion.each_pair do |key, value|
    field = find_field_by_name(key)

    # Do not process nil value, but empty string is ok in order to reset
    # some fields.
    next unless field && value

    # We do not serialize at this point, as we are still in Ruby-land.
    # Filemaker::Server will help us serialize into FileMaker format.
    if value.is_a?(Array)
      accepted_fields[field.fm_name] = value.map { |v| field.serialize_for_query(v) }
    else
      accepted_fields[field.fm_name] = field.serialize_for_query(value)
    end
  end

  accepted_fields
end
with_model_fields_for_update(criterion) click to toggle source
# File lib/filemaker/model.rb, line 183
def with_model_fields_for_update(criterion)
  accepted_fields = {}

  criterion.each_pair do |key, value|
    field = find_field_by_name(key)

    next unless field

    # Able to process nil value only for update

    # We do not serialize at this point, as we are still in Ruby-land.
    # Filemaker::Server will help us serialize into FileMaker format.
    if value.is_a?(Array)
      field.max_repeat.times do |idx|
        index = idx + 1
        repeated_fm_name = "#{field.fm_name}(#{index})"
        accepted_fields[repeated_fm_name] = field.serialize_for_update(value[idx])
      end
    else
      accepted_fields[field.fm_name] = field.serialize_for_update(value)
    end
  end

  accepted_fields
end