Sha256: 2a3211da23bcf84a816953f2e38a492dd05a4e461579f897a4c2dab2edd9fe55

Contents?: true

Size: 1.58 KB

Versions: 3

Compression:

Stored size: 1.58 KB

Contents

module Rao
  module ServiceChain
    module Step
      class Base
        attr_accessor :service, :service_name, :label, :chain

        def initialize(options = {})
          @service = options.delete(:service)
          @chain = options.delete(:chain)
          @completed_if = options.delete(:completed_if)
          @display = options.delete(:display) || ->() { true }
          @service_name = @service.try(:name)
          @label = service.try(:model_name).try(:human)
          @url = options.delete(:url)
        end

        def url(context = nil)
          return nil if context.nil?
          return context.instance_exec(@service, &@url) if @url.respond_to?(:call)
          @url ||= context.url_for([:new, @service, only_path: true])
        end

        def completed?
          return nil unless @completed_if.respond_to?(:call)
          @chain.instance_exec(@service, &@completed_if)
        end

        def pending?
          !completed?
        end

        def completion_status
          completed? ? :completed : :pending
        end

        def actual?
          return false if @chain.actual_step.nil?
          @chain.actual_step.service == @service
        end

        def to_hash(context = nil)
          {
            service: service,
            service_name: service.name,
            label: label,
            completed: completed?,
            pending: pending?,
            completion_status: completion_status,
            actual: actual?,
            url: url(context)
          }
        end

        def display?
          !!@display.call
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rao-service_chain-0.0.38.pre app/service_steps/rao/service_chain/step/base.rb
rao-service_chain-0.0.36.pre app/service_steps/rao/service_chain/step/base.rb
rao-service_chain-0.0.35.pre app/service_steps/rao/service_chain/step/base.rb