Module: Association::Index

Extended by:
Annotation
Defined in:
lib/scout/association/index.rb

Instance Method Summary collapse

Methods included from Annotation

list_tsv_values, load_info, load_tsv, load_tsv_values, obj_tsv_values, resolve_tsv_array, tsv

Instance Method Details

#filter(value_field = nil, target_value = nil, &block) ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/scout/association/index.rb', line 176

def filter(value_field = nil, target_value = nil, &block)
  if block_given?
    matches = []
    if value_field
      through :key, value_field do |key,values|
        pass = block.call values
        matches << key if pass
      end
    else
      through do |key,values|
        pass = block.call [key, values]
        matches << key if pass
      end
    end
    matches

  else
    matches = []
    if target_value
      target_value = [target_value] unless Array === target_value
      through :key, value_field do |key,values|
        pass = (values & target_value).any?
        matches << key if pass
      end
    else
      through :key, value_field do |key,values|
        pass = false
        values.each do |value|
          pass = true unless value.nil? or value.empty? or value.downcase == 'false'
        end
        matches << key if pass
      end
    end
    matches
  end
end

#match(entity) ⇒ Object



88
89
90
91
92
# File 'lib/scout/association/index.rb', line 88

def match(entity)
  return entity.inject([]){|acc,e| acc.concat match(e); acc } if Array === entity
  return [] if entity.nil?
  prefix(entity + "~")
end

#parse_key_fieldObject



84
85
86
# File 'lib/scout/association/index.rb', line 84

def parse_key_field
  @source_field, @target_field, @undirected = key_field.split("~")
end

#reverseObject



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/scout/association/index.rb', line 128

def reverse
  @reverse ||= begin
                 if self.respond_to? :persistence_path
                   persistence_path = self.persistence_path
                   persistence_path = persistence_path.find if Path === persistence_path
                   reverse_filename = persistence_path + '.reverse'
                 else
                   raise "Can only reverse a TokyoCabinet::BDB dataset at the time"
                 end

                 if Open.exist?(reverse_filename)
                   new = Persist.open_tokyocabinet(reverse_filename, false, serializer, TokyoCabinet::BDB)
                   raise "Index has no info: #{reverse_filename}" if new.key_field.nil?
                   new.extend Index
                   new
                 else
                   Open.mkdir File.dirname(reverse_filename) unless Open.exist?(File.dirname(reverse_filename))

                   new = Persist.open_tokyocabinet(reverse_filename, true, serializer, TokyoCabinet::BDB)

                   self.with_unnamed do
                     self.traverse do |key, value|
                       new_key = key.split("~").reverse.join("~")
                       new[new_key] = value
                     end
                   end
                   annotate(new)
                   new.key_field = key_field.split("~").values_at(1,0,2).compact * "~"
                   new.save_annotation_hash
                   new.read_and_close do
                     Association::Index.setup new
                   end
                   new.parse_key_field
                   new.read
                 end

                 new.unnamed = self.unnamed

                 new.undirected = undirected

                 new
               rescue Exception
                 Log.error "Deleting after error reversing database: #{ reverse_filename }"
                 FileUtils.rm reverse_filename if File.exist? reverse_filename
                 raise $!
               end
end

#subset(source, target) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/scout/association/index.rb', line 94

def subset(source, target)
  return [] if source.nil? or target.nil? or source.empty? or target.empty?

  if source == :all or source == "all"
    if target == :all or target == "all"
      return keys
    else
      matches = reverse.subset(target, source)
      return matches.collect{|m| r = m.partition "~"; r.reverse*"" }
    end
  end

  matches = source.uniq.inject([]){|acc,e| 
    if block_given?
      acc.concat(match(e))
    else
      acc.concat(match(e))
    end
  }

  return matches if target == :all or target == "all"

  target_matches = {}

  matches.each{|code| 
    s,sep,t = code.partition "~"
    next if undirected and t > s and source.include? t
    target_matches[t] ||= []
    target_matches[t] << code
  }

  target_matches.values_at(*target.uniq).flatten.compact
end

#to_matrix(value_field = nil, &block) ⇒ Object



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/scout/association/index.rb', line 213

def to_matrix(value_field = nil, &block)
  value_field = fields.first if value_field.nil? and fields.length == 1
  value_pos = identify_field value_field if value_field and String === value_field
  key_field = source_field

  tsv = if value_pos
          AssociationItem.incidence self.keys, key_field do |key|
            if block_given? 
              yield self[key][value_pos]
            else
              self[key][value_pos]
            end
          end
        elsif block_given?
          AssociationItem.incidence self.keys, key_field, &block
        else
          AssociationItem.incidence self.keys, key_field 
        end
end