Skip to content

Commit

Permalink
Extract Dry::Types integration into a separate gem (#173)
Browse files Browse the repository at this point in the history
  • Loading branch information
bolshakov committed Apr 2, 2024
1 parent 7428c38 commit f8f718e
Show file tree
Hide file tree
Showing 11 changed files with 4 additions and 485 deletions.
74 changes: 4 additions & 70 deletions README.md
Expand Up @@ -1193,76 +1193,7 @@ end

### Dry-Types integration

#### Option

NOTE: Requires the dry-tyes gem to be loaded.

Load the `:fear_option` extension in your application.

```ruby
require 'dry-types'
require 'dry/types/fear'

Dry::Types.load_extensions(:fear_option)

module Types
include Dry.Types()
end
```

Append .option to a Type to return a `Fear::Option` object:

```ruby
Types::Option::Strict::Integer[nil]
#=> Fear.none
Types::Option::Coercible::String[nil]
#=> Fear.none
Types::Option::Strict::Integer[123]
#=> Fear.some(123)
Types::Option::Strict::String[123]
#=> Fear.some(123)
Types::Option::Coercible::Float['12.3']
#=> Fear.some(12.3)
```

'Option' types can also accessed by calling '.option' on a regular type:

```ruby
Types::Strict::Integer.option # equivalent to Types::Option::Strict::Integer
```


You can define your own optional types:

```ruby
option_string = Types::Strict::String.option
option_string[nil]
# => Fear.none
option_string[nil].map(&:upcase)
# => Fear.none
option_string['something']
# => Fear.some('something')
option_string['something'].map(&:upcase)
# => Fear.some('SOMETHING')
option_string['something'].map(&:upcase).get_or_else { 'NOTHING' }
# => "SOMETHING"
```

You can use it with dry-struct as well:

```ruby
class User < Dry::Struct
attribute :name, Types::Coercible::String
attribute :age, Types::Coercible::Integer.option
end

user = User.new(name: 'Bob', age: nil)
user.name #=> "Bob"
user.age #=> Fear.none

user = User.new(name: 'Bob', age: 42)
user.age #=> Fear.some(42)
```
To use `Fear::Option` as optional type for `Dry::Types` use the [dry-types-fear] gem.

## Testing

Expand All @@ -1286,3 +1217,6 @@ provides a bunch of rspec matchers.
* [maybe](https://github.com/bhb/maybe)
* [ruby-possibly](https://github.com/rap1ds/ruby-possibly)
* [rumonade](https://github.com/ms-ati/rumonade)


[dry-types-fear]: https://github.com/bolshakov/dry-types-fear
8 changes: 0 additions & 8 deletions lib/dry/types/fear.rb

This file was deleted.

125 changes: 0 additions & 125 deletions lib/dry/types/fear/option.rb

This file was deleted.

1 change: 0 additions & 1 deletion lib/fear.rb
Expand Up @@ -2,7 +2,6 @@

require "zeitwerk"
loader = Zeitwerk::Loader.for_gem
loader.ignore("#{__dir__}/dry")
loader.setup

module Fear
Expand Down
22 changes: 0 additions & 22 deletions spec/dry/types/fear/option/constrained_spec.rb

This file was deleted.

77 changes: 0 additions & 77 deletions spec/dry/types/fear/option/core_spec.rb

This file was deleted.

21 changes: 0 additions & 21 deletions spec/dry/types/fear/option/default_spec.rb

This file was deleted.

0 comments on commit f8f718e

Please sign in to comment.