errgonomic 0.2.0 → 0.3.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 +4 -4
- data/CHANGELOG.md +4 -0
- data/gemset.nix +1 -1
- data/lib/errgonomic/type.rb +67 -0
- data/lib/errgonomic/version.rb +1 -1
- data/lib/errgonomic.rb +3 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f5b329e5d25e517ff65724ceca5d9ad3e226740e90e57e5ac21917de5f926845
|
4
|
+
data.tar.gz: 2878a2c35bff61a7b57cb5d4a3ec476dd98e42a2ebe21fba3423b6180f8fc68e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4b39914d663d14df9816265d5ce5cef11fec14527cdc2a4400e7c1a9424e4d32e65705e6e79493fb1a8c2102ad02d94bf7b0001b11b4855f6310c6041c40f7ff
|
7
|
+
data.tar.gz: d16813545c25785abfd2e3064a51ec80ed5e07c8e9715c349f11c426223a53e40e8976e53c26fb76be9805d2012359d90a2c9a1db08879ca59a8256eab3b6ca1
|
data/CHANGELOG.md
CHANGED
data/gemset.nix
CHANGED
@@ -0,0 +1,67 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class Object
|
4
|
+
# Returns the receiver if it matches the expected type, otherwise raises a TypeMismatchError.
|
5
|
+
# This is useful for enforcing type expectations in method arguments.
|
6
|
+
#
|
7
|
+
# @param type [Class] The expected type or module the receiver should be.
|
8
|
+
# @param message [String] Optional error message to raise if type doesn't match.
|
9
|
+
# @return [Object] The receiver if it is of the expected type.
|
10
|
+
# @example
|
11
|
+
# 'hello'.type_or_raise!(String) #=> "hello"
|
12
|
+
# 123.type_or_raise!(String, "We need a string!") #=> raise Errgonomic::TypeMismatchError, "We need a string!"
|
13
|
+
# 123.type_or_raise!(String) #=> raise Errgonomic::TypeMismatchError, "Expected String but got Integer"
|
14
|
+
def type_or_raise!(type, message = nil)
|
15
|
+
message ||= "Expected #{type} but got #{self.class}"
|
16
|
+
raise Errgonomic::TypeMismatchError, message unless is_a?(type)
|
17
|
+
|
18
|
+
self
|
19
|
+
end
|
20
|
+
|
21
|
+
alias type_or_raise type_or_raise!
|
22
|
+
|
23
|
+
# Returns the receiver if it matches the expected type, otherwise returns the default value.
|
24
|
+
#
|
25
|
+
# @param type [Class] The expected type or module the receiver should be.
|
26
|
+
# @param default [Object] The value to return if type doesn't match.
|
27
|
+
# @return [Object] The receiver if it is of the expected type, otherwise the default value.
|
28
|
+
# @example
|
29
|
+
# 'hello'.type_or(String, 'default') # => "hello"
|
30
|
+
# 123.type_or(String, 'default') # => "default"
|
31
|
+
def type_or(type, default)
|
32
|
+
return self if is_a?(type)
|
33
|
+
|
34
|
+
default
|
35
|
+
end
|
36
|
+
|
37
|
+
# Returns the receiver if it matches the expected type, otherwise returns the result of the block.
|
38
|
+
# Useful when constructing the default value is expensive.
|
39
|
+
#
|
40
|
+
# @param type [Class] The expected type or module the receiver should be.
|
41
|
+
# @param block [Proc] The block to call if type doesn't match.
|
42
|
+
# @return [Object] The receiver if it is of the expected type, otherwise the block result.
|
43
|
+
# @example
|
44
|
+
# 'hello'.type_or_else(String) { 'default' } # => "hello"
|
45
|
+
# 123.type_or_else(String) { 'default' } # => "default"
|
46
|
+
def type_or_else(type, &block)
|
47
|
+
return self if is_a?(type)
|
48
|
+
|
49
|
+
block.call
|
50
|
+
end
|
51
|
+
|
52
|
+
# Returns the receiver if it does not match the expected type, otherwise raises a TypeMismatchError.
|
53
|
+
#
|
54
|
+
# @param type [Class] The type or module the receiver should not be.
|
55
|
+
# @param message [String] Optional error message to raise if type matches.
|
56
|
+
# @return [Object] The receiver if it is not of the specified type.
|
57
|
+
# @example
|
58
|
+
# 'hello'.not_type_or_raise!(Integer) #=> "hello"
|
59
|
+
# 123.not_type_or_raise!(Integer, "We dont want an integer!") #=> raise Errgonomic::TypeMismatchError, "We dont want an integer!"
|
60
|
+
# 123.not_type_or_raise!(Integer) #=> raise Errgonomic::TypeMismatchError, "Expected anything but Integer but got Integer"
|
61
|
+
def not_type_or_raise!(type, message = nil)
|
62
|
+
message ||= "Expected anything but #{type} but got #{self.class}"
|
63
|
+
raise Errgonomic::TypeMismatchError, message if is_a?(type)
|
64
|
+
|
65
|
+
self
|
66
|
+
end
|
67
|
+
end
|
data/lib/errgonomic/version.rb
CHANGED
data/lib/errgonomic.rb
CHANGED
@@ -5,6 +5,9 @@ require_relative 'errgonomic/version' unless defined?(Errgonomic::VERSION)
|
|
5
5
|
# A more opinionated blend with Rails presence.
|
6
6
|
require_relative 'errgonomic/presence'
|
7
7
|
|
8
|
+
# Bring in a subtle and manual type checker.
|
9
|
+
require_relative 'errgonomic/type'
|
10
|
+
|
8
11
|
# Bring in our Option and Result.
|
9
12
|
require_relative 'errgonomic/option'
|
10
13
|
require_relative 'errgonomic/result'
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: errgonomic
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nick Zadrozny
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 1980-01-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: concurrent-ruby
|
@@ -79,6 +79,7 @@ files:
|
|
79
79
|
- lib/errgonomic/option.rb
|
80
80
|
- lib/errgonomic/presence.rb
|
81
81
|
- lib/errgonomic/result.rb
|
82
|
+
- lib/errgonomic/type.rb
|
82
83
|
- lib/errgonomic/version.rb
|
83
84
|
- sig/errgonomic.rbs
|
84
85
|
homepage: https://omc.io/
|