Class: WorkersLoader::Path

Inherits:
Object
  • Object
show all
Defined in:
lib/workers_loader/path.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base, parent = true) ⇒ Path

Returns a new instance of Path.



8
9
10
11
12
13
# File 'lib/workers_loader/path.rb', line 8

def initialize(base, parent = true)
  base = match[1] if match = /(.*)\/$/.match(base)
  path = base.split('/')
  @parent = path.pop if parent
  @base = path.join('/')
end

Instance Attribute Details

#baseObject (readonly)

Returns the value of attribute base.



6
7
8
# File 'lib/workers_loader/path.rb', line 6

def base
  @base
end

#parentObject (readonly)

Returns the value of attribute parent.



6
7
8
# File 'lib/workers_loader/path.rb', line 6

def parent
  @parent
end

Instance Method Details

#base_with_parentObject



39
40
41
# File 'lib/workers_loader/path.rb', line 39

def base_with_parent
  parent.nil? ? base : File.join(base, parent)
end

#class_for(relative_path) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/workers_loader/path.rb', line 26

def class_for(relative_path)
  relative_path.split('/').map(&:camelize).join('::').constantize
rescue NameError
  file = File.join(base, "#{relative_path}.rb")
  raise "File not found: #{file}" unless File.exist?(file)
  load file
  class_for(relative_path)
end

#filesObject



15
16
17
18
19
# File 'lib/workers_loader/path.rb', line 15

def files
  path = File.join(base_with_parent, '{**/*.rb}')
  Dir[path]
    .map { |file| /#{base}\/(.*).rb/.match(file)[1] }
end

#findObject



21
22
23
24
# File 'lib/workers_loader/path.rb', line 21

def find
  files.map { |file| queue_for(file) }
    .reject(&:blank?)
end

#queue_for(relative_path) ⇒ Object



35
36
37
# File 'lib/workers_loader/path.rb', line 35

def queue_for(relative_path)
  Resque.queue_from_class(class_for(relative_path))
end