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



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/rbbt/tsv/filter.rb', line 154

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



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

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



236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/rbbt/tsv/filter.rb', line 236

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



251
252
253
254
255
256
257
258
# File 'lib/rbbt/tsv/filter.rb', line 251

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



222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/rbbt/tsv/filter.rb', line 222

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



184
185
186
187
188
189
190
# File 'lib/rbbt/tsv/filter.rb', line 184

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



203
204
205
206
207
208
209
210
211
# File 'lib/rbbt/tsv/filter.rb', line 203

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



192
193
194
195
196
197
198
199
200
201
# File 'lib/rbbt/tsv/filter.rb', line 192

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



213
214
215
216
217
218
219
220
# File 'lib/rbbt/tsv/filter.rb', line 213

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



269
270
271
# File 'lib/rbbt/tsv/filter.rb', line 269

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

#sizeObject



273
274
275
# File 'lib/rbbt/tsv/filter.rb', line 273

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