Class: Filtered::Filter
- Inherits:
-
Object
- Object
- Filtered::Filter
- Defined in:
- lib/scout/tsv/util/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.
24 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 65 66 67 |
# File 'lib/scout/tsv/util/filter.rb', line 24 def initialize(data, match, value, persistence = nil) @data = data @match = match @value = value @unsaved = [] case when Hash === persistence @persistence = persistence when String === persistence @persistence = begin file = ScoutCabinet.open(persistence, true, "HDB") TSV.setup file, :type => :list file.extend TSVAdapter file end @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.
21 22 23 |
# File 'lib/scout/tsv/util/filter.rb', line 21 def data @data end |
#fieldnum ⇒ Object
Returns the value of attribute fieldnum.
21 22 23 |
# File 'lib/scout/tsv/util/filter.rb', line 21 def fieldnum @fieldnum end |
#list ⇒ Object
Returns the value of attribute list.
21 22 23 |
# File 'lib/scout/tsv/util/filter.rb', line 21 def list @list end |
#match ⇒ Object
Returns the value of attribute match.
21 22 23 |
# File 'lib/scout/tsv/util/filter.rb', line 21 def match @match end |
#persistence ⇒ Object
Returns the value of attribute persistence.
22 23 24 |
# File 'lib/scout/tsv/util/filter.rb', line 22 def persistence @persistence end |
#unsaved ⇒ Object
Returns the value of attribute unsaved.
21 22 23 |
# File 'lib/scout/tsv/util/filter.rb', line 21 def unsaved @unsaved end |
#value ⇒ Object
Returns the value of attribute value.
21 22 23 |
# File 'lib/scout/tsv/util/filter.rb', line 21 def value @value end |
Instance Method Details
#add(id) ⇒ Object
132 133 134 |
# File 'lib/scout/tsv/util/filter.rb', line 132 def add(id) unsaved.push id end |
#add_unsaved ⇒ Object
116 117 118 119 |
# File 'lib/scout/tsv/util/filter.rb', line 116 def add_unsaved save(Misc.merge_sorted_arrays(unsaved.sort, saved || [])) if unsaved.any? unsaved.clear end |
#clean ⇒ Object
136 137 138 139 140 141 142 143 144 145 |
# File 'lib/scout/tsv/util/filter.rb', line 136 def clean add_unsaved if persistence and persistence.include? self.key persistence.write_and_close do persistence.delete self.key end else @list = nil end end |
#ids ⇒ Object
121 122 123 124 125 126 127 128 129 130 |
# File 'lib/scout/tsv/util/filter.rb', line 121 def ids add_unsaved list = saved if list.nil? update list = saved end list end |
#key ⇒ Object
69 70 71 72 73 74 75 76 |
# File 'lib/scout/tsv/util/filter.rb', line 69 def key case when String === value value else Marshal.dump(value) end end |
#reset ⇒ Object
147 148 149 150 151 152 153 154 155 156 |
# File 'lib/scout/tsv/util/filter.rb', line 147 def reset add_unsaved if persistence persistence.write_and_close do persistence.clear end else @list = nil end end |
#save(ids) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/scout/tsv/util/filter.rb', line 78 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
104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/scout/tsv/util/filter.rb', line 104 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
92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/scout/tsv/util/filter.rb', line 92 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 |