Module: Coiasira::Jobs

Defined in:
lib/coiasira/jobs.rb

Constant Summary collapse

@@job_paths =
[]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.find_class(name) ⇒ Object



78
79
80
# File 'lib/coiasira/jobs.rb', line 78

def find_class(name)
  "#{name.camelize}Job".constantize
end

.has_job?(name) ⇒ Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/coiasira/jobs.rb', line 69

def has_job?(name)
  possible_jobs.include?(name)
end

.job_pathsObject



8
9
10
# File 'lib/coiasira/jobs.rb', line 8

def self.job_paths
  @@job_paths
end

.job_paths=(obj) ⇒ Object



15
16
17
# File 'lib/coiasira/jobs.rb', line 15

def self.job_paths=(obj)
  @@job_paths = obj
end

.load_job(name) ⇒ Object

Raises:

  • (NameError)


73
74
75
76
# File 'lib/coiasira/jobs.rb', line 73

def load_job(name)
  raise NameError, name unless has_job?(name)
  JobWrapper.new(name)
end

.normalize_paths(paths) ⇒ Object

Returns an array of paths, cleaned of double-slashes and relative path references.

  • “\" and ”//“ become ”\“ or ”/“.

  • “/foo/bar/../config” becomes “/foo/config”.

The returned array is sorted by length, descending.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/coiasira/jobs.rb', line 27

def normalize_paths(paths)
  # do the hokey-pokey of path normalization...
  paths = paths.collect do |path|
    path = path.
      gsub("//", "/").           # replace double / chars with a single
      gsub("\\\\", "\\").        # replace double \ chars with a single
      gsub(%r{(.)[\\/]$}, '\1')  # drop final / or \ if path ends with it

    # eliminate .. paths where possible
    re = %r{[^/\\]+[/\\]\.\.[/\\]}
    path.gsub!(re, "") while path.match(re)
    path
  end

  # start with longest path, first
  paths = paths.uniq.sort_by { |path| - path.length }
end

.possible_jobsObject

Returns the array of job names currently available.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/coiasira/jobs.rb', line 46

def possible_jobs
  unless @possible_jobs
    @possible_jobs = []
    paths = job_paths.select { |path| File.directory?(path) && path != "." }

    seen_paths = Hash.new {|h, k| h[k] = true; false}
    normalize_paths(paths).each do |load_path|
      Dir["#{load_path}/**/*_job.rb"].collect do |path|
        next if seen_paths[path.gsub(%r{^\.[/\\]}, "")]

        job_name = path[(load_path.length + 1)..-1]

        job_name.gsub!(/_job\.rb\Z/, '')
        @possible_jobs << job_name
      end
    end

    # remove duplicates
    @possible_jobs.uniq!
  end
  @possible_jobs
end

Instance Method Details

#job_pathsObject



12
13
14
# File 'lib/coiasira/jobs.rb', line 12

def job_paths
  @@job_paths
end

#job_paths=(obj) ⇒ Object



18
19
20
# File 'lib/coiasira/jobs.rb', line 18

def job_paths=(obj)
  @@job_paths = obj
end