kind 2.3.0 → 3.0.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.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +41 -14
  3. data/lib/kind/maybe.rb +34 -11
  4. data/lib/kind/version.rb +1 -1
  5. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a234e4c63dea23906a65da0718515bff19ac7934a3e919535596042cfd605bba
4
- data.tar.gz: 5bcb0f1f669a0e87de070c5095cf8f769ddf97dde4b2169060fb21769ffbd623
3
+ metadata.gz: a0a947ffbd4a82b5ca7a74006ae8383c725393e1e8640364dc9e73874aac0eb0
4
+ data.tar.gz: d49f7382d4786e079c42ee7f45609ca8ca292ccb04e4d8ac299cdfa4cbf2e1c8
5
5
  SHA512:
6
- metadata.gz: 232d87a65618b51c3ec8756ad9d08bce53a49d50368d4629c8e694050502dddf12cdecd878193e40e09c8fcc13139b1a35383eedaa36a1660f7cf71f7e4d9810
7
- data.tar.gz: 4cef62a76d8981fa7409e0d598448b35b4bb01794084ad2ab264741cbf0a78e6b9142352369892e124f130750847078f90297e5d1eda955881ebea0f13e96ec7
6
+ metadata.gz: 77195516f80bae446150221c3c95a621ecacb70335f4594242b3d7f60e9de7ed5be32624c02183faf1ab5cc4717b0c229c82c52ec70aad6122d1fe51a71dfc92
7
+ data.tar.gz: 1f7ab4c70b8548dda0707d31ba9d500946c5314d8d9462ecdcd5ca0b23f1a197691d0eb6586e4a1628426794f1495d0303d2b1b7c4b32a1bcc73fdc88ea1123b
data/README.md CHANGED
@@ -48,6 +48,7 @@ One of the goals of this project is to do simple type checking like `"some strin
48
48
  - [Kind.of.\<Type\>.as_optional](#kindoftypeas_optional)
49
49
  - [Kind::Maybe(<Type>)](#kindmaybetype)
50
50
  - [Kind::Maybe#try](#kindmaybetry)
51
+ - [Kind::Maybe#try!](#kindmaybetry-1)
51
52
  - [Kind::Empty](#kindempty)
52
53
  - [Similar Projects](#similar-projects)
53
54
  - [Development](#development)
@@ -883,10 +884,11 @@ In these scenarios, you could check the given input type as optional and avoid u
883
884
 
884
885
  ```ruby
885
886
  def person_name(params)
886
- Kind::Of::Hash.as_optional(params)
887
- .map { |data| data if data.values_at(:first_name, :last_name).compact.size == 2 }
888
- .map { |data| "#{data[:first_name]} #{data[:last_name]}" }
889
- .value_or { 'John Doe' }
887
+ Kind::Of::Hash
888
+ .as_optional(params)
889
+ .map { |data| data if data.values_at(:first_name, :last_name).compact.size == 2 }
890
+ .map { |data| "#{data[:first_name]} #{data[:last_name]}" }
891
+ .value_or { 'John Doe' }
890
892
  end
891
893
 
892
894
  person_name('') # "John Doe"
@@ -907,6 +909,17 @@ def person_name(params)
907
909
  'John Doe'
908
910
  end
909
911
  end
912
+
913
+ #
914
+ # You can also use Kind::Optional(<Type>) to achieve the same behavior
915
+ #
916
+ def person_name(params)
917
+ Kind::Optional(Hash)
918
+ .wrap(params)
919
+ .map { |data| data if data.values_at(:first_name, :last_name).compact.size == 2 }
920
+ .map { |data| "#{data[:first_name]} #{data[:last_name]}" }
921
+ .value_or { 'John Doe' }
922
+ end
910
923
  ```
911
924
 
912
925
  > Note: You could use the `.as_optional` method (or it alias `.as_maybe`) with any [type checker](https://github.com/serradura/kind/blob/b177fed9cc2b3347d63963a2a2fd99f989c51a9a/README.md#type-checkers).
@@ -1025,18 +1038,18 @@ Kind::Optional(Numeric).wrap(5)
1025
1038
 
1026
1039
  ### Kind::Maybe#try
1027
1040
 
1028
- If you don't want to use a map to access the value, you could use the `#try` method to access it. So, if the value wasn't `nil` or `Kind::Undefined`, it will be returned.
1041
+ If you don't want to use `#map/#then` to access the value, you could use the `#try` method to access it. So, if the value wasn't `nil` or `Kind::Undefined`, the some monad will be returned.
1029
1042
 
