Class: Filtered::Filter
- Inherits:
-
Object
- Object
- Filtered::Filter
- Defined in:
- lib/rbbt/tsv/filter.rb
Overview
{{{ FILTER
Instance Attribute Summary collapse
-
#data ⇒ Object
Returns the value of attribute data.
-
#fieldnum ⇒ Object
Returns the value of attribute fieldnum.
-
#list ⇒ Object
Returns the value of attribute list.
-
#match ⇒ Object
Returns the value of attribute match.
-
#persistence ⇒ Object
Returns the value of attribute persistence.
-
#unsaved ⇒ Object
Returns the value of attribute unsaved.
-
#value ⇒ Object
Returns the value of attribute value.
Instance Method Summary collapse
- #add(id) ⇒ Object
- #add_unsaved ⇒ Object
- #clean ⇒ Object
- #ids ⇒ Object
-
#initialize(data, match, value, persistence = nil) ⇒ Filter
constructor
A new instance of Filter.
- #key ⇒ Object
- #reset ⇒ Object
- #save(ids) ⇒ Object
- #saved ⇒ Object
- #update ⇒ Object
Constructor Details
#initialize(data, match, value, persistence = nil) ⇒ Filter
Returns a new instance of Filter.
25 26 27 28 29 30 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 |
# File 'lib/rbbt/tsv/filter.rb', line 25 def initialize(data, match, value, persistence = nil) @data = data @match = match @value = value @unsaved = [] case when Hash === persistence @persistence = persistence when String === persistence @persistence = Persist.open_tokyocabinet(persistence, false, :list) @persistence = TSV.setup(@persistence) @persistence.read end @list = nil case when @match == :key @value = Set.new(@value) class << self self end.class_eval <<-EOC def match_entry(key, entry) key == @value or (Set === @value and @value.include? key) end EOC when @match.match(/field:(.*)/) @fieldnum = data.identify_field $1 class << self self end.class_eval <<-EOC def match_entry(key, entry) value = entry[@fieldnum] value == @value or (Array === value and value.include? @value) end EOC else raise "Unknown match: #{ @match }" end end |
Instance Attribute Details
#data ⇒ Object
Returns the value of attribute data.
22 23 24 |
# File 'lib/rbbt/tsv/filter.rb', line 22 def data @data end |
#fieldnum ⇒ Object
Returns the value of attribute fieldnum.
22 23 24 |
# File 'lib/rbbt/tsv/filter.rb', line 22 def fieldnum @fieldnum end |
#list ⇒ Object
Returns the value of attribute list.
22 23 24 |
# File 'lib/rbbt/tsv/filter.rb', line 22 def list @list end |
#match ⇒ Object
Returns the value of attribute match.
22 23 24 |
# File 'lib/rbbt/tsv/filter.rb', line 22 def match @match end |
#persistence ⇒ Object
Returns the value of attribute persistence.
23 24 25 |
# File 'lib/rbbt/tsv/filter.rb', line 23 def persistence @persistence end |
#unsaved ⇒ Object
Returns the value of attribute unsaved.
22 23 24 |
# File 'lib/rbbt/tsv/filter.rb', line 22 def unsaved @unsaved end |
#value ⇒ Object
Returns the value of attribute value.
22 23 24 |
# File 'lib/rbbt/tsv/filter.rb', line 22 def value @value end |
Instance Method Details
#add(id) ⇒ Object
129 130 131 |
# File 'lib/rbbt/tsv/filter.rb', line 129 def add(id) unsaved.push id end |
#add_unsaved ⇒ Object
113 114 115 116 |
# File 'lib/rbbt/tsv/filter.rb', line 113 def add_unsaved save(Misc.merge_sorted_arrays(unsaved.sort, saved || [])) if unsaved.any? unsaved.clear end |
#clean ⇒ Object
133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/rbbt/tsv/filter.rb', line 133 def clean add_unsaved if persistence and persistence.include? self.key persistence.write_and_close do persistence.delete self.key end #restore = ! persistence.write? #persistence.write unless persistence.write? #persistence.delete self.key #persistence.read if restore else @list = nil end end |
#ids ⇒ Object
118 119 120 121 122 123 124 125 126 127 |
# File 'lib/rbbt/tsv/filter.rb', line 118 def ids add_unsaved list = saved if list.nil? update list = saved end list end |
#key ⇒ Object
66 67 68 69 70 71 72 73 |
# File 'lib/rbbt/tsv/filter.rb', line 66 def key case when String === value value else Marshal.dump(value) end end |
#reset ⇒ Object
148 149 150 151 152 153 154 155 156 157 |
# File 'lib/rbbt/tsv/filter.rb', line 148 def reset add_unsaved if persistence persistence.write_and_close do persistence.clear end else @list = nil end end |
#save(ids) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/rbbt/tsv/filter.rb', line 75 def save(ids) if persistence persistence.write_and_close do persistence[self.key] = ids end else if @list.nil? @list = ids else @list.replace ids end end end |
#saved ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/rbbt/tsv/filter.rb', line 101 def saved if persistence.nil? return nil if list.nil? list else return nil if not persistence.include?(self.key) persistence.write_and_close do persistence[self.key] end end end |
#update ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/rbbt/tsv/filter.rb', line 89 def update ids = [] data.with_unnamed do data.unfiltered_each do |key, entry| ids << key if match_entry(key, entry) end end save(ids.sort) end |