Skip to content

Commit

Permalink
[WIP] Move source to result (transition) property
Browse files Browse the repository at this point in the history
  • Loading branch information
serradura committed Jan 25, 2024
1 parent 97b8413 commit 889387e
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 25 deletions.
4 changes: 2 additions & 2 deletions lib/bcdd/result.rb
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,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 +170,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
13 changes: 9 additions & 4 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,17 +118,22 @@ def assert_transitions_metadata(result)
assert_instance_of(Array, metadata[:tree_map])
end

TimeValue = ->(value) { value.is_a?(::Time) && value.utc? }
AndThenValue = ->(value) { value.is_a?(::Hash) && value.empty? }

def assert_transition_record(result, index, options)
transition = result.transitions[:records][index]

root, parent, current, result = options.fetch_values(:root, :parent, :current, :result)
root, parent, current, result_data = options.fetch_values(:root, :parent, :current, :result)

result_data[:source] = result.send(:source) unless result_data.key?(:source)

and_then = options.fetch(:and_then) { ->(value) { value.is_a?(::Hash) && value.empty? } }
and_then = options.fetch(:and_then) { AndThenValue }

time = options.fetch(:time) { ->(value) { value.is_a?(::Time) && value.utc? } }
time = options.fetch(:time) { TimeValue }

assert_hash_schema!(
{ root: root, parent: parent, current: current, result: result, and_then: and_then, time: time },
{ root: root, parent: parent, current: current, result: result_data, and_then: and_then, time: time },
transition
)
end
Expand Down

0 comments on commit 889387e

Please sign in to comment.