Skip to content

Commit

Permalink
refactor(core): make method required for middlewares
Browse files Browse the repository at this point in the history
  • Loading branch information
marian13 committed Oct 11, 2022
1 parent 6c72433 commit 54fee7f
Show file tree
Hide file tree
Showing 14 changed files with 188 additions and 99 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ This library is under heavy development. Public API may be subject to change. Th
use ConvenientService::Plugins::Common::AssignsAttributesInConstructor::UsingActiveModelAttributeAssignment::Concern
end

middlewares method: :initialize do
middlewares :initialize do
use ConvenientService::Plugins::Common::AssignsAttributesInConstructor::UsingActiveModelAttributeAssignment::Middleware
end

Expand All @@ -215,7 +215,7 @@ This library is under heavy development. Public API may be subject to change. Th
use ConvenientService::Plugins::Service::HasResultParamsValidations::UsingActiveModelValidations::Concern
end

middlewares method: :result do
middlewares :result do
use ConvenientService::Plugins::Service::HasResultParamsValidations::UsingActiveModelValidations::Middleware
end
end
Expand Down Expand Up @@ -347,7 +347,7 @@ This library is under heavy development. Public API may be subject to change. Th
use ConvenientService::Plugins::Service::HasResultParamsValidations::UsingDryValidation::Concern
end

middlewares method: :result do
middlewares :result do
use ConvenientService::Plugins::Service::HasResultParamsValidations::UsingDryValidation::Middleware
end
end
Expand Down
32 changes: 16 additions & 16 deletions lib/convenient_service/configs/standard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ module Standard
#
end

middlewares method: :initialize do
middlewares :initialize do
use Plugins::Common::NormalizesEnv::Middleware

use Plugins::Common::CachesConstructorParams::Middleware
end

middlewares method: :result do
middlewares :result do
use Plugins::Common::NormalizesEnv::Middleware

use Plugins::Service::HasResult::Middleware
Expand All @@ -60,25 +60,25 @@ module Standard
use Plugins::Common::CachesReturnValue::Middleware
end

middlewares method: :success do
middlewares :success do
use Plugins::Common::NormalizesEnv::Middleware

use Plugins::Service::HasResultShortSyntax::Success::Middleware
end

middlewares method: :failure do
middlewares :failure do
use Plugins::Common::NormalizesEnv::Middleware

use Plugins::Service::HasResultShortSyntax::Failure::Middleware
end

middlewares method: :error do
middlewares :error do
use Plugins::Common::NormalizesEnv::Middleware

use Plugins::Service::HasResultShortSyntax::Error::Middleware
end

middlewares method: :step, scope: :class do
middlewares :step, scope: :class do
use Plugins::Common::NormalizesEnv::Middleware

use Plugins::Service::HasResultMethodSteps::Middleware
Expand All @@ -102,55 +102,55 @@ class self::Result
use Plugins::Result::CanRecalculateResult::Concern
end

middlewares method: :success? do
middlewares :success? do
use Plugins::Common::NormalizesEnv::Middleware

use Plugins::Result::MarksResultStatusAsChecked::Middleware
end

middlewares method: :failure? do
middlewares :failure? do
use Plugins::Common::NormalizesEnv::Middleware

use Plugins::Result::MarksResultStatusAsChecked::Middleware
end

middlewares method: :error? do
middlewares :error? do
use Plugins::Common::NormalizesEnv::Middleware

use Plugins::Result::MarksResultStatusAsChecked::Middleware
end

middlewares method: :not_success? do
middlewares :not_success? do
use Plugins::Common::NormalizesEnv::Middleware

use Plugins::Result::MarksResultStatusAsChecked::Middleware
end

middlewares method: :not_failure? do
middlewares :not_failure? do
use Plugins::Common::NormalizesEnv::Middleware

use Plugins::Result::MarksResultStatusAsChecked::Middleware
end

middlewares method: :not_error? do
middlewares :not_error? do
use Plugins::Common::NormalizesEnv::Middleware

use Plugins::Result::MarksResultStatusAsChecked::Middleware
end

middlewares method: :data do
middlewares :data do
use Plugins::Common::NormalizesEnv::Middleware

use Plugins::Result::RaisesOnNotCheckedResultStatus::Middleware
end

