Sha256: 65084b96652e921780d8353d0577045b10e79d754837e0ed78b7071a45f53243

Contents?: true

Size: 1.94 KB

Versions: 3

Compression:

Stored size: 1.94 KB

Contents

# frozen_string_literal: true

module ConvenientService
  module Service
    module Plugins
      module HasResultSteps
        module Entities
          class Method
            module Commands
              class DefineMethodInContainer < Support::Command
                include Support::Delegate

                attr_reader :method, :container, :index

                delegate :key, :name, to: :method

                def initialize(method:, container:, index:)
                  @method = method
                  @container = container
                  @index = index
                end

                def call
                  ##
                  # NOTE: Assignement in the beginning for easier debugging.
                  #
                  <<~RUBY.tap { |code| container.klass.class_eval(code, __FILE__, __LINE__ + 1) }
                    def #{name}
                      step, key, method_name = steps[#{index}], :#{key}, :#{name}

                      raise #{not_completed_step_error}.new(step: step, method_name: method_name) unless step.completed?

                      raise #{not_existing_step_result_data_attribute_error}.new(step: step, key: key) unless step.result.data.has_attribute?(key)

                      step.result.data[key]
                    end
                  RUBY

                  true
                end

                private

                def not_completed_step_error
                  <<~RUBY.chomp
                    ::ConvenientService::Service::Plugins::HasResultSteps::Entities::Method::Errors::NotCompletedStep
                  RUBY
                end

                def not_existing_step_result_data_attribute_error
                  <<~RUBY.chomp
                    ::ConvenientService::Service::Plugins::HasResultSteps::Entities::Method::Errors::NotExistingStepResultDataAttribute
                  RUBY
                end
              end
            end
          end
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
convenient_service-0.6.0 lib/convenient_service/service/plugins/has_result_steps/entities/method/commands/define_method_in_container.rb
convenient_service-0.5.0 lib/convenient_service/service/plugins/has_result_steps/entities/method/commands/define_method_in_container.rb
convenient_service-0.4.0 lib/convenient_service/service/plugins/has_result_steps/entities/method/commands/define_method_in_container.rb