Module: Filemaker::Model::Selectable

Included in:
Criteria
Defined in:
lib/filemaker/model/selectable.rb

Instance Method Summary collapse

Instance Method Details

#custom_query(criterion) ⇒ Object



125
126
127
128
129
130
131
# File 'lib/filemaker/model/selectable.rb', line 125

def custom_query(criterion)
  chains.push(:custom)
  chains.delete(:where)
  chains.delete(:in)
  @selector = criterion
  self
end

#find(criterion) ⇒ Filemaker::Model::Criteria, Filemaker::Model Also known as: id

Find records based on model ID. If passed a hash, will use ‘where`. On the last resort, if we seriously can’t find using ‘where`, we find it thru the `recid`. Is this a good design? We will see in production. Performance note: 2 HTTP requests if going that last resort route.

Examples:

Find by model ID.

Model.find('CAID324')

Find with a Hash. This will delegate to ‘where`.

Model.find(name: 'Bob', salary: 4000)

Parameters:

  • criterion (Integer, String, Hash)

Returns:



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/filemaker/model/selectable.rb', line 38

def find(criterion)
  return where(criterion) if criterion.is_a? Hash

  # Find using model ID (may not be the -recid)
  # Always append double '=' for ID instead of just one '='
  id = criterion.to_s.gsub(/\A=*/, '==')

  # If we are finding with ID, we just limit to one and return
  # immediately. Last resort is to use the recid to find.
  where(klass.identity.name => id).first || recid(criterion)
end

#in(criterion, negating = false) {|options| ... } ⇒ Filemaker::Model::Criteria

Find records based on FileMaker’s compound find syntax.

Examples:

Find using a single hash

Model.in(nationality: %w(Singapore Malaysia))

Find using an array of hashes

Model.in([{nationality: %w(Singapore Malaysia)}, {age: [20, 30]}])

Parameters:

  • (Hash, Array)

Yields:

  • (options)

Returns:



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/filemaker/model/selectable.rb', line 105

def in(criterion, negating = false)
  if chains.include?(:where)
    raise Filemaker::Errors::MixedClauseError,
          "Can't mix 'in' with 'where'."
  end

  chains.push(:in)
  chains.delete(:where)
  @selector ||= []

  become_array(criterion).each do |hash|
    accepted_hash = klass.with_model_fields_for_query(hash)
    accepted_hash['-omit'] = true if negating
    @selector << accepted_hash
  end

  yield options if block_given?
  self
end

#not_in(criterion) ⇒ Object

Simply append ‘-omit’ => true to all criteria



134
135
136
# File 'lib/filemaker/model/selectable.rb', line 134

def not_in(criterion)
  self.in(criterion, true)
end

#or(criterion) {|options| ... } ⇒ Filemaker::Model::Criteria

Used with ‘where` to specify how the queries are combined. Default is ’and’, so you won’t find any ‘and` method.

Examples:

Mix with where to ‘or’ query

Model.where(name: 'Bob').or(age: '50')

Parameters:

  • criterion (Hash)

    Hash criterion

Yields:

  • (options)

Returns:



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

def or(criterion)
  if chains.include?(:in)
    raise Filemaker::Errors::MixedClauseError,
          "Can't mix 'or' with 'in'."
  end

  @selector ||= {}
  selector.merge!(klass.with_model_fields_for_query(criterion))
  options[:lop] = 'or'
  yield options if block_given?
  self
end

#recid(id) ⇒ Object

Using FileMaker’s internal ID to find the record.



51
52
53
54
55
56
57
58
# File 'lib/filemaker/model/selectable.rb', line 51

def recid(id)
  return nil if id.blank?

  @selector = {} # We want to clear the selector when it comes to recid
  selector['-recid'] = id
  chains.push(:where) unless chains.include?(:where) # No double :where
  first
end

#where(criterion) {|options| ... } ⇒ Filemaker::Model::Criteria

Find records based on query hash.

Parameters:

  • criterion (Hash)

    Hash criterion

Yields:

  • (options)

Returns:



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/filemaker/model/selectable.rb', line 9

def where(criterion)
  if chains.include?(:in)
    raise Filemaker::Errors::MixedClauseError,
          "Can't mix 'where' with 'in'."
  end

  chains.push(:where)
  chains.delete(:in)

  @selector ||= {}
  selector.merge!(klass.with_model_fields_for_query(criterion))
  yield options if block_given?
  self
end