Sha256: e2303bd97bc2a23cc249be749f32494ab488f3bd3982b8069ccd4c16e2dd60c5

Contents?: true

Size: 1.59 KB

Versions: 6

Compression:

Stored size: 1.59 KB

Contents

class ProconBypassMan::Procon::Macro
  class NestedStep
    def initialize(value)
      @hash = value
      unless @hash[:end_at]
        @hash[:end_at] = (Time.now + @hash[:continue_for]).round(4)
      end
    end

    def over_end_at?
      (@hash[:end_at] < Time.now).tap do |result|
        if result
          ProconBypassMan.logger.debug { "[Macro] nested step is finished(#{@hash})" }
        end
      end
    end

    def next_step
      incr_step_index!

      debug_incr_called_count!
      if step = current_step
        return step
      else
        reset_step_index!
        return current_step
      end
    end

    private

    def current_step
      @hash[:steps][step_index]
    end

    def step_index
      @hash[:step_index]
    end

    def incr_step_index!
      if step_index
        @hash[:step_index] += 1
      else
        @hash[:step_index] = 0
      end
    end

    def reset_step_index!
      @hash[:step_index] = 0
    end

    def debug_incr_called_count!
      @hash[:debug_called_count] ||= 0
      @hash[:debug_called_count] += 1
    end
  end

  attr_accessor :name, :steps

  def initialize(name: , steps: )
    self.name = name
    self.steps = steps
  end

  def next_step
    step = steps.first
    if step.is_a?(Symbol)
      return steps.shift
    end

    if step.is_a?(Hash)
      nested_step = NestedStep.new(step)
      if nested_step.over_end_at?
        steps.shift # NestedStepを破棄する
        return next_step
      else
        return nested_step.next_step
      end
    end
  end

  def finished?
    steps.empty?
  end

  def ongoing?
    !finished?
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
procon_bypass_man-0.1.20.2 lib/procon_bypass_man/procon/macro.rb
procon_bypass_man-0.1.20.1 lib/procon_bypass_man/procon/macro.rb
procon_bypass_man-0.1.20 lib/procon_bypass_man/procon/macro.rb
procon_bypass_man-0.1.19.1 lib/procon_bypass_man/procon/macro.rb
procon_bypass_man-0.1.19 lib/procon_bypass_man/procon/macro.rb
procon_bypass_man-0.1.18 lib/procon_bypass_man/procon/macro.rb