Skip to content

Commit

Permalink
Change addons definition (Hash instead Array)
Browse files Browse the repository at this point in the history
  • Loading branch information
serradura committed Nov 8, 2023
1 parent 8689802 commit 745d42f
Show file tree
Hide file tree
Showing 14 changed files with 35 additions and 37 deletions.
4 changes: 1 addition & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,7 @@
```
- **To**
```ruby
BCDD::Result.mixin(with: :continue)
BCDD::Result.mixin(with: [:continue])
BCDD::Result.mixin(with: { continue: true })
BCDD::Result.mixin(config: { addon: { continue: true } })
```
- These examples are valid to all kinds of mixins (`BCDD::Result.mixin`, `BCDD::Result::Context.mixin`, `BCDD::Result::Expectations.mixin`, `BCDD::Result::Context::Expectations.mixin`)

Expand Down
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -746,10 +746,10 @@ class Divide
private

def validate_numbers
arg1.is_a?(::Numeric) or return BCDD::Result::Failure(:invalid_arg, 'arg1 must be numeric') # This will raise an error
arg1.is_a?(::Numeric) or return Failure(:invalid_arg, 'arg1 must be numeric') # This will raise an error
arg2.is_a?(::Numeric) or return Failure(:invalid_arg, 'arg2 must be numeric')

BCDD::Result::Success(:ok, [arg1, arg2]) # This will raise an error
Success(:ok, [arg1, arg2]) # This will raise an error
end

def validate_non_zero(numbers)
Expand Down Expand Up @@ -838,7 +838,7 @@ This addon will create the `Continue(value)` method, which will know how to prod

```ruby
module Divide
extend self, BCDD::Result.mixin(config: :continue)
extend self, BCDD::Result.mixin(config: { addon: { continue: true } })

