Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename BCDD::Result::Data#name to BCDD::Result::Data#kind #19

Merged
merged 3 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@

- **(BREAKING)** Change the `:continue` addon to halt the step chain on the first `Success()` result. So, if you want to advance to the next step, you must use `Continue(value)` instead of `Success(type, value)`. Otherwise, the step chain will be halted. (Implementation of the following proposal: https://github.com/B-CDD/result/issues/14)

- **(BREAKING)** Rename `BCDD::Result::Data#name` to `BCDD::Result::Data#kind`. The new word is more appropriate as it represents a result's kind (success or failure).

## [0.8.0] - 2023-12-11

### Added
Expand Down
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ divide(100, 0).value_or { 0 } # 0

#### `result.data`

The `BCDD::Result#data` exposes the result attributes (name, type, value) directly and as a hash (`to_h`/`to_hash`) and array (`to_a`/`to_ary`).
The `BCDD::Result#data` exposes the result attributes (kind, type, value) directly and as a hash (`to_h`/`to_hash`) and array (`to_a`/`to_ary`).

This is helpful if you need to access the result attributes generically or want to use Ruby features like splat (`*`) and double splat (`**`) operators.

Expand All @@ -492,25 +492,25 @@ See the examples below to understand how to use it.
```ruby
result = BCDD::Result::Success(:ok, 1)

success_data = result.data # #<BCDD::Result::Data name=:success type=:ok value=1>
success_data = result.data # #<BCDD::Result::Data kind=:success type=:ok value=1>

success_data.name # :success
success_data.kind # :success
success_data.type # :ok
success_data.value # 1

success_data.to_h # {:name=>:success, :type=>:ok, :value=>1}
success_data.to_h # {:kind=>:success, :type=>:ok, :value=>1}
success_data.to_a # [:success, :ok, 1]

name, type, value = success_data
kind, type, value = success_data

[name, type, value] # [:success, :ok, 1]
[kind, type, value] # [:success, :ok, 1]

def print_to_ary(name, type, value)
puts [name, type, value].inspect
def print_to_ary(kind, type, value)
puts [kind, type, value].inspect
end

def print_to_hash(name:, type:, value:)
puts [name, type, value].inspect
def print_to_hash(kind:, type:, value:)
puts [kind, type, value].inspect
end

print_to_ary(*success_data) # [:success, :ok, 1]
Expand Down
8 changes: 4 additions & 4 deletions lib/bcdd/result.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ def self.configuration
end

def initialize(type:, value:, subject: nil, expectations: nil, halted: nil)
data = Data.new(name, type, value)
data = Data.new(kind, type, value)

@type_checker = Contract.evaluate(data, expectations)
@subject = subject
@halted = halted || name == :failure
@halted = halted || kind == :failure
@data = data

self.unknown = true
Expand Down Expand Up @@ -121,15 +121,15 @@ def deconstruct
end

def deconstruct_keys(_keys)
{ name => { type => value } }
{ kind => { type => value } }
end

alias eql? ==
alias on_type on

private

def name
def kind
:unknown
end

Expand Down
2 changes: 1 addition & 1 deletion lib/bcdd/result/contract/evaluator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def type_and_value!(data)
private

def for(data)
case data.name
case data.kind
when :unknown then Contract::Disabled
when :success then success
else failure
Expand Down
14 changes: 7 additions & 7 deletions lib/bcdd/result/data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,26 @@

class BCDD::Result
class Data
attr_reader :name, :type, :value
attr_reader :kind, :type, :value

def initialize(name, type, value)
@name = name
def initialize(kind, type, value)
@kind = kind
@type = type.to_sym
@value = value
end

def to_h
{ name: name, type: type, value: value }
{ kind: kind, type: type, value: value }
end

def to_a
[name, type, value]
[kind, type, value]
end

def inspect
format(
'#<%<class_name>s name=%<name>p type=%<type>p value=%<value>p>',
class_name: self.class.name, name: name, type: type, value: value
'#<%<class_name>s kind=%<kind>p type=%<type>p value=%<value>p>',
class_name: self.class.name, kind: kind, type: type, value: value
)
end

Expand Down
2 changes: 1 addition & 1 deletion lib/bcdd/result/failure/methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def value_or

private

def name
def kind
:failure
end
end
2 changes: 1 addition & 1 deletion lib/bcdd/result/success/methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def value_or

private

def name
def kind
:success
end
end
10 changes: 6 additions & 4 deletions sig/bcdd/result.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class BCDD::Result

private

def name: -> Symbol
def kind: -> Symbol
def known: (Proc) -> untyped
def call_subject_method: (Symbol, untyped) -> BCDD::Result
def ensure_result_object: (untyped, origin: Symbol) -> BCDD::Result
Expand All @@ -68,7 +68,8 @@ class BCDD::Result
def value: -> untyped

private
def name: -> Symbol

def kind: -> Symbol
def type_checker: -> BCDD::Result::Contract::TypeChecker
end

Expand All @@ -88,7 +89,8 @@ class BCDD::Result
def value: -> untyped

private
def name: -> Symbol

def kind: -> Symbol
def type_checker: -> BCDD::Result::Contract::TypeChecker
end

Expand Down Expand Up @@ -138,7 +140,7 @@ end

class BCDD::Result
class Data
attr_reader name: Symbol
attr_reader kind: Symbol
attr_reader type: Symbol
attr_reader value: untyped
attr_reader to_h: Hash[Symbol, untyped]
Expand Down
18 changes: 9 additions & 9 deletions test/bcdd/result/data_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

class BCDD::Result
class DataTest < Minitest::Test
test '#name' do
test '#kind' do
success = Success.new(type: :ok, value: 1)
failure = Failure.new(type: :err, value: 0)

assert_equal :success, success.data.name
assert_equal :failure, failure.data.name
assert_equal :success, success.data.kind
assert_equal :failure, failure.data.kind
end

test '#type' do
Expand Down Expand Up @@ -48,29 +48,29 @@ class DataTest < Minitest::Test
success = Success.new(type: :ok, value: 1)
failure = Failure.new(type: :err, value: 0)

assert_equal({ name: :success, type: :ok, value: 1 }, success.data.to_h)
assert_equal({ name: :failure, type: :err, value: 0 }, failure.data.to_h)
assert_equal({ kind: :success, type: :ok, value: 1 }, success.data.to_h)
assert_equal({ kind: :failure, type: :err, value: 0 }, failure.data.to_h)
end

test '#to_hash' do
success = Success.new(type: :ok, value: 1)
failure = Failure.new(type: :err, value: 0)

assert_equal({ name: :success, type: :ok, value: 1 }, success.data.to_hash)
assert_equal({ name: :failure, type: :err, value: 0 }, failure.data.to_hash)
assert_equal({ kind: :success, type: :ok, value: 1 }, success.data.to_hash)
assert_equal({ kind: :failure, type: :err, value: 0 }, failure.data.to_hash)
end

test '#inspect' do
success = Success.new(type: :ok, value: 1)
failure = Failure.new(type: :err, value: 0)

assert_equal(
'#<BCDD::Result::Data name=:success type=:ok value=1>',
'#<BCDD::Result::Data kind=:success type=:ok value=1>',
success.data.inspect
)

assert_equal(
'#<BCDD::Result::Data name=:failure type=:err value=0>',
'#<BCDD::Result::Data kind=:failure type=:err value=0>',
failure.data.inspect
)
end
Expand Down