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 |
# 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 = TSV.setup Persist.open_tokyocabinet(persistence, false, :list) @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 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
124 125 126 |
# File 'lib/rbbt/tsv/filter.rb', line 124 def add(id) unsaved.push id end |
#add_unsaved ⇒ Object
108 109 110 111 |
# File 'lib/rbbt/tsv/filter.rb', line 108 def add_unsaved save(Misc.merge_sorted_arrays(unsaved.sort, saved || [])) if unsaved.any? unsaved.clear end |
#clean ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/rbbt/tsv/filter.rb', line 128 def clean add_unsaved if persistence and persistence.include? self.key restore = ! persistence.write? persistence.write unless persistence.write? persistence.delete self.key persistence.read if restore else @list = nil end end |
#ids ⇒ Object
113 114 115 116 117 118 119 120 121 122 |
# File 'lib/rbbt/tsv/filter.rb', line 113 def ids add_unsaved list = saved if list.nil? update list = saved end list end |
#key ⇒ Object
63 64 65 66 67 68 69 70 |
# File 'lib/rbbt/tsv/filter.rb', line 63 def key case when String === value value else Marshal.dump(value) end end |
#reset ⇒ Object
140 141 142 143 144 145 146 147 |
# File 'lib/rbbt/tsv/filter.rb', line 140 def reset add_unsaved if persistence persistence.clear else @list = nil end end |
#save(ids) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/rbbt/tsv/filter.rb', line 72 def save(ids) if persistence persistence.write persistence[self.key] = ids persistence.read else if @list.nil? @list = ids else @list.replace ids end end end |
#saved ⇒ Object
98 99 100 101 102 103 104 105 106 |
# File 'lib/rbbt/tsv/filter.rb', line 98 def saved if persistence.nil? return nil if list.nil? list else return nil if not persistence.include?(self.key) persistence[self.key] end end |
#update ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/rbbt/tsv/filter.rb', line 86 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 |