dry-types-fear 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 7944142467a930e085d2cd0c17c6ac64074d1eae541fcc22df99d0378b112147
4
+ data.tar.gz: bc81b914b6676746a6710a1f0c2a1d4210b581ebceb8e58bac7222ee418ce974
5
+ SHA512:
6
+ metadata.gz: b64be83062f40faf38229aa0bb3f358ce00f1c488ec525747eff16eab570bec03c57fb7a43297f2e643d262199be32bff92423a4dca41a45dc696ee3f1ee44b5
7
+ data.tar.gz: 899253ccf38c0f1aca5de14251a37e9e3b34abc3b30f69afbc42d233a02e0ec4e2142de51682e65b62ecd211704d66c72688bb417078a11f79dfcdd6319f16d9
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.standard.yml ADDED
@@ -0,0 +1,3 @@
1
+ # For available configuration options, see:
2
+ # https://github.com/standardrb/standard
3
+ ruby_version: 3.0
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015-2024 Tema Bolshakov
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,98 @@
1
+ # Dry::Types::Fear
2
+
3
+ `Dry::Types` integration with [fear] allows using `Fear::Option` as optional type for `Dry::Types`
4
+
5
+ ## Installation
6
+
7
+ Install the gem and add to the application's Gemfile by executing:
8
+
9
+ $ bundle add dry-types-fear
10
+
11
+ If bundler is not being used to manage dependencies, install the gem by executing:
12
+
13
+ $ gem install dry-types-fear
14
+
15
+ ## Usage
16
+
17
+ Load the `:fear_option` extension in your application.
18
+
19
+ ```ruby
20
+ require 'dry-types'
21
+ require 'dry/types/fear'
22
+
23
+ Dry::Types.load_extensions(:fear_option)
24
+
25
+ module Types
26
+ include Dry.Types()
27
+ end
28
+ ```
29
+
30
+ Append `.option` to a Type to return a `Fear::Option` object:
31
+
32
+ ```ruby
33
+ Types::Option::Strict::Integer[nil]
34
+ #=> Fear.none
35
+ Types::Option::Coercible::String[nil]
36
+ #=> Fear.none
37
+ Types::Option::Strict::Integer[123]
38
+ #=> Fear.some(123)
39
+ Types::Option::Strict::String[123]
40
+ #=> Fear.some(123)
41
+ Types::Option::Coercible::Float['12.3']
42
+ #=> Fear.some(12.3)
43
+ ```
44
+
45
+ 'Option' types can also accessed by calling `.option` on a regular type:
46
+
47
+ ```ruby
48
+ Types::Strict::Integer.option # equivalent to Types::Option::Strict::Integer
49
+ ```
50
+
51
+
52
+ You can define your own optional types:
53
+
54
+ ```ruby
55
+ option_string = Types::Strict::String.option
56
+ option_string[nil]
57
+ # => Fear.none
58
+ option_string[nil].map(&:upcase)
59
+ # => Fear.none
60
+ option_string['something']
61
+ # => Fear.some('something')
62
+ option_string['something'].map(&:upcase)
63
+ # => Fear.some('SOMETHING')
64
+ option_string['something'].map(&:upcase).get_or_else { 'NOTHING' }
65
+ # => "SOMETHING"
66
+ ```
67
+
68
+ You can use it with dry-struct as well:
69
+
70
+ ```ruby
71
+ class User < Dry::Struct
72
+ attribute :name, Types::Coercible::String
73
+ attribute :age, Types::Coercible::Integer.option
74
+ end
75
+
76
+ user = User.new(name: 'Bob', age: nil)
77
+ user.name #=> "Bob"
78
+ user.age #=> Fear.none
79
+
80
+ user = User.new(name: 'Bob', age: 42)
81
+ user.age #=> Fear.some(42)
82
+ ```
83
+
84
+ ## Development
85
+
86
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run
87
+ `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will
88
+ allow you to experiment.
89
+
90
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version,
91
+ update the version number in the gemspec, and then run `bundle exec rake release`, which will create a git
92
+ tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
93
+
94
+ ## Contributing
95
+
96
+ Bug reports and pull requests are welcome on GitHub at https://github.com/bolshakov/dry-types-fear.
97
+
98
+ [fear]: https://github.com/bolshakov/fear
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "standard/rake"
9
+
10
+ task default: %i[spec standard]
@@ -0,0 +1,123 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Dry
4
+ module Types
5
+ class Option
6
+ include Type
7
+ include ::Dry::Equalizer(:type, :options, inspect: false, immutable: true)
8
+ include Decorator
9
+ include Builder
10
+ include Printable
11
+
12
+ # @param [Fear::Option, Object] input
13
+ #
14
+ # @return [Fear::Option]
15
+ #
16
+ # @api private
17
+ def call_unsafe(input = Undefined)
18
+ case input
19
+ when ::Fear::Option
20
+ input
21
+ when Undefined
22
+ Fear.none
23
+ else
24
+ Fear.option(type.call_unsafe(input))
25
+ end
26
+ end
27
+
28
+ # @param [Fear::Option, Object] input
29
+ #
30
+ # @return [Fear::Option]
31
+ #
32
+ # @api private
33
+ def call_safe(input = Undefined)
34
+ case input
35
+ when ::Fear::Option
36
+ input
37
+ when Undefined
38
+ Fear.none
39
+ else
40
+ Fear.option(type.call_safe(input) { |output = input| return yield(output) })
41
+ end
42
+ end
43
+
44
+ # @param [Object] input
45
+ #
46
+ # @return [Result::Success]
47
+ #
48
+ # @api public
49
+ def try(input = Undefined)
50
+ result = type.try(input)
51
+
52
+ if result.success?
53
+ Result::Success.new(Fear.option(result.input))
54
+ else
55
+ result
56
+ end
57
+ end
58
+
59
+ # @return [true]
60
+ #
61
+ # @api public
62
+ def default?
63
+ true
64
+ end
65
+
66
+ # @param [Object] value
67
+ #
68
+ # @see Dry::Types::Builder#default
69
+ #
70
+ # @raise [ArgumentError] if nil provided as default value
71
+ #
72
+ # @api public
73
+ def default(value)
74
+ raise ArgumentError, "nil cannot be used as a default of a maybe type" if value.nil?
75
+
76
+ super
77
+ end
78
+ end
79
+
80
+ module Builder
81
+ # Turn a type into a maybe type
82
+ #
83
+ # @return [Option]
84
+ #
85
+ # @api public
86
+ def option
87
+ Option.new(Types["nil"] | self)
88
+ end
89
+ end
90
+
91
+ # @api private
92
+ class Schema
93
+ class Key
94
+ # @api private
95
+ def option
96
+ __new__(type.option)
97
+ end
98
+ end
99
+ end
100
+
101
+ # @api private
102
+ class Printer
103
+ MAPPING[Option] = :visit_option
104
+
105
+ # @api private
106
+ def visit_option(maybe)
107
+ visit(maybe.type) do |type|
108
+ yield "Fear::Option<#{type}>"
109
+ end
110
+ end
111
+ end
112
+
113
+ # Register non-coercible maybe types
114
+ NON_NIL.each_key do |name|
115
+ register("option.strict.#{name}", self[name.to_s].option)
116
+ end
117
+
118
+ # Register coercible maybe types
119
+ COERCIBLE.each_key do |name|
120
+ register("option.coercible.#{name}", self["coercible.#{name}"].option)
121
+ end
122
+ end
123
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "dry/types"
4
+ require "fear"
5
+
6
+ Dry::Types.register_extension(:fear_option) do
7
+ require "dry/types/fear/option"
8
+ end
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dry-types-fear
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Tëma Bolshakov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-04-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: dry-types
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: fear
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: The gems enables you to use Fear::Option as optional type for Dry::Types
42
+ email:
43
+ - either.free@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".rspec"
49
+ - ".standard.yml"
50
+ - LICENSE.txt
51
+ - README.md
52
+ - Rakefile
53
+ - lib/dry/types/fear.rb
54
+ - lib/dry/types/fear/option.rb
55
+ homepage: https://github.com/bolshakov/dry-types-fear
56
+ licenses:
57
+ - MIT
58
+ metadata:
59
+ homepage_uri: https://github.com/bolshakov/dry-types-fear
60
+ source_code_uri: https://github.com/bolshakov/dry-types-fear
61
+ post_install_message:
62
+ rdoc_options: []
63
+ require_paths:
64
+ - lib
65
+ required_ruby_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: 3.0.0
70
+ required_rubygems_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ requirements: []
76
+ rubygems_version: 3.5.7
77
+ signing_key:
78
+ specification_version: 4
79
+ summary: Dry::Types integration with Fear.
80
+ test_files: []