Class: Bricolage::JobClass

Inherits:
Object
  • Object
show all
Defined in:
lib/bricolage/jobclass.rb

Constant Summary collapse

CLASSES =
{}
LOAD_PATHES =
[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id) ⇒ JobClass

Returns a new instance of JobClass.



61
62
63
64
65
66
67
# File 'lib/bricolage/jobclass.rb', line 61

def initialize(id)
  @id = id
  @parameters = nil
  @parameters_filter = nil   # optional
  @declarations = nil
  @script = nil
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



69
70
71
# File 'lib/bricolage/jobclass.rb', line 69

def id
  @id
end

Class Method Details

.add_load_path(path) ⇒ Object



22
23
24
# File 'lib/bricolage/jobclass.rb', line 22

def JobClass.add_load_path(path)
  LOAD_PATHES.push path
end

.define(id, &block) ⇒ Object

Raises:



12
13
14
15
16
17
18
# File 'lib/bricolage/jobclass.rb', line 12

def JobClass.define(id, &block)
  id = id.to_s
  raise FatalError, "duplicated job class: #{@id.inspect}" if CLASSES[id]
  c = new(id)
  c.instance_exec(c, &block)
  CLASSES[id] = c
end

.each(&block) ⇒ Object



57
58
59
# File 'lib/bricolage/jobclass.rb', line 57

def JobClass.each(&block)
  CLASSES.each_value(&block)
end

.get(id) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/bricolage/jobclass.rb', line 37

def JobClass.get(id)
  unless CLASSES[id.to_s]
    begin
      path = LOAD_PATHES.map {|prefix| prefix + "#{id}.rb" }.detect(&:exist?)
      raise ParameterError, "no such job class: #{id}" unless path
      ::Bricolage.module_eval File.read(path), path.to_path, 1
    rescue SystemCallError => err
      raise FatalError, "could not load job class: #{id}: #{err.message}"
    end
    raise FatalError, "job class file loaded but required job class is not defined: #{id}" unless CLASSES[id.to_s]
  end
  CLASSES[id.to_s]
end

.listObject



51
52
53
54
55
# File 'lib/bricolage/jobclass.rb', line 51

def JobClass.list
  LOAD_PATHES.map {|dir|
    Dir.glob("#{dir}/*.rb").map {|path| File.basename(path, '.rb') }
  }.flatten.uniq.sort
end

.load_path_for_lib_file(path) ⇒ Object



30
31
32
33
# File 'lib/bricolage/jobclass.rb', line 30

def JobClass.load_path_for_lib_file(path)
  srcdir = Pathname(path).realpath.parent.parent.parent.cleanpath
  srcdir + 'jobclass'
end

.primary_load_pathObject



26
27
28
# File 'lib/bricolage/jobclass.rb', line 26

def JobClass.primary_load_path
  load_path_for_lib_file(__FILE__)
end

Instance Method Details

#declarations(&block) ⇒ Object



93
94
95
# File 'lib/bricolage/jobclass.rb', line 93

def declarations(&block)
  @declarations = block
end

#get_declarations(params) ⇒ Object



97
98
99
# File 'lib/bricolage/jobclass.rb', line 97

def get_declarations(params)
  @declarations ? @declarations.call(params) : Declarations.new
end

#get_parametersObject



79
80
81
82
83
# File 'lib/bricolage/jobclass.rb', line 79

def get_parameters
  Parameters::Declarations.new.tap {|params|
    @parameters.call(params)
  }
end

#get_script(params) ⇒ Object



105
106
107
108
109
# File 'lib/bricolage/jobclass.rb', line 105

def get_script(params)
  Script.new.tap {|script|
    @script.call(params, script)
  }
end

#inspectObject



71
72
73
# File 'lib/bricolage/jobclass.rb', line 71

def inspect
  "\#<#{self.class} #{@id}>"
end

#invoke_parameters_filter(job) ⇒ Object



89
90
91
# File 'lib/bricolage/jobclass.rb', line 89

def invoke_parameters_filter(job)
  @parameters_filter.call(job) if @parameters_filter
end

#parameters(&block) ⇒ Object



75
76
77
# File 'lib/bricolage/jobclass.rb', line 75

def parameters(&block)
  @parameters = block
end

#parameters_filter(&block) ⇒ Object



85
86
87
# File 'lib/bricolage/jobclass.rb', line 85

def parameters_filter(&block)
  @parameters_filter = block
end

#script(&block) ⇒ Object



101
102
103
# File 'lib/bricolage/jobclass.rb', line 101

def script(&block)
  @script = block
end