Class: Anoubis::Etc::Filter

Inherits:
Object
  • Object
show all
Defined in:
app/controllers/anoubis/etc/filter.rb

Overview

Definitions of filter options for data.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Filter

Sets default parameters for filter



28
29
30
31
32
33
# File 'app/controllers/anoubis/etc/filter.rb', line 28

def initialize(options = {})
  self.fields = if options.key? :fields then options[:fields] else {} end
  self.hash = {}
  self.array = []
  if options.key? :data then self.init_data(options[:data]) else self.data end
end

Instance Attribute Details

#arrayFieldOrder

Defines ActiveRecord array representation of where.



19
# File 'app/controllers/anoubis/etc/filter.rb', line 19

class_attribute :array, default: []

#dataHash?

Defines row filter data



9
# File 'app/controllers/anoubis/etc/filter.rb', line 9

class_attribute :data, default: nil

#fieldsHash<Anoubis::Etc::Field>

Defines reference for fields



24
# File 'app/controllers/anoubis/etc/filter.rb', line 24

class_attribute :fields, default: nil

#hashFieldOrder

Defines ActiveRecord hash representation of where.



14
# File 'app/controllers/anoubis/etc/filter.rb', line 14

class_attribute :hash, default: {}

Instance Method Details

#attach_to_array(str) ⇒ Object

Attach parameters to where



211
212
213
214
215
216
217
# File 'app/controllers/anoubis/etc/filter.rb', line 211

def attach_to_array(str)
  if self.array.count == 0
    self.array.push(str)
  else
    self.array[0] += ' AND ' + str
  end
end

#init_data(data = nil) ⇒ Object

Initializes all where parameters according by data and fields.



54
55
56
57
58
59
60
61
62
63
64
65
# File 'app/controllers/anoubis/etc/filter.rb', line 54

def init_data(data = nil)
  self.data = if data then data else {} end

  if self.fields
    self.fields.each do |key, field|
      if self.data.key? key.to_s
        proc = format('init_data_%s', field.type)
        result = self.send proc, key, self.data[key.to_s]
      end
    end
  end
end

#init_data_datetime(key, value) ⇒ Object

Initializes where parameters for filed with type ‘datetime’.



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'app/controllers/anoubis/etc/filter.rb', line 223

def init_data_datetime(key, value)
  if value.class == Array
    if value.count == 2
      begin
        from = Time.parse value[0]
        to = Time.parse value[1]
      rescue
        from = nil
        to = nil
      end
      if from && to
        self.hash[key] = [ from..to ]
      end
    end
  end
  #words = value.split(' ')

#        words.each do |word|
#          if self.array.count == 0
#            self.array.push(self.fields[key].table_field.to_s + ' LIKE ?')
#          else
#            self.array[0] += ' AND '+self.fields[key].table_field.to_s+' LIKE ?'
#          end
#          self.array.push("%#{word}%")
#        end
end

#init_data_key(key, value) ⇒ Object

Initializes where parameters for filed with type key.



199
200
201
202
203
204
205
206
# File 'app/controllers/anoubis/etc/filter.rb', line 199

def init_data_key(key, value)
  words = value.split(' ')

  words.each do |word|
    self.attach_to_array self.fields[key].table_field.to_s + ' LIKE ?'
    self.array.push("%#{word}%")
  end
end

#init_data_listbox(key, value) ⇒ Object

Initializes where parameters for filed with type listbox.



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'app/controllers/anoubis/etc/filter.rb', line 174

def init_data_listbox(key, value)
  values = []
  if value.class == Array
    value.each do |data|
      if self.fields[key].options.enum
        values.push self.fields[key].options.enum[data]
      else
        values.push data
      end
    end
  end
  if values.count > 0
    if self.fields[key].table_field.to_s.include? '.'
      self.attach_to_array self.fields[key].table_field.to_s + ' IN (?)'
      self.array.push(values)
    else
      self.hash[self.fields[key].table_field] = values
    end
  end
end

#init_data_number(key, value) ⇒ Object

Initializes where parameters for filed with type number.



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'app/controllers/anoubis/etc/filter.rb', line 97

def init_data_number(key, value)
  if value.index ','
    if self.fields[key].table_field.to_s.include? '.'
      self.attach_to_array self.fields[key].table_field.to_s + ' IN (?)'
      self.array.push(value.split ',')
    else
      self.hash[key] = value.split ','
    end
    return
  end

  if value.index '>'
    if value.to_s.include? '.'
      result_value = value.to_s[1..100].to_f
    else
      result_value = value.to_s[1..100].to_i
    end
    if self.fields[key].table_field.to_s.include? '.'
      self.attach_to_array self.fields[key].table_field.to_s + ' > ?'

      self.array.push(result_value)
    else
      self.hash[key] = [result_value..Float::INFINITY]
    end
    return
  end

  if value.index '<'
    if value.to_s.include? '.'
      result_value = value.to_s[1..100].to_f
    else
      result_value = value.to_s[1..100].to_i
    end
    if self.fields[key].table_field.to_s.include? '.'
      self.attach_to_array self.fields[key].table_field.to_s + ' < ?'
      self.array.push(result_value)
    else
      self.hash[key] = [-Float::INFINITY..result_value]
    end
    return
  end

  if value.index '-'
    data = value.split '-'
    if data[0].include? '.'
      min_value = data[0].to_f
    else
      min_value = data[0].to_i
    end
    if data[1].include? '.'
      max_value = data[1].to_f
    else
      max_value = data[1].to_i
    end
    min_value, max_value = max_value, min_value if min_value > max_value
    if self.fields[key].table_field.to_s.include? '.'
      self.attach_to_array self.fields[key].table_field.to_s + ' > ? AND ' + self.fields[key].table_field.to_s + ' < ?'
      self.array.push(min_value)
      self.array.push(max_value)
    else
      self.hash[key] = [min_value..max_value]
    end
    return
  end

  if self.fields[key].table_field.to_s.include? '.'
    self.attach_to_array self.fields[key].table_field.to_s + ' = ?'
    self.array.push(value)
  else
    self.hash[key] = value
  end
end

#init_data_string(key, value) ⇒ Object

Initializes where parameters for filed with type string.



71
72
73
74
75
76
77
78
# File 'app/controllers/anoubis/etc/filter.rb', line 71

def init_data_string(key, value)
  words = value.split(' ')

  words.each do |word|
    self.attach_to_array self.fields[key].table_field.to_s+' LIKE ?'
    self.array.push("%#{word}%")
  end
end

#init_data_text(key, value) ⇒ Object

Initializes where parameters for filed with type text.



84
85
86
87
88
89
90
91
# File 'app/controllers/anoubis/etc/filter.rb', line 84

def init_data_text(key, value)
  words = value.split(' ')

  words.each do |word|
    self.attach_to_array self.fields[key].table_field.to_s+' LIKE ?'
    self.array.push("%#{word}%")
  end
end

#to_hHash

Generates hash representation of all class parameters,



38
39
40
41
42
43
44
45
46
47
48
49
# File 'app/controllers/anoubis/etc/filter.rb', line 38

def to_h
  result = {
      data: self.data,
      hash: self.hash,
      array: self.array,
      fields: {}
  }
  self.fields.each_key do |key|
    result[:fields][key] = self.fields[key].to_h
  end
  result
end