Skip to content

Commit

Permalink
Move source to result (transition) property
Browse files Browse the repository at this point in the history
  • Loading branch information
serradura committed Jan 26, 2024
1 parent 97b8413 commit 5067e59
Show file tree
Hide file tree
Showing 12 changed files with 106 additions and 82 deletions.
5 changes: 3 additions & 2 deletions lib/bcdd/result.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# frozen_string_literal: true

require 'set'
require 'singleton'

require_relative 'result/version'
Expand Down Expand Up @@ -153,7 +154,7 @@ def known(block)
def call_and_then_source_method(method_name, injected_value)
method = source.method(method_name)

Transitions.tracking.record_and_then(method, injected_value, source) do
Transitions.tracking.record_and_then(method, injected_value) do
result = call_and_then_source_method!(method, injected_value)

ensure_result_object(result, origin: :method)
Expand All @@ -170,7 +171,7 @@ def call_and_then_source_method!(method, injected_value)
end

def call_and_then_block(block)
Transitions.tracking.record_and_then(:block, nil, source) do
Transitions.tracking.record_and_then(:block, nil) do
result = call_and_then_block!(block)

ensure_result_object(result, origin: :block)
Expand Down
2 changes: 1 addition & 1 deletion lib/bcdd/result/callable_and_then/caller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class CallableAndThen::Caller
def self.call(source, value:, injected_value:, method_name:)
method = callable_method(source, method_name)

Transitions.tracking.record_and_then(method, injected_value, source) do
Transitions.tracking.record_and_then(method, injected_value) do
result =
if source.is_a?(::Proc)
call_proc!(source, value, injected_value)
Expand Down
2 changes: 1 addition & 1 deletion lib/bcdd/result/transitions/tracking/disabled.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def self.reset!; end

def self.record(result); end

def self.record_and_then(_type, _data, _source)
def self.record_and_then(_type, _data)
yield
end

Expand Down
9 changes: 5 additions & 4 deletions lib/bcdd/result/transitions/tracking/enabled.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ def record(result)
track(result, time: ::Time.now.getutc)
end

def record_and_then(type_arg, arg, source)
def record_and_then(type_arg, arg)
type = type_arg.instance_of?(::Method) ? :method : type_arg

unless tree.frozen?
current_and_then = { type: type, arg: arg, source: source }
current_and_then = { type: type, arg: arg }
current_and_then[:method_name] = type_arg.name if type == :method

tree.current.value[1] = current_and_then
Expand Down Expand Up @@ -84,13 +84,14 @@ def root_start(name_and_desc)
end

def track(result, time:)
result = result.data.to_h
result_data = result.data.to_h
result_data[:source] = result.send(:source)

root, = tree.root_value
parent, = tree.parent_value
current, and_then = tree.current_value

records << { root: root, parent: parent, current: current, result: result, and_then: and_then, time: time }
records << { root: root, parent: parent, current: current, result: result_data, and_then: and_then, time: time }
end

def now_in_milliseconds
Expand Down
4 changes: 2 additions & 2 deletions sig/bcdd/result/transitions.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class BCDD::Result
def exec: (String, String) { () -> untyped } -> BCDD::Result
def reset!: () -> void
def record: (BCDD::Result) -> void
def record_and_then: ((untyped), untyped, untyped) { () -> BCDD::Result } -> BCDD::Result
def record_and_then: ((untyped), untyped) { () -> BCDD::Result } -> BCDD::Result
def reset_and_then!: () -> void

private
Expand All @@ -79,7 +79,7 @@ class BCDD::Result
def self.exec: (String, String) { () -> untyped } -> BCDD::Result
def self.reset!: () -> void
def self.record: (BCDD::Result) -> void
def self.record_and_then: ((untyped), untyped, untyped) { () -> BCDD::Result } -> BCDD::Result
def self.record_and_then: ((untyped), untyped) { () -> BCDD::Result } -> BCDD::Result
def self.reset_and_then!: () -> void

private
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,26 +84,30 @@ def teardown

root = { id: 0, name: 'NormalizeAndValidateEmail', desc: nil }

source = -> { _1 == NormalizeAndValidateEmail }

{
root: root,
parent: root,
current: root,
result: { kind: :success, type: :given, value: 1 }
result: { kind: :success, type: :given, value: 1, source: source }
}.then { assert_transition_record(result1, 0, _1) }

source = -> { _1 == NormalizeEmail }

