Module: Filtered

Defined in:
lib/rbbt/tsv/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/rbbt/tsv/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



267
268
269
270
271
272
273
274
# File 'lib/rbbt/tsv/filter.rb', line 267

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



243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/rbbt/tsv/filter.rb', line 243

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 = TSV.setup({}, self.options)

    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



258
259
260
261
262
263
264
265
# File 'lib/rbbt/tsv/filter.rb', line 258

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



229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/rbbt/tsv/filter.rb', line 229

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/rbbt/tsv/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.hash2md5(:values => f.value) : f.value] * "="} * ", "}]"
  end
end

#filtered_keysObject



210
211
212
213
214
215
216
217
218
# File 'lib/rbbt/tsv/filter.rb', line 210

def filtered_keys
  with_monitor(false) do
    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
end

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



199
200
201
202
203
204
205
206
207
208
# File 'lib/rbbt/tsv/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



220
221
222
223
224
225
226
227
# File 'lib/rbbt/tsv/filter.rb', line 220

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



276
277
278
# File 'lib/rbbt/tsv/filter.rb', line 276

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

#sizeObject



280
281
282
# File 'lib/rbbt/tsv/filter.rb', line 280

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