Sha256: 6c5f6e1c144ae79728c9f211777a85d065595ecd9b72be84d5ea7de6dadbb0be

Contents?: true

Size: 1.25 KB

Versions: 3

Compression:

Stored size: 1.25 KB

Contents

# -*- coding: utf-8 -*-
require 'tengine/job/runtime'

module Tengine::Job::Runtime::StateTransition

  def self.included(mod)
    mod.extend(ClassMethods)
  end

  module ClassMethods
    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
            update_with_lock do
              #{original_method}(*args, &block)
            end
          #{ignore_case}
          else
            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}"
          end
        end
      EOS

    end
  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
tengine_job-1.2.2 lib/tengine/job/runtime/state_transition.rb
tengine_job-1.2.1 lib/tengine/job/runtime/state_transition.rb
tengine_job-1.2.0 lib/tengine/job/runtime/state_transition.rb