Module: Filtered

Defined in:
lib/scout/tsv/util/filter.rb

Defined Under Namespace

Classes: Filter, FilterArray

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object

}}} FILTER



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/scout/tsv/util/filter.rb', line 161

def self.extended(base)
  if not base.respond_to? :unfiltered_set
    class << base
      attr_accessor :filter_dir, :filters
      
      alias unfiltered_set []=
      alias []= filtered_set

      alias unfiltered_filename filename
      alias filename filtered_filename

      alias unfiltered_keys keys
      alias keys filtered_keys

      alias unfiltered_values values
      alias values filtered_values

      alias unfiltered_each each
      alias each filtered_each

      alias unfiltered_collect collect
      alias collect filtered_collect

      alias unfiltered_delete delete
      alias delete filtered_delete
    end
  end
  base.filters = []
end

Instance Method Details

#add_filter(match, value, persistence = nil) ⇒ Object



265
266
267
268
269
270
271
272
# File 'lib/scout/tsv/util/filter.rb', line 265

def add_filter(match, value, persistence = nil)
  if persistence.nil? and filter_dir
    persistence = File.join(filter_dir, match.to_s + '.filter')
  end

  filter = Filter.new self, match, value, persistence
  filters.push filter
end

#filtered_collect(&block) ⇒ Object



241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/scout/tsv/util/filter.rb', line 241

def filtered_collect(&block)
  if filters.empty?
    self.send(:unfiltered_collect, &block)
  else
    ids = filters.inject(nil){|list,filter| list = (list.nil? ? filter.ids.dup : Misc.intersect_sorted_arrays(list, filter.ids.dup))}

    new = self.annotate({})

    ids.zip(self.send(:values_at, *ids)).each do |id, values|
      new[id] = values
    end
    new.send :collect, &block
  end
end

#filtered_delete(key) ⇒ Object



256
257
258
259
260
261
262
263
# File 'lib/scout/tsv/util/filter.rb', line 256

def filtered_delete(key)
  if filters.empty?
    self.send(:unfiltered_delete, key)
  else
    reset_filters
    self.send :unfiltered_delete, key
  end
end

#filtered_each(&block) ⇒ Object



227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/scout/tsv/util/filter.rb', line 227

def filtered_each(&block)
  if filters.empty?
    self.send(:unfiltered_each, &block)
  else
    ids = filters.inject(nil){|list,filter| list.nil? ? filter.ids.dup : Misc.intersect_sorted_arrays(list, filter.ids.dup)}

    ids.each do |id|
      value = self[id]
      yield id, value if block_given?
      [id, value]
    end
  end
end

#filtered_filenameObject



191
192
193
194
195
196
197
# File 'lib/scout/tsv/util/filter.rb', line 191

def filtered_filename
  if filters.empty?
    unfiltered_filename
  else
    unfiltered_filename + ":Filtered[#{filters.collect{|f| [f.match, Array === f.value ? Misc.digest(:values => f.value) : f.value] * "="} * ", "}]"
  end
end

#filtered_keysObject



210
211
212
213
214
215
216
# File 'lib/scout/tsv/util/filter.rb', line 210

def filtered_keys
  if filters.empty?
    self.send(:unfiltered_keys)
  else
    filters.inject(nil){|list,filter| list.nil? ? filter.ids.dup : Misc.intersect_sorted_arrays(list, filter.ids.dup)}
  end
end

#filtered_set(key, value, clean = false) ⇒ Object



199
200
201
202
203
204
205
206
207
208
# File 'lib/scout/tsv/util/filter.rb', line 199

def filtered_set(key, value, clean = false)
  if filters.empty?
    self.send(:unfiltered_set, key, value, clean)
  else
    filters.each do |filter| 
      filter.add key if filter.match_entry key, value
    end
    self.send(:unfiltered_set, key, value, clean)
  end
end

#filtered_valuesObject



218
219
220
221
222
223
224
225
# File 'lib/scout/tsv/util/filter.rb', line 218

def filtered_values 
  if filters.empty?  
    self.send(:unfiltered_values) 
  else 
    ids = filters.inject(nil){|list,filter| list.nil? ? filter.ids.dup : Misc.intersect_sorted_arrays(list, filter.ids.dup)}
    self.send :values_at, *ids
  end
end

#pop_filterObject



274
275
276
# File 'lib/scout/tsv/util/filter.rb', line 274

def pop_filter
  filters.pop.add_unsaved if filters.any?
end

#sizeObject



278
279
280
# File 'lib/scout/tsv/util/filter.rb', line 278

def size
  filters.empty? ? super : filters.collect{|f| f.ids.length }.min
end