class Filemaker::Model::Criteria

Criteria encapsulates query arguments and options to represent a single query. It has convenient query DSL like where and in to represent both -find and -findquery FileMaker query. On top of that you can negate any query with the not clause to omit selection.

Attributes

chains[R]

@return [Array] keep track of where clause and in clause to not mix them

klass[R]

@return [Filemaker::Model] the class of the model

loaded[R]

@return [Filemaker::Model] the class of the model

loaded?[R]

@return [Filemaker::Model] the class of the model

options[R]

@return [Hash] options like skip, limit and order

selector[R]

@return [Hash. Array] represents the query arguments

Public Class Methods

new(klass) click to toggle source
# File lib/filemaker/model/criteria.rb, line 32
def initialize(klass)
  @klass    = klass
  @options  = {}
  @chains   = []
  @_page    = 1
  @loaded   = false
end

Public Instance Methods

all() click to toggle source
# File lib/filemaker/model/criteria.rb, line 70
def all
  execute
end
count() click to toggle source

The count this criteria is capable of returning

@return [Integer] the count

# File lib/filemaker/model/criteria.rb, line 81
def count
  if chains.include?(:where)
    klass.api.find(selector, options.merge(max: 0)).count
  elsif chains.include?(:in)
    klass.api.query(selector, options.merge(max: 0)).count
  elsif chains.include?(:custom)
    klass.api.findquery(selector, options.merge(max: 0)).count
  else
    klass.api.findall(options.merge(max: 0)).count
  end
end
each() { |record| ... } click to toggle source
# File lib/filemaker/model/criteria.rb, line 62
def each
  execute.each { |record| yield record } if block_given?
end
first() click to toggle source
# File lib/filemaker/model/criteria.rb, line 66
def first
  limit(1).execute.first
end
limit?() click to toggle source
# File lib/filemaker/model/criteria.rb, line 74
def limit?
  !options[:max].nil?
end
load() click to toggle source

Causes the records to be loaded from FM if they have not been loaded already

# File lib/filemaker/model/criteria.rb, line 55
def load
  return if loaded?

  @records = all
  @loaded = true
end
records() click to toggle source
# File lib/filemaker/model/criteria.rb, line 48
def records
  load
  @records
end
to_a() click to toggle source
# File lib/filemaker/model/criteria.rb, line 44
def to_a
  records.dup
end
to_s() click to toggle source
# File lib/filemaker/model/criteria.rb, line 40
def to_s
  "#{selector}, #{options}"
end

Protected Instance Methods

execute() click to toggle source
# File lib/filemaker/model/criteria.rb, line 95
def execute
  resultset = []
  paginated = chains.include?(:page)

  if chains.include?(:where)
    # Use -find
    resultset = klass.api.find(selector, options)
  elsif chains.include?(:in)
    # Use -findquery
    resultset = klass.api.query(selector, options)
  elsif chains.include?(:custom)
    # Use -findquery directly
    resultset = klass.api.findquery(selector, options)
  else
    # Use -findall
    limit(1) unless limit?
    resultset = klass.api.findall(options)
  end

  models = Filemaker::Model::Builder.collection(resultset, klass)

  if defined?(Kaminari) && paginated
    Kaminari.paginate_array(models, total_count: resultset.count)
            .page(@_page)
            .per(options[:max])
  else
    models
  end
end