Module: Tengine::Job::Jobnet::StateTransition::ClassMethods

Defined in:
lib/tengine/job/jobnet/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
# File 'lib/tengine/job/jobnet/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(<<-EOS, __FILE__, __LINE__ + 1)
    if method_defined?(:#{original_method})
      raise "Already available_on #{method_name}"
    end
    alias #{original_method} #{method_name}

    def #{method_name}(*args, &block)
      case self.phase_key
      when #{available_phase_keys.map(&:inspect).join(', ')} then
        #{original_method}(*args, &block)
      #{ignore_case}
      else
        raise Tengine::Job::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}"
      end
    end
  EOS

end