Class: Conditioner::Configurator

Inherits:
Object
  • Object
show all
Defined in:
lib/conditioner/configurator.rb

Instance Method Summary collapse

Instance Method Details

#activate_default_rules!Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/conditioner/configurator.rb', line 17

def activate_default_rules!
  add_rule do |field, v, cnd|
    unless v.nil? || (v.respond_to?(:'empty?') && v.empty?)
      if cnd.is_field?(field) && v =~ /(\*$|^\*)/
        cnd.and(["#{cnd.with_table_name(field)} ILIKE ?",v.gsub(/(\*$|^\*)/,'%')])
      elsif cnd.is_field?(field) && v =~ /(%$|^%)/
        cnd.and(["upper(#{cnd.with_table_name(field)}) like upper(?)",v])
        #FIXME: add logic for ranges
      elsif field=~ /^from_(\w*_(datetime|at))/ and cnd.is_field?($1)
        datetime = Time.zone.parse(v)
        cnd.and(["#{cnd.with_table_name($1)} >= ?",datetime])
      elsif field=~ /^to_(\w*_(datetime|at))/ and cnd.is_field?($1)
        datetime = Time.zone.parse(v).change(:hour => 23,:min => 59,:sec => 59,:usec => 0)
        cnd.and(["#{cnd.with_table_name($1)} <= ?",datetime])
      elsif cnd.is_field?(field) && (field.include?('_datetime') || field.include?('_at'))
        start_date = Time.zone.parse(v).change(:hour => 0,:min => 0,:sec => 0,:usec => 0)
        end_date = Time.zone.parse(v).change(:hour => 23,:min => 59,:sec => 59,:usec => 0)
        cnd.and(["(#{cnd.with_table_name(field)} BETWEEN ? AND ?)",start_date,end_date])
      elsif field=~ /^(\w*)_(ltoe|gtoe|lt|gt)$/ and cnd.is_field?($1)
        operator = { 'lt' => '<', 'gt' => '>', 'ltoe' => '<=', 'gtoe' => '>=' }[$2]
        cnd.and(["#{cnd.with_table_name($1)} #{operator} ?", v])
      elsif field=~ /^(\w*)_(begins_with|ends_with|contains)$/ and cnd.is_field?($1)
        if $2 == 'begins_with'
          like_expr = "#{v}%"
        elsif $2 == 'ends_with'
          like_expr = "%#{v}"
        else
          like_expr = "%#{v}%"
        end
        cnd.and(["#{cnd.with_table_name($1)} ILIKE ?", like_expr])
      elsif field=~ /^(\w*)_in$/ and cnd.is_field?($1)
        cnd.and(["#{cnd.with_table_name($1)} IN (?)", v])
      end
    end
  end
end

#add_rule(&rule) ⇒ Object



8
9
10
# File 'lib/conditioner/configurator.rb', line 8

def add_rule(&rule)
  extract_rules<< rule
end

#clear_rules!Object

You can clear default rules with



13
14
15
# File 'lib/conditioner/configurator.rb', line 13

def clear_rules!
  @rules = []
end

#extract_rulesObject



4
5
6
# File 'lib/conditioner/configurator.rb', line 4

def extract_rules
  @rules ||= []
end