Sha256: e3c269b0699cd94e4ef26bd757096d8d18730cfa8ca5cd401f2b209265f0c4ae

Contents?: true

Size: 1.59 KB

Versions: 3

Compression:

Stored size: 1.59 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?

              steps.each { |step| step.validate! && 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

3 entries across 3 versions & 1 rubygems

Version Path
convenient_service-0.9.0 lib/convenient_service/service/plugins/has_result_steps/entities/step_collection.rb
convenient_service-0.8.0 lib/convenient_service/service/plugins/has_result_steps/entities/step_collection.rb
convenient_service-0.7.0 lib/convenient_service/service/plugins/has_result_steps/entities/step_collection.rb