Skip to content

Commit

Permalink
Merge pull request serradura#63 from serradura/improve_the_usage_of_t…
Browse files Browse the repository at this point in the history
…hen_method

Improve the usage of the method then[NEW FEATURES]
  • Loading branch information
serradura committed Aug 11, 2020
2 parents ee90df7 + a3c40ca commit 0641af1
Show file tree
Hide file tree
Showing 8 changed files with 426 additions and 298 deletions.
5 changes: 2 additions & 3 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ end

pry_byebug_version =
case RUBY_VERSION
when /\A2.2/ then '3.6'
when /\A2.3/ then '3.7'
when /\A2.[23]/ then '3.6'
else '3.9'
end

Expand All @@ -37,7 +36,7 @@ pry_version =
group :development, :test do
gem 'awesome_print', '~> 1.8'

gem 'byebug', '~> 10.0', '>= 10.0.2' if RUBY_VERSION =~ /\A2.2/
gem 'byebug', '~> 10.0', '>= 10.0.2' if RUBY_VERSION =~ /\A2.[23]/

gem 'pry', "~> #{pry_version}"
gem 'pry-byebug', "~> #{pry_byebug_version}"
Expand Down
23 changes: 17 additions & 6 deletions lib/micro/case.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,25 @@ class Case

include Micro::Attributes.without(:strict_initialize)

def self.call(options = {})
def self.call(options = Kind::Empty::HASH)
new(options).__call__
end

def self.then(use_case = nil, &block)
can_yield_self = respond_to?(:yield_self)

if block
raise Error::InvalidInvocationOfTheThenMethod if use_case
raise NotImplementedError if !can_yield_self

yield_self(&block)
else
return yield_self if !use_case && can_yield_self

self.call.then(use_case)
end
end

def self.to_proc
Proc.new { |arg| call(arg) }
end
Expand Down Expand Up @@ -196,11 +211,7 @@ def __get_result(is_success, value, type)
private_constant :MapFailureType
end

def self.case?(arg)
(arg.is_a?(Class) && arg < Case) || arg.is_a?(Case)
end

def self.case_or_flow?(arg)
case?(arg) || arg.is_a?(Cases::Flow)
(arg.is_a?(Class) && arg < Case) || arg.is_a?(Cases::Flow)
end
end
9 changes: 6 additions & 3 deletions lib/micro/case/result.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,17 @@ def then(use_case = nil, attributes = nil, &block)
return failure? ? self : __call_proc(use_case, expected: 'then(-> {})'.freeze)
end

# TODO: Test the then method with a Micro::Cases.{flow,safe_flow}() instance.
raise Error::InvalidInvocationOfTheThenMethod unless ::Micro.case_or_flow?(use_case)

return self if failure?

input = attributes.is_a?(Hash) ? self.data.merge(attributes) : self.data

use_case.__call_and_set_transition__(self, input)
if use_case.is_a?(::Micro::Cases::Flow)
use_case.call!(input: input, result: self)
else
use_case.__call_and_set_transition__(self, input)
end
end
end

Expand All @@ -129,7 +132,7 @@ def transitions

def __set__(is_success, data, type, use_case)
raise Error::InvalidResultType unless type.is_a?(Symbol)
raise Error::InvalidUseCase unless ::Micro.case?(use_case)
raise Error::InvalidUseCase unless use_case.is_a?(::Micro::Case)

@success, @type, @use_case = is_success, type, use_case

Expand Down
42 changes: 18 additions & 24 deletions lib/micro/cases/flow.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,48 +29,42 @@ def initialize(use_cases)
@next_use_cases = use_cases[1..-1]
end

def call(arg = {})
memo = arg.is_a?(Hash) ? arg.dup : {}
def call!(input:, result:)
memo = input.is_a?(Hash) ? input.dup : Kind::Empty::HASH

first_result = __first_use_case_result(arg)
first_result = @first_use_case.__call_and_set_transition__(result, input)

return first_result if @next_use_cases.empty?

__next_use_cases_result(first_result, memo)
end

def call(input = Kind::Empty::HASH)
call!(input: input, result: Case::Result.new)
end

alias __call__ call

def to_proc
Proc.new { |arg| call(arg) }
end

private

def __is_a_result?(arg)
arg.is_a?(Case::Result)
end

def __call_arg(arg)
output = arg.__call__
def then(use_case = nil, &block)
can_yield_self = respond_to?(:yield_self)

__is_a_result?(output) ? output.value : output
end
if block
raise Error::InvalidInvocationOfTheThenMethod if use_case
raise NotImplementedError if !can_yield_self

def __first_use_case_input(arg)
return __call_arg(arg) if ::Micro.case_or_flow?(arg)
return arg.value if __is_a_result?(arg)
yield_self(&block)
else
return yield_self if !use_case && can_yield_self

arg
self.call.then(use_case)
end
end

def __first_use_case_result(arg)
input = __first_use_case_input(arg)

result = Case::Result.new

@first_use_case.__call_and_set_transition__(result, input)
end
private

def __next_use_case_result(use_case, result, input)
use_case.__new__(result, input).__call__
Expand Down
Loading

0 comments on commit 0641af1

Please sign in to comment.