Module: Birddog::FieldConditions

Included in:
Birddog
Defined in:
lib/birddog/field_conditions.rb

Instance Method Summary collapse

Instance Method Details

#cast_numeric(field_type, value) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/birddog/field_conditions.rb', line 37

def cast_numeric(field_type, value)
  case field_type
  when :integer then
    value.to_i
  else
    value.to_f
  end
end

#conditionally_scoped(current_scope, field, condition, value, aggregate, is_numeric = false) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/birddog/field_conditions.rb', line 46

def conditionally_scoped(current_scope, field, condition, value, aggregate, is_numeric = false)
  having_or_where = (aggregate ? :having : :where)
  scope = case condition
  when :=~, "=~", "~=" then
    if is_numeric
      current_scope.__send__(having_or_where, field.gteq(value.floor).and(field.lt((value + 1).floor))) 
    else
      current_scope.__send__(having_or_where, field.matches(value))
    end
  when :<, "<" then
    current_scope.__send__(having_or_where, field.lt(value))
  when :>, ">" then
    current_scope.__send__(having_or_where, field.gt(value))
  when :<=, "<=", "=<" then
    current_scope.__send__(having_or_where, field.lteq(value))
  when :>=, ">=", "=>" then
    current_scope.__send__(having_or_where, field.gteq(value))
  when :==, "=", "==" then
    current_scope.__send__(having_or_where, field.eq(value))
  else
    raise "#{condition} not defined for #{field}"
  end

  return scope
end

#conditions_for_date(current_scope, field, condition, value) ⇒ Object



29
30
31
# File 'lib/birddog/field_conditions.rb', line 29

def conditions_for_date(current_scope, field, condition, value)
  conditionally_scoped(current_scope, field[:attribute], condition, value.value.strftime("%Y-%m-%d"), field[:aggregate])
end

#conditions_for_numeric(current_scope, field, condition, value) ⇒ Object



33
34
35
# File 'lib/birddog/field_conditions.rb', line 33

def conditions_for_numeric(current_scope, field, condition, value)
  conditionally_scoped(current_scope, field[:attribute], condition, cast_numeric(field[:type], value), field[:aggregate], true)
end

#conditions_for_string_search(current_scope, field, condition, value) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/birddog/field_conditions.rb', line 5

def conditions_for_string_search(current_scope, field, condition, value)
  search_con = condition
  strict_equality = (search_con == "==")
  value_to_search = (value ? value.dup : nil)

  if !strict_equality
    if field[:match_substring]
      search_con = "=~"
      value_to_search = "%#{value_to_search}%"
    end

    if field[:regex] && regexed?(value)
      # TODO check db driver to determine regex operator for DB (current is Postgres)
      search_con = "=~"
      value_to_search = value[1..value.size-2]
    elsif field[:wildcard] && value_to_search.include?("*")
      search_con = "=~"
      value_to_search.gsub!(/[\*]/, "%")
    end
  end

  conditionally_scoped(current_scope, field[:attribute], search_con, value_to_search, field[:aggregate])
end

#regexed?(value) ⇒ Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/birddog/field_conditions.rb', line 72

def regexed?(value)
  (value[0].chr == '/' && value[-1].chr == '/')
end