Class: Filemaker::Model::Criteria
- Inherits:
-
Object
- Object
- Filemaker::Model::Criteria
- 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
-
#chains ⇒ Array
readonly
Keep track of where clause and in clause to not mix them.
-
#klass ⇒ Filemaker::Model
readonly
The class of the model.
-
#loaded ⇒ Filemaker::Model
(also: #loaded?)
readonly
The class of the model.
-
#options ⇒ Hash
readonly
Options like skip, limit and order.
-
#selector ⇒ Hash. Array
readonly
Represents the query arguments.
Instance Method Summary collapse
- #all ⇒ Object
-
#count ⇒ Integer
The count this criteria is capable of returning.
- #each ⇒ Object
- #first ⇒ Object
-
#initialize(klass) ⇒ Criteria
constructor
A new instance of Criteria.
- #limit? ⇒ Boolean
-
#load ⇒ Object
Causes the records to be loaded from FM if they have not been loaded already.
- #records ⇒ Object
- #to_a ⇒ Object
- #to_s ⇒ Object
Methods included from Pagination
#__page, #__per, #page, #per, #positive_page, #update_skip
Methods included from Optional
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
#chains ⇒ Array (readonly)
Returns 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 |
#klass ⇒ Filemaker::Model (readonly)
Returns the class of the model.
19 20 21 |
# File 'lib/filemaker/model/criteria.rb', line 19 def klass @klass end |
#loaded ⇒ Filemaker::Model (readonly) Also known as: loaded?
Returns the class of the model.
19 20 21 |
# File 'lib/filemaker/model/criteria.rb', line 19 def loaded @loaded end |
#options ⇒ Hash (readonly)
Returns options like skip, limit and order.
25 26 27 |
# File 'lib/filemaker/model/criteria.rb', line 25 def @options end |
#selector ⇒ Hash. Array (readonly)
Returns represents the query arguments.
22 23 24 |
# File 'lib/filemaker/model/criteria.rb', line 22 def selector @selector end |
Instance Method Details
#all ⇒ Object
70 71 72 |
# File 'lib/filemaker/model/criteria.rb', line 70 def all execute end |
#count ⇒ Integer
The count this criteria is capable of returning
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, .merge(max: 0)).count elsif chains.include?(:in) klass.api.query(selector, .merge(max: 0)).count elsif chains.include?(:custom) klass.api.findquery(selector, .merge(max: 0)).count else klass.api.findall(.merge(max: 0)).count end end |
#each ⇒ Object
62 63 64 |
# File 'lib/filemaker/model/criteria.rb', line 62 def each execute.each { |record| yield record } if block_given? end |
#first ⇒ Object
66 67 68 |
# File 'lib/filemaker/model/criteria.rb', line 66 def first limit(1).execute.first end |
#limit? ⇒ Boolean
74 75 76 |
# File 'lib/filemaker/model/criteria.rb', line 74 def limit? ![:max].nil? end |
#load ⇒ Object
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 |
#records ⇒ Object
48 49 50 51 |
# File 'lib/filemaker/model/criteria.rb', line 48 def records load @records end |
#to_a ⇒ Object
44 45 46 |
# File 'lib/filemaker/model/criteria.rb', line 44 def to_a records.dup end |
#to_s ⇒ Object
40 41 42 |
# File 'lib/filemaker/model/criteria.rb', line 40 def to_s "#{selector}, #{}" end |