Method: Hook::Collection#where

Defined in:
lib/hook-client/collection.rb

#where(fields = {}, operation = 'and') ⇒ Collection

Add where clause to the current query.

Supported modifiers on fields: .gt, .gte, .lt, .lte, .ne, .in, .not_in, .nin, .like, .between, .not_between

Examples:

hook.collection(:movies).where({
  :name => "Hook",
  :year.gt => 1990
})

Using Range

hook.collection(:movies).where({
  :name.like => "%panic%",
  :year.between => 1990..2014
})

Parameters:

  • fields (Hash) (defaults to: {})

    fields and values to filter

  • operation (String) (defaults to: 'and')

    (and, or)

Returns:



92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/hook-client/collection.rb', line 92

def where fields = {}, operation = 'and'
  fields.each_pair do |k, value|
    field = (k.respond_to?(:field) ? k.field : k).to_s
    comparation = k.respond_to?(:comparation) ? k.comparation : '='

    # Range syntatic sugar
    value = [ value.first, value.last ] if value.kind_of?(Range)

    @wheres << [field, comparation, value, operation]
  end
  self
end