middlewares method: :message do
middlewares :message do
use Plugins::Common::NormalizesEnv::Middleware

use Plugins::Result::RaisesOnNotCheckedResultStatus::Middleware
end

middlewares method: :code do
middlewares :code do
use Plugins::Common::NormalizesEnv::Middleware

use Plugins::Result::RaisesOnNotCheckedResultStatus::Middleware
Expand All @@ -172,7 +172,7 @@ class self::Step
use Plugins::Common::HasInternals::Concern
end

middlewares method: :result do
middlewares :result do
use Plugins::Common::NormalizesEnv::Middleware

use Plugins::Common::CachesReturnValue::Middleware
Expand Down
65 changes: 16 additions & 49 deletions lib/convenient_service/core/class_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,46 +25,36 @@ module ClassMethods
def concerns(&configuration_block)
@concerns ||= Entities::Concerns.new(entity: self)

return @concerns unless configuration_block

@concerns.assert_not_included!

@concerns.configure(&configuration_block)
if configuration_block
@concerns.assert_not_included!
@concerns.configure(&configuration_block)
end

@concerns
end

##
# Sets or gets middlewares for a service class.
#
# @overload middlewares
# Returns all instance middewares.
# @return [Hash<Symbol, Hash<Symbol, ConvenientService::Core::Entities::MethodMiddlewares>>]
#
# @overload middlewares(scope:)
# Returns all scoped middewares.
# @param scope [:instance, :class]
# @return [Hash<Symbol, Hash<Symbol, ConvenientService::Core::Entities::MethodMiddlewares>>]
#
# @overload middlewares(method:)
# @overload middlewares(method)
# Returns all instance middlewares for particular method.
# @param method [Symbol] Method name.
# @return [Hash<Symbol, Hash<Symbol, ConvenientService::Core::Entities::MethodMiddlewares>>]
#
# @overload middlewares(method:, scope:)
# @overload middlewares(method, scope:)
# Returns all scoped middlewares for particular method.
# @param method [Symbol] Method name.
# @param scope [:instance, :class]
# @return [Hash<Symbol, Hash<Symbol, ConvenientService::Core::Entities::MethodMiddlewares>>]
#
# @overload middlewares(method:, &configuration_block)
# @overload middlewares(method, &configuration_block)
# Configures instance middlewares for particular method.
# @param method [Symbol] Method name.
# @param configuration_block [Proc] Block that configures middlewares.
# @see https://github.com/marian13/ruby-middleware#a-basic-example
# @return [ConvenientService::Core::Entities::MethodMiddlewares]
#
# @overload middlewares(method:, scope:, &configuration_block)
# @overload middlewares(method, scope:, &configuration_block)
# Configures scoped middlewares for particular method.
# @param method [Symbol] Method name.
# @param scope [:instance, :class]
Expand All @@ -73,45 +63,25 @@ def concerns(&configuration_block)
# @return [ConvenientService::Core::Entities::MethodMiddlewares]
#
# @example Getters
# middlewares
# middlewares(scope: :instance)
# middlewares(scope: :class)
# middlewares(method: :result)
# middlewares(method: :result, scope: :instance)
# middlewares(method: :result, scope: :class)
# middlewares(:result)
# middlewares(:result, scope: :instance)
# middlewares(:result, scope: :class)
#
# @example Setters
# middlewares(method: :result, &configuration_block)
# middlewares(method: :result, scope: :instance, &configuration_block)
# middlewares(method: :result, scope: :class, &configuration_block)
# middlewares(:result, &configuration_block)
# middlewares(:result, scope: :instance, &configuration_block)
# middlewares(:result, scope: :class, &configuration_block)
#
def middlewares(**kwargs, &configuration_block)
scope = kwargs[:scope] || :instance
method = kwargs[:method] || (raise ::ArgumentError if configuration_block)
container = self

def middlewares(method, scope: :instance, &configuration_block)
@middlewares ||= {}

@middlewares[scope] ||= {}
@middlewares[scope][method] ||= Entities::MethodMiddlewares.new(scope: scope, method: method, container: self)

##
# NOTE: Setter.
#
if configuration_block
@middlewares[scope][method] ||= Entities::MethodMiddlewares.new(scope: scope, method: method, container: container)

