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.



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
# File 'lib/rbbt/tsv/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 = TSV.setup Persist.open_tokyocabinet(persistence, false, :list)
    @persistence.read
  end

  @list = nil
  case
  when @match.match(/field:(.*)/)
    @fieldnum = data.identify_field $1
    class << self
      self
    end.class_eval <<-EOC
      def match_entry(entry)
        value = entry[@fieldnum] 
        value == @value or (Array === value and value.include? @value)
      end
    EOC
  end
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



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

def data
  @data
end

#fieldnumObject

Returns the value of attribute fieldnum.



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

def fieldnum
  @fieldnum
end

#listObject

Returns the value of attribute list.



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

def list
  @list
end

#matchObject

Returns the value of attribute match.



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

def match
  @match
end

#persistenceObject

Returns the value of attribute persistence.



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

def persistence
  @persistence
end

#unsavedObject

Returns the value of attribute unsaved.



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

def unsaved
  @unsaved
end

#valueObject

Returns the value of attribute value.



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

def value
  @value
end

Instance Method Details

#add(id) ⇒ Object



114
115
116
# File 'lib/rbbt/tsv/filter.rb', line 114

def add(id)
  unsaved.push id
end

#add_unsavedObject



98
99
100
101
# File 'lib/rbbt/tsv/filter.rb', line 98

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

#cleanObject



118
119
120
121
122
123
124
125
126
127
128
# File 'lib/rbbt/tsv/filter.rb', line 118

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



103
104
105
106
107
108
109
110
111
112
# File 'lib/rbbt/tsv/filter.rb', line 103

def ids
  add_unsaved

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

#keyObject



53
54
55
56
57
58
59
60
# File 'lib/rbbt/tsv/filter.rb', line 53

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

#resetObject



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

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

#save(ids) ⇒ Object



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

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



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

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



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

def update
  ids = []

  data.with_unnamed do
    data.unfiltered_each do |key, entry|
      ids << key if match_entry(entry)
    end
  end

  save(ids.sort)
end