{
root: root,
parent: root,
current: { id: 1, name: 'NormalizeEmail', desc: nil },
result: { kind: :success, type: :given, value: 1 }
result: { kind: :success, type: :given, value: 1, source: source }
}.then { assert_transition_record(result1, 1, _1) }

{
root: root,
parent: root,
current: { id: 1, name: 'NormalizeEmail', desc: nil },
result: { kind: :failure, type: :invalid_input, value: 'input must be a String' },
and_then: { type: :method, arg: nil, source: -> { _1 == NormalizeEmail }, method_name: :normalize }
result: { kind: :failure, type: :invalid_input, value: 'input must be a String', source: source },
and_then: { type: :method, arg: nil, method_name: :normalize }
}.then { assert_transition_record(result1, 2, _1) }

# ---
Expand All @@ -116,41 +120,45 @@ def teardown

root = { id: 0, name: 'NormalizeAndValidateEmail', desc: nil }

source = -> { _1 == NormalizeAndValidateEmail }

{
root: root,
parent: root,
current: root,
result: { kind: :success, type: :given, value: " [email protected] \n" }
result: { kind: :success, type: :given, value: " [email protected] \n", source: source }
}.then { assert_transition_record(result2, 0, _1) }

source = -> { _1 == NormalizeEmail }

{
root: root,
parent: root,
current: { id: 1, name: 'NormalizeEmail', desc: nil },
result: { kind: :success, type: :given, value: " [email protected] \n" }
result: { kind: :success, type: :given, value: " [email protected] \n", source: source }
}.then { assert_transition_record(result2, 1, _1) }

{
root: root,
parent: root,
current: { id: 1, name: 'NormalizeEmail', desc: nil },
result: { kind: :success, type: :normalized_input, value: '[email protected]' },
and_then: { type: :method, arg: nil, source: -> { _1 == NormalizeEmail }, method_name: :normalize }
result: { kind: :success, type: :normalized_input, value: '[email protected]', source: source },
and_then: { type: :method, arg: nil, method_name: :normalize }
}.then { assert_transition_record(result2, 2, _1) }

{
root: root,
parent: root,
current: { id: 2, name: 'EmailValidation', desc: nil },
result: { kind: :success, type: :given, value: '[email protected]' }
result: { kind: :success, type: :given, value: '[email protected]', source: EmailValidation }
}.then { assert_transition_record(result2, 3, _1) }

{
root: root,
parent: root,
current: { id: 2, name: 'EmailValidation', desc: nil },
result: { kind: :success, type: :valid_email, value: '[email protected]' },
and_then: { type: :method, arg: nil, source: EmailValidation, method_name: :validate }
result: { kind: :success, type: :valid_email, value: '[email protected]', source: EmailValidation },
and_then: { type: :method, arg: nil, method_name: :validate }
}.then { assert_transition_record(result2, 4, _1) }
end
end
Expand Down
18 changes: 9 additions & 9 deletions test/bcdd/result/context/callable_and_then/accumulation_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,45 +68,45 @@ def call_g(f:, **)
root: root,
parent: root,
current: root,
result: { kind: :success, type: :given, value: { a: 1 } }
result: { kind: :success, type: :given, value: { a: 1 }, source: root_process }
}.then { assert_transition_record(result, 0, _1) }

{
root: root,
parent: root,
current: root,
result: { kind: :success, type: :b, value: { b: 2 } },
and_then: { type: :method, arg: { b: 2 }, source: root_process, method_name: :call_b }
result: { kind: :success, type: :b, value: { b: 2 }, source: root_process },
and_then: { type: :method, arg: { b: 2 }, method_name: :call_b }
}.then { assert_transition_record(result, 1, _1) }

{
root: root,
parent: root,
current: { id: 1, name: 'CallC', desc: nil },
result: { kind: :success, type: :c, value: { c: 3 } }
result: { kind: :success, type: :c, value: { c: 3 }, source: nil }
}.then { assert_transition_record(result, 2, _1) }

{
root: root,
parent: root,
current: root,
result: { kind: :success, type: :d, value: { d: 4 } },
and_then: { type: :method, arg: -> { _1.is_a?(Hash) && _1.empty? }, source: root_process, method_name: :call_d }
result: { kind: :success, type: :d, value: { d: 4 }, source: root_process },
and_then: { type: :method, arg: -> { _1.is_a?(Hash) && _1.empty? }, method_name: :call_d }
}.then { assert_transition_record(result, 3, _1) }

