Module: TheWhere::Range

Included in:
TheWhere
Defined in:
lib/the_where/range.rb

Constant Summary collapse

PATTERN =
{
  '-gt' => '>',
  '-gte' => '>=',
  '-lt' => '<',
  '-lte' => '<='
}

Instance Method Summary collapse

Instance Method Details

#filter_range(params) ⇒ Object



39
40
41
42
43
# File 'lib/the_where/range.rb', line 39

def filter_range(params)
  params.select do |k, _|
    k.end_with?(*PATTERN.keys)
  end
end

#range_scope(params) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/the_where/range.rb', line 11

def range_scope(params)
  where_string = []
  where_hash = {}

  PATTERN.each do |char, sign|
    options = params.select{ |key, _| key.end_with?(char) }

    options.each do |key, value|
      exp = Regexp.new(char + '$')
      real_key = key.sub(exp, '')
      agent_key = key.gsub(/[-\.]/, '_')

      where_string << "#{real_key} #{sign} :#{agent_key}"

      where_hash.merge! agent_key.to_sym => value
    end
  end

  where_string = where_string.join ' AND '

  if where_string.present?
    condition = [where_string, where_hash]
    where(condition)
  else
    all
  end
end