def call(arg1, arg2)
validate_numbers(arg1, arg2)
Expand Down Expand Up @@ -947,10 +947,10 @@ class Divide
end
```

This mode also defines an `Expected` constant to be used inside and outside the module.
This mode also defines an `Result` constant to be used inside and outside the module.

> **PROTIP:**
> You can use the `Expected` constant to mock the result's type and value in your tests. As they will have the exact expectations, your tests will check if the result clients are handling the result correctly.
> You can use the `Result` constant to mock the result's type and value in your tests. As they will have the exact expectations, your tests will check if the result clients are handling the result correctly.
Now that you know the two modes, let's understand how expectations can be beneficial and powerful for defining contracts.

Expand Down Expand Up @@ -1274,12 +1274,12 @@ The `BCDD::Result::Expectations.mixin` also accepts the `with:` argument. It is

**Continue**

It is similar to `BCDD::Result.mixin(config: :continue)`, the key difference is that the `Continue(value)` will be ignored by the expectations. This is extremely useful when you want to use `Continue(value)` to chain operations, but you don't want to declare N success types in the expectations.
It is similar to `BCDD::Result.mixin(config: { addon: { continue: true } })`, the key difference is that the `Continue(value)` will be ignored by the expectations. This is extremely useful when you want to use `Continue(value)` to chain operations, but you don't want to declare N success types in the expectations.

```ruby
class Divide
include BCDD::Result::Expectations.mixin(
config: :continue,
config: { addon: { continue: true } },
success: :division_completed,
failure: %i[invalid_arg division_by_zero]
)
Expand Down Expand Up @@ -1581,7 +1581,7 @@ The `BCDD::Result::Context.mixin` and `BCDD::Result::Context::Expectations.mixin

**Continue**

The `BCDD::Result::Context.mixin(config: :continue)` or `BCDD::Result::Context::Expectations.mixin(config: :continue)` adds a `Continue(**input)` that will be ignored by the expectations. This is extremely useful when you want to use `Continue()` to chain operations, but you don't want to declare N success types in the expectations.
The `BCDD::Result::Context.mixin(config: { addon: { continue: true } })` or `BCDD::Result::Context::Expectations.mixin(config: { addon: { continue: true } })` adds a `Continue(**input)` that will be ignored by the expectations. This is extremely useful when you want to use `Continue()` to chain operations, but you don't want to declare N success types in the expectations.

Let's use a mix of `BCDD::Result::Context` features to see in action with this add-on:

Expand All @@ -1596,7 +1596,7 @@ module Divide
require 'logger'

extend self, BCDD::Result::Context::Expectations.mixin(
config: :continue,
config: { addon: { continue: true } },
success: {
division_completed: ->(value) { value => { number: Numeric } }
},
Expand Down
4 changes: 2 additions & 2 deletions lib/bcdd/result/context/expectations/mixin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ module Continuable

OPTIONS = { continue: Continuable }.freeze

def self.options(names)
::BCDD::Result::Config::Options.filter(names, OPTIONS)
def self.options(config_flags)
::BCDD::Result::Config::Options.unwrap(options: { addon: OPTIONS }, flags: config_flags)
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/bcdd/result/context/mixin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ module Continuable

OPTIONS = { continue: Continuable }.freeze

def self.options(names)
::BCDD::Result::Config::Options.filter(names, OPTIONS)
def self.options(config_flags)
::BCDD::Result::Config::Options.unwrap(options: { addon: OPTIONS }, flags: config_flags)
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/bcdd/result/expectations/mixin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ module Continuable

OPTIONS = { continue: Continuable }.freeze

def self.options(names)
Config::Options.filter(names, OPTIONS)
def self.options(config_flags)
Config::Options.unwrap(options: { addon: OPTIONS }, flags: config_flags)
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/bcdd/result/mixin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ module Continuable

OPTIONS = { continue: Continuable }.freeze

def self.options(names)
Config::Options.filter(names, OPTIONS)
def self.options(config_flags)
Config::Options.unwrap(options: { addon: OPTIONS }, flags: config_flags)
end
end
end
Expand Down
12 changes: 6 additions & 6 deletions sig/bcdd/result.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,11 @@ class BCDD::Result

OPTIONS: Hash[Symbol, Module]

def self.options: (Array[Symbol]) -> Array[Module]
def self.options: (Hash[Symbol, Hash[Symbol, bool]]) -> Array[Module]
end
end

def self.mixin: (?config: Array[Symbol]) -> Module
def self.mixin: (?config: Hash[Symbol, Hash[Symbol, bool]]) -> Module

def self.mixin_module: -> singleton(BCDD::Result::Mixin)
end
Expand Down Expand Up @@ -342,7 +342,7 @@ end

class BCDD::Result::Expectations
def self.mixin: (
?config: Symbol,
?config: Hash[Symbol, Hash[Symbol, bool]],
?success: Hash[Symbol, untyped] | Array[Symbol],
?failure: Hash[Symbol, untyped] | Array[Symbol]
) -> Module
Expand Down Expand Up @@ -381,7 +381,7 @@ module BCDD::Result::Expectations::Mixin

OPTIONS: Hash[Symbol, Module]

def self.options: (Symbol) -> Array[Module]
def self.options: (Hash[Symbol, Hash[Symbol, bool]]) -> Array[Module]
end
end

Expand Down Expand Up @@ -450,7 +450,7 @@ class BCDD::Result::Context

OPTIONS: Hash[Symbol, Module]

def self.options: (Array[Symbol]) -> Array[Module]
def self.options: (Hash[Symbol, Hash[Symbol, bool]]) -> Array[Module]
end
end

Expand All @@ -475,7 +475,7 @@ module BCDD::Result::Context::Expectations::Mixin

OPTIONS: Hash[Symbol, Module]

def self.options: (Symbol) -> Array[Module]
def self.options: (Hash[Symbol, Hash[Symbol, bool]]) -> Array[Module]
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

class BCDD::Result::AndThenWithSubjectContinueInstanceTest < Minitest::Test
class Divide
include BCDD::Result.mixin(config: :continue)
include BCDD::Result.mixin(config: { addon: { continue: true } })

def call(arg1, arg2)
validate_numbers(arg1, arg2)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

class BCDD::Result::AndThenWithSubjectContinueSingletonTest < Minitest::Test
module Divide
extend self, BCDD::Result.mixin(config: :continue)
extend self, BCDD::Result.mixin(config: { addon: { continue: true } })

def call(arg1, arg2)
validate_numbers(arg1, arg2)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

class BCDD::Result::Context::AndThenWithSubjectContinueInstanceTest < Minitest::Test
class Divide
include BCDD::Result::Context.mixin(config: :continue)
include BCDD::Result::Context.mixin(config: { addon: { continue: true } })

def call(arg1, arg2)
validate_numbers(arg1, arg2)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

class BCDD::Result::Context::AndThenWithSubjectContinueSingletonTest < Minitest::Test
module Divide
extend self, BCDD::Result::Context.mixin(config: :continue)
extend self, BCDD::Result::Context.mixin(config: { addon: { continue: true } })

def call(arg1, arg2)
validate_numbers(arg1, arg2)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
class BCDD::Result::Context::ExpectationsWithSubjectSuccessTypeTest < Minitest::Test
class DivideType
include BCDD::Result::Context::Expectations.mixin(
config: :continue,
config: { addon: { continue: true } },
success: :ok,
failure: :err
)
Expand Down Expand Up @@ -36,7 +36,7 @@ def divide(number1:, number2:)

class DivideTypes
include BCDD::Result::Context::Expectations.mixin(
config: :continue,
config: { addon: { continue: true } },
success: :division_completed,
failure: %i[invalid_arg division_by_zero]
)
Expand Down Expand Up @@ -67,7 +67,7 @@ def divide(number1:, number2:)

module DivideTypeAndValue
extend self, BCDD::Result::Context::Expectations.mixin(
config: :continue,
config: { addon: { continue: true } },
success: {
division_completed: ->(value) {
case value
Expand Down
6 changes: 3 additions & 3 deletions test/bcdd/result/expectations/with_subject/continue_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
class BCDD::Result::ExpectationsWithSubjectSuccessTypeTest < Minitest::Test
class DivideType
include BCDD::Result::Expectations.mixin(
config: :continue,
config: { addon: { continue: true } },
success: :ok,
failure: :err
)
Expand Down Expand Up @@ -38,7 +38,7 @@ def divide((number1, number2))

class DivideTypes
include BCDD::Result::Expectations.mixin(
config: :continue,
config: { addon: { continue: true } },
success: :division_completed,
failure: %i[invalid_arg division_by_zero]
)
Expand Down Expand Up @@ -71,7 +71,7 @@ def divide((number1, number2))

module DivideTypeAndValue
extend self, BCDD::Result::Expectations.mixin(
config: :continue,
config: { addon: { continue: true } },
success: { division_completed: Numeric },
failure: { invalid_arg: String, division_by_zero: String }
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

class BCDD::RailwayOrientedProgrammingResultMixinSingletonTest < Minitest::Test
module Divide
extend self, BCDD::Result.mixin(config: :continue)
extend self, BCDD::Result.mixin(config: { addon: { continue: true } })

def call(arg1, arg2)
validate_numbers(arg1, arg2)
Expand Down

0 comments on commit 745d42f

Please sign in to comment.