Class: Digger::Index

Inherits:
Struct
  • Object
show all
Defined in:
lib/digger/index.rb

Defined Under Namespace

Classes: NoBlockError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#argsObject

Returns the value of attribute args

Returns:

  • (Object)

    the current value of args



2
3
4
# File 'lib/digger/index.rb', line 2

def args
  @args
end

#patternObject

Returns the value of attribute pattern

Returns:

  • (Object)

    the current value of pattern



2
3
4
# File 'lib/digger/index.rb', line 2

def pattern
  @pattern
end

Class Method Details

.batch(entities, cocurrence = 1, &block) ⇒ Object

Raises:



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/digger/index.rb', line 20

def self.batch(entities, cocurrence = 1, &block)
  raise NoBlockError, "No block given" unless block

  if cocurrence > 1
    results = {}
    entities.each_slice(cocurrence) do |group|
      threads = []
      group.each do |entity|
        threads << Thread.new(entity) do |ent|
          results[ent] = block.call(ent)
        end
      end
      threads.each{|thread| thread.join}
    end
    entities.map{|ent| results[ent]}
  else
    entities.map{|ent| block.call(ent) }
  end
end

Instance Method Details

#pattern_applied_url(arg) ⇒ Object



16
17
18
# File 'lib/digger/index.rb', line 16

def pattern_applied_url(arg)
  pattern.gsub('*').each_with_index{|_, i| arg[i]}
end

#process(cocurrence = 1, &block) ⇒ Object



5
6
7
# File 'lib/digger/index.rb', line 5

def process(cocurrence = 1, &block)
  Index.batch(urls, cocurrence, &block)
end

#urlsObject



9
10
11
12
13
14
# File 'lib/digger/index.rb', line 9

def urls
  @urls ||= begin
    args = self.args.map{|a| (a.respond_to? :each) ? a.to_a : [a]}
    args.shift.product(*args).map{|arg| pattern_applied_url(arg)}
  end
end