Class: SqlFilter

Inherits:
Object
  • Object
show all
Includes:
SqlFilterAttributes
Defined in:
lib/sql_filter.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from SqlFilterAttributes

included

Constructor Details

#initialize(vars = nil) ⇒ SqlFilter

Returns a new instance of SqlFilter.



121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/sql_filter.rb', line 121

def initialize(vars=nil)
  # set default values
  for attr,options in self.class.attributes
    instance_variable_set '@'+attr.to_s, options[:default] 
  end
  
  if vars
    # send input values to setter methods
    for key,val in vars
      send "#{key}=", val 
    end
  end
end

Class Method Details

.attribute(*args) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/sql_filter.rb', line 31

def self.attribute(*args)
  options   = args.delete_at(args.size-1) if args.last.is_a?(Hash)
  options ||= {}
  
  #puts "#############################"
  #puts self
  #puts "#############################"
  
  for attr in args
    
    
    class_method_lines = []
    
    options[:escape]       ||= true
    options[:ignore_blank] ||= true
    
    value    = options[:value]
    operator = options[:operator] || "="
    
    if operator.is_a?(Array)
      attr_accessor "operator_for_#{attr}"
      class_method_lines << "def operator_for_#{attr}"
      class_method_lines << "  options = self.class.attributes[:#{attr}]"
      class_method_lines << "  @operator_for_#{attr} ||= options[:default_operator]"
      if options[:ignore_blank]
        class_method_lines << "  return nil if @operator_for_#{attr}.to_s.empty?"
      end
      class_method_lines << "  raise 'unknown operator '+@operator_for_#{attr}+' for #{attr}' unless options[:operator].include?(@operator_for_#{attr})"
      class_method_lines << "  @operator_for_#{attr}"
      class_method_lines << "end"
      
    else
      class_method_lines << "def operator_for_#{attr}"
      class_method_lines << "  '#{operator.to_s.upcase}'"
      class_method_lines << "end"
    end
    
    class_method_lines << "def #{attr}_to_sql"
    class_method_lines << "return nil unless operator_for_#{attr}"
    
    # wenn kein statischer Wert vorgegeben ist
    unless options[:value].is_a?(String)
      attr_accessor attr
      # leere Werte bei Wunsch ignorieren
      if options[:ignore_blank]
        class_method_lines << "return nil if @#{attr}.to_s.empty?"
      end
    end
    
    
    right_sql = nil
    if value
      if value.is_a?(Array)
        # eingabe überprüfen
        class_method_lines << "raise 'xxx' unless self.class.attributes[:#{attr}][:value].include?(#{attr})"
        right_sql = attr.to_s
      else
        # vorgabe einsetzen
        right_sql = value.inspect
      end
    else
      if operator==:like
        right_sql = "'%'+#{attr}+'%'"
      else
        # nur methode aufrufen
        right_sql = attr.to_s
      end
    end
    
    field = options[:field] ? options[:field]  : attr
    field = field.to_s.split(".").collect{|c| "`"+c+"`"}.join "."
    if options[:escape] == false
      class_method_lines << "  '#{field} ' + operator_for_#{attr} + " + right_sql
    else
      class_method_lines << "  ['#{field} ' + operator_for_#{attr} + ' ?', " + right_sql + ']'
    end
    
    class_method_lines << "end"
    
    @attributes[attr] = options
    
    
    #puts class_method_lines.join "\n"
    class_eval class_method_lines.join "\n"

    operator = options[:operators]
    
  end
end

Instance Method Details

#to_aObject

Build the whole conditions array



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/sql_filter.rb', line 136

def to_a
  sql = ["1"]
  
  for key, options in self.class.attributes
    val = instance_variable_get("@#{key}") || options[:default]
    res = send "#{key}_to_sql"
    if res.is_a?(Array)
      sql[0] << " AND (#{res.shift})"
      sql = sql + res
    elsif res # nicht nil
      sql[0] << " AND (#{res})"
    end
  end
  
  sql
end