Module: Filemaker::Model::Selectable

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

Instance Method Summary collapse

Instance Method Details

#find(criterion) ⇒ Filemaker::Model::Criteria, Filemaker::Model

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:



34
35
36
37
38
39
40
41
42
43
# File 'lib/filemaker/model/selectable.rb', line 34

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

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

  # 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:



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/filemaker/model/selectable.rb', line 99

def in(criterion, negating = false)
  fail Filemaker::Error::MixedClauseError,
       "Can't mix 'in' with 'where'." if chains.include?(:where)
  chains.push(:in)
  @selector ||= []

  become_array(criterion).each do |hash|
    accepted_hash = klass.with_model_fields(hash)
    accepted_hash.merge!('-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



116
117
118
# File 'lib/filemaker/model/selectable.rb', line 116

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:



129
130
131
132
133
134
135
136
137
# File 'lib/filemaker/model/selectable.rb', line 129

def or(criterion)
  fail Filemaker::Error::MixedClauseError,
       "Can't mix 'or' with 'in'." if chains.include?(:in)
  @selector ||= {}
  selector.merge!(klass.with_model_fields(criterion))
  options[:lop] = 'or'
  yield options if block_given?
  self
end

#recid(id) ⇒ Object

Using FileMaker’s internal ID to find the record.



46
47
48
49
50
51
52
53
# File 'lib/filemaker/model/selectable.rb', line 46

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
# File 'lib/filemaker/model/selectable.rb', line 9

def where(criterion)
  fail Filemaker::Error::MixedClauseError,
       "Can't mix 'where' with 'in'." if chains.include?(:in)
  chains.push(:where)

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