Sha256: 3921c1690aca34bfb9689bfc252a695a2db86f4c0b83ea3b308a7e15d6629d5b

Contents?: true

Size: 1.72 KB

Versions: 2

Compression:

Stored size: 1.72 KB

Contents

# frozen_string_literal: true

module ConvenientService
  module Service
    module Plugins
      module HasResultSteps
        module Entities
          class StepCollection
            include ::Enumerable

            attr_reader :steps

            def initialize
              @steps = []
            end

            ##
            # TODO: Specs.
            #
            def commit!
              return false if committed?

              ##
              # IMPORTANT: Temporarily removed `step.validate!` since it is neither thread-safe nor idempotent.
              #
              steps.each { |step| step.define! }.freeze

              true
            end

            ##
            # TODO: Specs.
            #
            def committed?
              steps.frozen?
            end

            def each(&block)
              steps.each(&block)
            end

            ##
            # Returns step by index.
            #
            # @param index [Integer]
            # @return [ConvenientService::Service::Plugins::HasResultSteps::Entities::Step]
            #
            # @note Works in a similar way as `Array#[]`.
            # @see https://ruby-doc.org/core-2.7.0/Array.html#method-i-5B-5D
            #
            def [](index)
              steps[index]
            end

            def <<(step)
              steps << step.copy(overrides: {kwargs: {index: next_available_index}})
            end

            def ==(other)
              return unless other.instance_of?(self.class)

              return false if steps != other.steps

              true
            end

            private

            def next_available_index
              steps.size
            end
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
convenient_service-0.10.1 lib/convenient_service/service/plugins/has_result_steps/entities/step_collection.rb
convenient_service-0.10.0 lib/convenient_service/service/plugins/has_result_steps/entities/step_collection.rb