@middlewares[scope][method].configure(&configuration_block)

@middlewares[scope][method].define!

return @middlewares[scope][method]
end

##
# NOTE: Getter
#
return @middlewares[scope] unless method

@middlewares[scope][method]
end

Expand All @@ -122,9 +92,6 @@ def commit_config!
concerns.include!

ConvenientService.logger.debug { "[Core] Included concerns into `#{self}` | Triggered by `.commit_config!`" }

middlewares(scope: :instance).values.each(&:define!)
middlewares(scope: :class).values.each(&:define!)
end

##
Expand Down
4 changes: 3 additions & 1 deletion lib/convenient_service/core/entities/concerns.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,11 @@ def assert_not_included!
end

##
# @param args [Array]
# @param kwargs [Hash]
# @return [void]
#
def configure(&configuration_block)
def configure(*args, **kwargs, &configuration_block)
stack.instance_exec(&configuration_block)
end

Expand Down
25 changes: 22 additions & 3 deletions lib/convenient_service/core/entities/method_middlewares.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@ def self.resolve_super_method(entity, scope, method)
end

##
# @param args [Array]
# @param kwargs [Hash]
# @return [ConvenientService::Core::Entities::MethodMiddlewares]
#
def configure(&configuration_block)
stack.instance_exec(&configuration_block)
def configure(*args, **kwargs, &configuration_block)
stack.instance_exec(*args, **kwargs, &configuration_block)

self
end
Expand All @@ -60,14 +62,29 @@ def call(env, original)
stack.dup.use(original).call(env.merge(method: method))
end

##
# @param other [ConvenientService::Core::Entities::MethodMiddlewares, Object]
# @return [Boolean, nil]
#
def ==(other)
return unless other.instance_of?(self.class)

return false if scope != other.scope
return false if method != other.method
return false if container != other.container
return false if stack != other.stack

true
end

##
# @return [Array<ConvenientService::Core::Entities::MethodMiddlewares::Entities::Middleware>]
#
def to_a
stack.to_a.map(&:first)
end

private
protected

##
# @!attribute [r] scope
Expand All @@ -94,6 +111,8 @@ def stack
@stack ||= Entities::Stack.new(name: stack_name)
end

private

##
# @return [String]
#
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@ def define_method_middlewares_caller!(scope, method)
def resolve_methods_middlewares_callers(scope)
Commands::ResolveMethodsMiddlewaresCallers.call(scope: scope, container: self)
end

##
# @param other [ConvenientService::Core::Entities::MethodMiddlewares::Entities::Container, Object]
# @return [Boolean, nil]
#
def ==(other)
return unless other.instance_of?(self.class)

return false if service_class != other.service_class

true
end
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def #{method}(*args, **kwargs, &block)
env = {entity: self, args: args, kwargs: kwargs, block: block}
original_method = proc { |env| super(*env[:args], **env[:kwargs], &env[:block]) }
middlewares(method: method, scope: scope).call(env, original_method)
middlewares(method, scope: scope).call(env, original_method)
end
RUBY
end
Expand All @@ -72,7 +72,7 @@ def #{method}(*args, **kwargs, &block)
original_method = proc { |env| super_method.call(*env[:args], **env[:kwargs], &env[:block]) }
middlewares(method: method, scope: scope).call(env, original_method)
middlewares(method, scope: scope).call(env, original_method)
end
RUBY
end
Expand Down
4 changes: 2 additions & 2 deletions lib/convenient_service/core/instance_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ def concerns(&configuration_block)
# @param (see ConvenientService::Core::ClassMethods#middlewares)
# @return [ConvenientService::Core::Entities::MethodMiddlewares]
#
def middlewares(**kwargs, &block)
self.class.middlewares(**kwargs, &block)
def middlewares(*args, **kwargs, &block)
self.class.middlewares(*args, **kwargs, &block)
end

##
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def self.included(service_class)
use Plugins::Service::HasResultParamsValidations::UsingDryValidation::Concern
end

middlewares method: :result do
middlewares :result do
use Plugins::Service::HasResultParamsValidations::UsingDryValidation::Middleware
end
end
Expand Down
Loading

0 comments on commit 54fee7f

Please sign in to comment.