{
root: root,
parent: root,
current: { id: 2, name: 'CallE', desc: nil },
result: { kind: :success, type: :e, value: { e: 5 } }
result: { kind: :success, type: :e, value: { e: 5 }, source: nil }
}.then { assert_transition_record(result, 4, _1) }

{
root: root,
parent: root,
current: root,
result: { kind: :success, type: :g, value: { g: 6 } },
and_then: { type: :method, arg: { h: 7 }, source: root_process, method_name: :call_g }
result: { kind: :success, type: :g, value: { g: 6 }, source: root_process },
and_then: { type: :method, arg: { h: 7 }, method_name: :call_g }
}.then { assert_transition_record(result, 5, _1) }

{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,26 +84,30 @@ def teardown

root = { id: 0, name: 'NormalizeAndValidateEmail', desc: nil }

source = -> { _1 == NormalizeAndValidateEmail }

{
root: root,
parent: root,
current: root,
result: { kind: :success, type: :given, value: { input: 1 } }
result: { kind: :success, type: :given, value: { input: 1 }, source: source }
}.then { assert_transition_record(result1, 0, _1) }

source = -> { _1 == NormalizeEmail }

{
root: root,
parent: root,
current: { id: 1, name: 'NormalizeEmail', desc: nil },
result: { kind: :success, type: :given, value: { input: 1 } }
result: { kind: :success, type: :given, value: { input: 1 }, source: source }
}.then { assert_transition_record(result1, 1, _1) }

{
root: root,
parent: root,
current: { id: 1, name: 'NormalizeEmail', desc: nil },
result: { kind: :failure, type: :invalid_input, value: { message: 'input must be a String' } },
and_then: { type: :method, arg: {}, source: -> { _1 == NormalizeEmail }, method_name: :normalize }
result: { kind: :failure, type: :invalid_input, value: { message: 'input must be a String' }, source: source },
and_then: { type: :method, arg: {}, method_name: :normalize }
}.then { assert_transition_record(result1, 2, _1) }

# ---
Expand All @@ -116,41 +120,45 @@ def teardown

root = { id: 0, name: 'NormalizeAndValidateEmail', desc: nil }

source = -> { _1 == NormalizeAndValidateEmail }

{
root: root,
parent: root,
current: root,
result: { kind: :success, type: :given, value: { input: " [email protected] \n" } }
result: { kind: :success, type: :given, value: { input: " [email protected] \n" }, source: source }
}.then { assert_transition_record(result2, 0, _1) }

source = -> { _1 == NormalizeEmail }

{
root: root,
parent: root,
current: { id: 1, name: 'NormalizeEmail', desc: nil },
result: { kind: :success, type: :given, value: { input: " [email protected] \n" } }
result: { kind: :success, type: :given, value: { input: " [email protected] \n" }, source: source }
}.then { assert_transition_record(result2, 1, _1) }

{
root: root,
parent: root,
current: { id: 1, name: 'NormalizeEmail', desc: nil },
result: { kind: :success, type: :normalized_input, value: { input: '[email protected]' } },
and_then: { type: :method, arg: {}, source: -> { _1 == NormalizeEmail }, method_name: :normalize }
result: { kind: :success, type: :normalized_input, value: { input: '[email protected]' }, source: source },
and_then: { type: :method, arg: {}, method_name: :normalize }
}.then { assert_transition_record(result2, 2, _1) }

{
root: root,
parent: root,
current: { id: 2, name: 'EmailValidation', desc: nil },
result: { kind: :success, type: :given, value: { input: '[email protected]' } }
result: { kind: :success, type: :given, value: { input: '[email protected]' }, source: EmailValidation }
}.then { assert_transition_record(result2, 3, _1) }

{
root: root,
parent: root,
current: { id: 2, name: 'EmailValidation', desc: nil },
result: { kind: :success, type: :valid_email, value: { email: '[email protected]' } },
and_then: { type: :method, arg: {}, source: EmailValidation, method_name: :validate }
result: { kind: :success, type: :valid_email, value: { email: '[email protected]' }, source: EmailValidation },
and_then: { type: :method, arg: {}, method_name: :validate }
}.then { assert_transition_record(result2, 4, _1) }
end
end
Expand Down
Loading

0 comments on commit 5067e59

Please sign in to comment.