1030
1043
  ```ruby
1031
1044
  object = 'foo'
1032
1045
 
1033
- Kind::Maybe[object].try(:upcase) # "FOO"
1046
+ Kind::Maybe[object].try(:upcase).value # "FOO"
1034
1047
 
1035
- Kind::Maybe[{}].try(:fetch, :number, 0) # 0
1048
+ Kind::Maybe[{}].try(:fetch, :number, 0).value # 0
1036
1049
 
1037
- Kind::Maybe[{number: 1}].try(:fetch, :number) # 1
1050
+ Kind::Maybe[{number: 1}].try(:fetch, :number).value # 1
1038
1051
 
1039
- Kind::Maybe[object].try { |value| value.upcase } # "FOO"
1052
+ Kind::Maybe[object].try { |value| value.upcase }.value # "FOO"
1040
1053
 
1041
1054
  #############
1042
1055
  # Nil value #
@@ -1044,9 +1057,9 @@ Kind::Maybe[object].try { |value| value.upcase } # "FOO"
1044
1057
 
1045
1058
  object = nil
1046
1059
 
1047
- Kind::Maybe[object].try(:upcase) # nil
1060
+ Kind::Maybe[object].try(:upcase).value # nil
1048
1061
 
1049
- Kind::Maybe[object].try { |value| value.upcase } # nil
1062
+ Kind::Maybe[object].try { |value| value.upcase }.value # nil
1050
1063
 
1051
1064
  #########################
1052
1065
  # Kind::Undefined value #
@@ -1054,12 +1067,26 @@ Kind::Maybe[object].try { |value| value.upcase } # nil
1054
1067
 
1055
1068
  object = Kind::Undefined
1056
1069
 
1057
- Kind::Maybe[object].try(:upcase) # nil
1070
+ Kind::Maybe[object].try(:upcase).value # nil
1071
+
1072
+ Kind::Maybe[object].try { |value| value.upcase }.value # nil
1073
+ ```
1074
+
1075
+ > **Note:** You can use the `#try` method with `Kind::Optional` objects.
1076
+
1077
+ [⬆️ Back to Top](#table-of-contents-)
1078
+
1079
+ ### Kind::Maybe#try!
1080
+
1081
+ Has the same behavior of its `#try`, but it will raise an error if the value doesn't respond to the expected method.
1082
+
1083
+ ```ruby
1084
+ Kind::Maybe[{}].try(:upcase) # => #<Kind::Maybe::None:0x0000... @value=nil>
1058
1085
 
1059
- Kind::Maybe[object].try { |value| value.upcase } # nil
1086
+ Kind::Maybe[{}].try!(:upcase) # => NoMethodError (undefined method `upcase' for {}:Hash)
1060
1087
  ```
1061
1088
 
1062
- > **Note:** You can use the try method with the `Kind::Optional`.
1089
+ > **Note:** You can also use the `#try!` method with `Kind::Optional` objects.
1063
1090
 
1064
1091
  [⬆️ Back to Top](#table-of-contents-)
1065
1092
 
@@ -68,12 +68,14 @@ module Kind
68
68
 
69
69
  alias_method :then, :map
70
70
 
71
- def try(method_name = Undefined, &block)
71
+ def try!(method_name = Undefined, &block)
72
72
  Kind.of.Symbol(method_name) if Undefined != method_name
73
73
 
74
- nil
74
+ NONE_WITH_NIL_VALUE
75
75
  end
76
76
 
77
+ alias_method :try, :try!
78
+
77
79
  private_constant :INVALID_DEFAULT_ARG
78
80
  end
79
81
 
@@ -92,22 +94,43 @@ module Kind
92
94
  def map(&fn)
93
95
  result = fn.call(@value)
94
96
 
95
- return result if Maybe::None === result
96
- return NONE_WITH_NIL_VALUE if result.nil?
97
- return NONE_WITH_UNDEFINED_VALUE if Undefined == result
98
-
99
- Some.new(result)
97
+ resolve(result)
100
98
  end
101
99
 
102
100
  alias_method :then, :map
103
101
 
104
- def try(method_name = Undefined, *args, &block)
105
- fn = Undefined == method_name ? block : Kind.of.Symbol(method_name).to_proc
102
+ def try!(method_name = Undefined, *args, &block)
103
+ Kind::Of::Symbol(method_name) if Undefined != method_name
106
104
 
107
- result = args.empty? ? fn.call(value) : fn.call(*args.unshift(value))
105
+ __try__(method_name, args, block)
106
+ end
108
107
 
109
- return result if Maybe::Value.some?(result)
108
+ def try(method_name = Undefined, *args, &block)
109
+ if (Undefined != method_name && value.respond_to?(Kind::Of::Symbol(method_name))) ||
110
+ (Undefined == method_name && block)
111
+ __try__(method_name, args, block)
112
+ else
113
+ NONE_WITH_NIL_VALUE
114
+ end
110
115
  end
116
+
117
+ private
118
+
119
+ def __try__(method_name = Undefined, args, block)
120
+ fn = Undefined == method_name ? block : method_name.to_proc
121
+
122
+ result = args.empty? ? fn.call(value) : fn.call(*args.unshift(value))
123
+
124
+ resolve(result)
125
+ end
126
+
127
+ def resolve(result)
128
+ return result if Maybe::None === result
129
+ return NONE_WITH_NIL_VALUE if result.nil?
130
+ return NONE_WITH_UNDEFINED_VALUE if Undefined == result
131
+
132
+ Some.new(result)
133
+ end
111
134
  end
112
135
 
113
136
  def self.new(value)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Kind
4
- VERSION = '2.3.0'
4
+ VERSION = '3.0.0'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kind
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.3.0
4
+ version: 3.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rodrigo Serradura