Module: Tengine::Job::Runtime::StateTransition::ClassMethods

Defined in:
lib/tengine/job/runtime/state_transition.rb

Instance Method Summary collapse

Instance Method Details

#available(method_name, options = {}) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/tengine/job/runtime/state_transition.rb', line 11

def available(method_name, options = {})
  original_method = :"__#{method_name}_without_ignore_and_na"
  available_phase_keys = Array(options[:on])
  ignored_phase_keys = Array(options[:ignored])
  ignore_case = ignored_phase_keys.empty? ? "" :
    "when #{ignored_phase_keys.map(&:inspect).join(', ')} then return"
  class_eval("    if method_defined?(:\#{original_method})\n      raise \"Already available_on \#{method_name}\"\n    end\n    alias \#{original_method} \#{method_name}\n\n    def \#{method_name}(*args, &block)\n      case self.phase_key\n      when \#{available_phase_keys.map(&:inspect).join(', ')} then\n        update_with_lock do\n          \#{original_method}(*args, &block)\n        end\n      \#{ignore_case}\n      else\n        raise Tengine::Job::Runtime::Executable::PhaseError, \"\\\#{name_path} \\\#{self.class.name}#\#{method_name} not available when the phase_key of \\\#{self.name_path.inspect} is \\\#{self.phase_key.inspect}\"\n      end\n    end\n  EOS\n\nend\n", __FILE__, __LINE__ + 1)