Class: Filemaker::Model::Criteria

Inherits:
Object
  • Object
show all
Includes:
Enumerable, Optional, Pagination, Selectable
Defined in:
lib/filemaker/model/criteria.rb

Overview

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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Pagination

#__page, #__per, #page, #per, #positive_page, #update_skip

Methods included from Optional

#limit, #order, #skip

Methods included from Selectable

#custom_query, #find, #in, #not_in, #or, #recid, #where

Constructor Details

#initialize(klass) ⇒ Criteria

Returns a new instance of Criteria.



32
33
34
35
36
37
38
# File 'lib/filemaker/model/criteria.rb', line 32

def initialize(klass)
  @klass    = klass
  @options  = {}
  @chains   = []
  @_page    = 1
  @loaded   = false
end

Instance Attribute Details

#chainsArray (readonly)

Returns keep track of where clause and in clause to not mix them.

Returns:

  • (Array)

    keep track of where clause and in clause to not mix them



28
29
30
# File 'lib/filemaker/model/criteria.rb', line 28

def chains
  @chains
end

#klassFilemaker::Model (readonly)

Returns the class of the model.

Returns:



19
20
21
# File 'lib/filemaker/model/criteria.rb', line 19

def klass
  @klass
end

#loadedFilemaker::Model (readonly) Also known as: loaded?

Returns the class of the model.

Returns:



19
20
21
# File 'lib/filemaker/model/criteria.rb', line 19

def loaded
  @loaded
end

#optionsHash (readonly)

Returns options like skip, limit and order.

Returns:

  • (Hash)

    options like skip, limit and order



25
26
27
# File 'lib/filemaker/model/criteria.rb', line 25

def options
  @options
end

#selectorHash. Array (readonly)

Returns represents the query arguments.

Returns:

  • (Hash. Array)

    represents the query arguments



22
23
24
# File 'lib/filemaker/model/criteria.rb', line 22

def selector
  @selector
end

Instance Method Details

#allObject



70
71
72
# File 'lib/filemaker/model/criteria.rb', line 70

def all
  execute
end

#countInteger

The count this criteria is capable of returning

Returns:

  • (Integer)

    the count



81
82
83
84
85
86
87
88
89
90
91
# 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

#eachObject



62
63
64
# File 'lib/filemaker/model/criteria.rb', line 62

def each
  execute.each { |record| yield record } if block_given?
end

#firstObject



66
67
68
# File 'lib/filemaker/model/criteria.rb', line 66

def first
  limit(1).execute.first
end

#limit?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/filemaker/model/criteria.rb', line 74

def limit?
  !options[:max].nil?
end

#loadObject

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



55
56
57
58
59
60
# File 'lib/filemaker/model/criteria.rb', line 55

def load
  return if loaded?

  @records = all
  @loaded = true
end

#recordsObject



48
49
50
51
# File 'lib/filemaker/model/criteria.rb', line 48

def records
  load
  @records
end

#to_aObject



44
45
46
# File 'lib/filemaker/model/criteria.rb', line 44

def to_a
  records.dup
end

#to_sObject



40
41
42
# File 'lib/filemaker/model/criteria.rb', line 40

def to_s
  "#{selector}, #{options}"
end