Class: RubyJobs::JobBase::Job

Inherits:
Object
  • Object
show all
Includes:
JobLogging
Defined in:
lib/job_base/job.rb

Constant Summary collapse

@@bootstrapped =
false
@@requires_rails =
nil
@@puts_overridden =
false

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from JobLogging

#init_logger, #log, #logger, #progress

Constructor Details

#initialize(defaults = nil) ⇒ Job

Returns a new instance of Job.



12
13
14
15
# File 'lib/job_base/job.rb', line 12

def initialize(defaults=nil)
  @values = self.class.defaults.dup
  @values.merge! defaults unless defaults.nil?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



177
178
179
180
181
182
183
184
185
# File 'lib/job_base/job.rb', line 177

def method_missing(method, *args, &block)
  if method.to_s.end_with? "="
    @values[method.to_s[0..-2].to_sym] = args.first
  elsif @values.include? method
    @values[method]
  else
    super
  end
end

Class Attribute Details

.blockObject (readonly)

Returns the value of attribute block.



87
88
89
# File 'lib/job_base/job.rb', line 87

def block
  @block
end

.instance_blocksObject (readonly)

Returns the value of attribute instance_blocks.



87
88
89
# File 'lib/job_base/job.rb', line 87

def instance_blocks
  @instance_blocks
end

.instance_hashesObject (readonly)

Returns the value of attribute instance_hashes.



87
88
89
# File 'lib/job_base/job.rb', line 87

def instance_hashes
  @instance_hashes
end

.puts_blockObject (readonly)

Returns the value of attribute puts_block.



87
88
89
# File 'lib/job_base/job.rb', line 87

def puts_block
  @puts_block
end

Class Method Details

.const_missing(const) ⇒ Object



170
171
172
173
174
# File 'lib/job_base/job.rb', line 170

def const_missing(const)
  initialize
  value = defaults[const.to_s.downcase.to_sym]
  value.nil? ? Object.const_get(const) : value
end

.default(key = nil, value = nil, &block) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/job_base/job.rb', line 146

def default(key=nil, value=nil, &block)
  initialize
  if value.nil? && block_given?
    @default_blocks[key] = block
  else
    if value.nil? && key.is_a?(Hash)
      key.each do |key, value|
        @defaults[key] = value
      end
    else
      @defaults[key] = value
    end
  end
end

.defaultsObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/job_base/job.rb', line 72

def defaults
  default_blocks = @default_blocks.dup
  @default_blocks = {}
  default_blocks.each do |k,b|
    if k.nil?
      b.call.each do |key, value|
        @defaults[key] = value
      end
    else
      @defaults[k] = b.call
    end
  end
  @defaults
end

.define_puts(&block) ⇒ Object



165
166
167
168
# File 'lib/job_base/job.rb', line 165

def define_puts(&block)
  initialize
  @puts_block = block
end

.initializeObject



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/job_base/job.rb', line 60

def initialize
  unless @initialized
    @initialized = true
    @defaults = {}
    @default_blocks = {}
    @instance_hashes = {}
    @instance_blocks = {}
    @loaded_rails = false
    load_rails if @@requires_rails || @@requires_rails.nil?
  end
end

.instance(name = nil, hash = nil, &block) ⇒ Object



138
139
140
141
142
143
144
# File 'lib/job_base/job.rb', line 138

def instance(name=nil, hash=nil, &block)
  initialize
  if name
    @instance_hashes[name] = hash unless hash.nil?
    @instance_blocks[name] = block
  end
end

.load_railsObject



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/job_base/job.rb', line 122

def load_rails
  initialize
  begin
    load_rails = require File.expand_path('config/boot', APP_ROOT)
    load_rails &&= require APP_PATH
    if load_rails
      puts "loading rails..."
      Rails.application.require_environment!
      return @loaded_rails = true
    end
    false
  rescue Exception
    false
  end
end

.railsObject



116
117
118
119
120
# File 'lib/job_base/job.rb', line 116

def rails
  load_rails
  return yield Rails if block_given?
  Rails
end

.requires_rails=(value) ⇒ Object



161
162
163
# File 'lib/job_base/job.rb', line 161

def requires_rails=(value)
  @@requires_rails ||= value
end

.run(instance_or_values = nil, &block) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/job_base/job.rb', line 89

def run(instance_or_values=nil, &block)
  initialize
  if instance_or_values.is_a? Hash
    values = instance_or_values
    job = self.new
    job.run values, &block
  else
    instance = instance_or_values
    run = true
    if block_given?
      @block = block
      if !@@bootstrapped && (!defined?(Rails) || @loaded_rails)
        instance = ARGV.first || instance
        @@bootstrapped = true
      else
        run = false
      end
    end

    if run
      job = self.new
      job.load_instance instance.to_sym if instance
      job.run
    end
  end
end

Instance Method Details

#_puts(message) ⇒ Object



206
207
208
209
210
211
212
213
# File 'lib/job_base/job.rb', line 206

def _puts(message)
  if @puts_overridden
    super
    message
  else
    puts(message)
  end
end

#const_missing(const) ⇒ Object



187
188
189
# File 'lib/job_base/job.rb', line 187

def const_missing(const)
  @values[const.to_s.downcase.to_sym]
end

#define_puts(&block) ⇒ Object Also known as: def_puts



191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/job_base/job.rb', line 191

def define_puts(&block)
  if block_given?
    @puts_block = block
    if @running && (!@@puts_overridden || @puts_overridden)
      @@puts_overridden = true
      @puts_overridden = true
      Kernel.send(:define_method, :puts) do |m|
        block.call m
        nil
      end
    end
  end
end

#instanceObject



17
18
19
# File 'lib/job_base/job.rb', line 17

def instance
  @instance
end

#job_nameObject



21
22
23
# File 'lib/job_base/job.rb', line 21

def job_name
  self.class.name.gsub(/([^$])([A-Z])/, '\1_\2').downcase # underscore without rails
end

#load_instance(instance, values = {}) ⇒ Object



215
216
217
218
219
220
221
222
223
# File 'lib/job_base/job.rb', line 215

def load_instance(instance, values={})
  @values.merge! self.class.instance_hashes[instance].merge(values) if self.class.instance_hashes.include?(instance)
  block = self.class.instance_blocks[instance]
  if block
    block_values = instance_exec @values, &block
    @values.merge! block_values if block_values.is_a? Hash
  end
  @instance = instance
end

#run(values = nil, &block) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/job_base/job.rb', line 25

def run(values=nil, &block)
  @running = true

  puts_block = @puts_block || self.class.puts_block
  if puts_block && !@@puts_overridden
    @@puts_overridden = true
    @puts_overridden = true

    Kernel.send(:define_method, :puts) do |m|
      puts_block.call m
      nil
    end
  end

  @values.merge! values unless values.nil?
  if block_given?
    block_values = instance_exec @values, &block
    @values.merge! block_values if block_values.is_a? Hash
  end

  result = instance_exec @values, &self.class.block

  @running = false

  if @puts_overridden
    Kernel.send :remove_method, :puts
    Kernel.send :alias_method, :puts, :_puts
    @puts_overridden = false
    @@puts_overridden = false
  end

  result
end