Class: Filtered::Filter

Inherits:
Object
  • Object
show all
Defined in:
lib/rbbt/tsv/filter.rb

Overview

{{{ FILTER

Instance Attribute Summary collapse

Instance Method Summary collapse

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
# 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
  else
    raise "Unknown match: #{ @match }"
  end
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



22
23
24
# File 'lib/rbbt/tsv/filter.rb', line 22

def data
  @data
end

#fieldnumObject

Returns the value of attribute fieldnum.



22
23
24
# File 'lib/rbbt/tsv/filter.rb', line 22

def fieldnum
  @fieldnum
end

#listObject

Returns the value of attribute list.



22
23
24
# File 'lib/rbbt/tsv/filter.rb', line 22

def list
  @list
end

#matchObject

Returns the value of attribute match.



22
23
24
# File 'lib/rbbt/tsv/filter.rb', line 22

def match
  @match
end

#persistenceObject

Returns the value of attribute persistence.



23
24
25
# File 'lib/rbbt/tsv/filter.rb', line 23

def persistence
  @persistence
end

#unsavedObject

Returns the value of attribute unsaved.



22
23
24
# File 'lib/rbbt/tsv/filter.rb', line 22

def unsaved
  @unsaved
end

#valueObject

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



126
127
128
# File 'lib/rbbt/tsv/filter.rb', line 126

def add(id)
  unsaved.push id
end

#add_unsavedObject



110
111
112
113
# File 'lib/rbbt/tsv/filter.rb', line 110

def add_unsaved
  save(Misc.merge_sorted_arrays(unsaved.sort, saved || [])) if unsaved.any?
  unsaved.clear
end

#cleanObject



130
131
132
133
134
135
136
137
138
139
140
# File 'lib/rbbt/tsv/filter.rb', line 130

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

#idsObject



115
116
117
118
119
120
121
122
123
124
# File 'lib/rbbt/tsv/filter.rb', line 115

def ids
  add_unsaved

  list = saved
  if list.nil?
    update
    list = saved
  end
  list
end

#keyObject



65
66
67
68
69
70
71
72
# File 'lib/rbbt/tsv/filter.rb', line 65

def key
  case 
  when String === value
    value
  else
    Marshal.dump(value)
  end
end

#resetObject



142
143
144
145
146
147
148
149
# File 'lib/rbbt/tsv/filter.rb', line 142

def reset
  add_unsaved
  if persistence
    persistence.clear
  else
    @list = nil
  end
end

#save(ids) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/rbbt/tsv/filter.rb', line 74

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

#savedObject



100
101
102
103
104
105
106
107
108
# File 'lib/rbbt/tsv/filter.rb', line 100

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

#updateObject



88
89
90
91
92
93
94
95
96
97
98
# File 'lib/rbbt/tsv/filter.rb', line 88

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