lamy_result 0.2.1 → 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9de251784cf83e80ac19a2f8d08bd00de25269ccae683b518570bcf710cf5918
4
- data.tar.gz: 287d464139c3ae445e4cbdf477e9ea4109f08dde36db4394e79f5613fc5be00d
3
+ metadata.gz: 7f54a9321e1e265a16acd63a030358c51ae01b1d7772625eed4fbc096c93eece
4
+ data.tar.gz: f8fd3fd1017bd56d64f7f0a89d75fb8ea3f1144ed250a881b34385acb8b7aa43
5
5
  SHA512:
6
- metadata.gz: 6845ef3a74016eebfdd7c34aa55cca0f4044cf0bdf89fff49f5c31d24393b7165b5e6a648dd08ae5ab17c079d1cbfff8d9a5be31c83693f934a3e6cfcc65141f
7
- data.tar.gz: 7eb32ea5eb7c3d4da5ff15905fece148885fcdd352d3c06cf4a80e455fd90d88eeacf5212a23fe79c658c3ccd7d787488e609e6a0ab976ba632c134d669ce5a7
6
+ metadata.gz: 89b224ea4e39b5289c2afb5f77ae9c6cc057800e0f3c264834b700def4ce2d10a21f36b71f60e1bf6b213b176b30efe3a7ed31117a7ec692e5f74faecd571903
7
+ data.tar.gz: 8bd659a264d38e898ebc690ed9aa49f0304e1975bb0313bc1bed72c9e21937b6ecc9dc1215d160833d42d53b1a65d30b1b75ca9fed74e561a3f0a82bfdfeb241
data/CHANGELOG.md CHANGED
@@ -1,13 +1,24 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.3.0] - 2022-03-13
4
+
5
+ ### Added
6
+
7
+ - `#any?` to compare the instance status against array of statuses.
8
+
9
+ ### Changed
10
+
11
+ - Conditional "#<status>_then" now returns nil instead of self if there is no match.
12
+ - Wrap checks with .format_status method to reliably format the status...
13
+
3
14
  ## [0.2.0] - 2022-03-07
4
15
 
5
- ### Add
16
+ ### Added
6
17
 
7
18
  - Allow aliases of the status tags to be defined with the .define_status_tags
8
19
  method.
9
20
 
10
- ### Remove
21
+ ### Removed
11
22
 
12
23
  - No longer define methods with the method_missing hook.
13
24
 
data/lamy_result.gemspec CHANGED
@@ -11,50 +11,9 @@ Gem::Specification.new do |spec|
11
11
  spec.summary = 'Wrap results with a status and a value.'
12
12
  spec.description = <<~EOF
13
13
  The project is inspired by Elixir and Erlang's tagged tuple and Rust's Result/Option. Despite inspiration from other languages, Lamy Result aims to be idiomatic Ruby and runtime dependency-free.
14
-
15
- Rather than change the way you're doing things. Returning a single result is great for most cases. You'll know if and when you need something more.
16
-
17
- ```ruby
18
- include LamyResult
19
-
20
- result = Lamy.ok('Lamy is awesome')
21
-
22
- if result.ok?
23
- do_something_cool result.value
24
- end
25
-
26
- # or
27
-
28
- # Will only evaluate if the status is :ok
29
- result.ok_then do |v|
30
- do_something_cool v
31
- end
32
-
33
- result.to_a
34
- # Output: [:ok, 'Lamy is awesome']
35
-
36
- result.to_h
37
- # Output: { status: :ok, value: 'Lamy is awesome' }
38
-
39
-
40
- # Aliases allow for natural expression.
41
-
42
- def do_another_cool_thing
43
- # Report success
44
- Lamy.success('It worked')
45
- end
46
-
47
- another_result = do_another_cool_thing
48
-
49
- # As in "Was the operation successful?"
50
- another_result.successful?
51
-
52
- # As in "Has the operation succeeded?"
53
- another_result.succeeded?
54
- ```
55
14
  EOF
56
15
 
57
- spec.homepage = 'https://evrnetwork.co.za'
16
+ spec.homepage = 'https://github.com/JedBurke/LamyResult'
58
17
  spec.license = 'Apache-2.0'
59
18
  spec.required_ruby_version = ">= 2.5.0"
60
19
 
@@ -15,11 +15,7 @@ module LamyResult
15
15
  # See .define_status_tags for the method defined at runtime.
16
16
  class Lamy
17
17
  def initialize(status:, value: nil)
18
- @status = status
19
- .to_s
20
- .downcase
21
- .to_sym
22
-
18
+ @status = Lamy.format_status(status)
23
19
  @value = value
24
20
  end
25
21
 
@@ -31,11 +27,25 @@ module LamyResult
31
27
  # Compares the current instance's status to the input value. This is a
32
28
  # lov-level method meant to be used by the #ok? and #success? methods.
33
29
  def status_is?(value)
34
- @status == value.to_sym
30
+ @status == Lamy.format_status(value)
31
+ end
32
+
33
+ # Compares the status with the input values and returns true if it matches
34
+ # any of them.
35
+ #
36
+ # Usage:
37
+ # status = Lamy.ok('Yukihana Lamy is awesome')
38
+ # status.any?(:ok, :success, :good)
39
+ #
40
+ def any?(*statuses)
41
+ statuses.any? {|status| status_is?(status) }
35
42
  end
36
43
 
44
+ # Consider: Add assert_any_then(*values, &block)
45
+ # either_then(*statuses,)
46
+
37
47
  # Calls #status_is? on the `status_to_check` and if true, yields the value.
38
- # Otherwise, returns the instance. This is a low-level method meant to be
48
+ # Otherwise, returns nil. This is a low-level method meant to be
39
49
  # used by #ok_then and #success_then methods.
40
50
  def assert_status_then(status_to_check:)
41
51
  if status_is?(status_to_check)
@@ -44,7 +54,7 @@ module LamyResult
44
54
  return @value
45
55
  end
46
56
 
47
- self
57
+ nil
48
58
  end
49
59
 
50
60
  # Returns the status attribute as a symbol or a TrueClass/FalseClass. This
@@ -53,9 +63,9 @@ module LamyResult
53
63
  # If the status is a boolean, return the status as a boolean.
54
64
  # Otherwise, return it as a symbol, which it should already be.
55
65
  if true? || false?
56
- @status.to_s.to_sym == true.to_s.to_sym
66
+ Lamy.format_status(@status) == Lamy.format_status(true)
57
67
  else
58
- @status.to_s.to_sym
68
+ Lamy.format_status(@status)
59
69
  end
60
70
  end
61
71
 
@@ -100,12 +110,19 @@ module LamyResult
100
110
  end
101
111
  end
102
112
 
113
+ def self.format_status(status)
114
+ status
115
+ .to_s
116
+ .downcase
117
+ .to_sym
118
+ end
119
+
103
120
  def self.format_status_check_method(status)
104
- "#{status.to_s}?".to_sym
121
+ self.format_status("#{status}?")
105
122
  end
106
123
 
107
124
  def self.format_conditional_method(status)
108
- "#{status.to_s}_then".to_sym
125
+ self.format_status("#{status}_then")
109
126
  end
110
127
 
111
128
  def self.define_status_method(status, *aliases)
@@ -139,6 +156,7 @@ module LamyResult
139
156
 
140
157
  def self.define_conditional_then_method(status, *aliases)
141
158
  method_symbol = format_conditional_method(status)
159
+
142
160
  status_conditional_aliases = aliases.map do |s|
143
161
  format_conditional_method(s)
144
162
  end
@@ -149,10 +167,7 @@ module LamyResult
149
167
 
150
168
  assert_new_instance_method! method_symbol
151
169
 
152
- define_instance_aliases_for(
153
- method_symbol,
154
- *status_conditional_aliases
155
- )
170
+ define_instance_aliases_for(method_symbol, *status_conditional_aliases)
156
171
  end
157
172
 
158
173
  # Checks if the method is an instance method, otherwise raises a
@@ -201,6 +216,7 @@ module LamyResult
201
216
  # the #status_is? method.
202
217
  alias == status_is?
203
218
  alias eql? status_is?
219
+ alias either? any?
204
220
 
205
221
  class << self
206
222
  # The singular form may be more comfortable for users wanting to define
@@ -215,8 +231,8 @@ module LamyResult
215
231
 
216
232
  # Add the basic tags to the class.
217
233
  Lamy.define_status_tags(
218
- [:succeeded, :success, :successful],
219
- [:failed, :fail, :error],
234
+ %i[succeeded success successful],
235
+ %i[failed fail error],
220
236
  :ok,
221
237
  :true,
222
238
  :false
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module LamyResult
4
- VERSION = '0.2.1'
4
+ VERSION = '0.3.0'
5
5
  end
metadata CHANGED
@@ -1,58 +1,20 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lamy_result
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jed Burke
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-04-10 00:00:00.000000000 Z
11
+ date: 2022-04-13 00:00:00.000000000 Z
12
12
  dependencies: []
13
- description: |
14
- The project is inspired by Elixir and Erlang's tagged tuple and Rust's Result/Option. Despite inspiration from other languages, Lamy Result aims to be idiomatic Ruby and runtime dependency-free.
13
+ description: 'The project is inspired by Elixir and Erlang''s tagged tuple and Rust''s
14
+ Result/Option. Despite inspiration from other languages, Lamy Result aims to be
15
+ idiomatic Ruby and runtime dependency-free.
15
16
 
16
- Rather than change the way you're doing things. Returning a single result is great for most cases. You'll know if and when you need something more.
17
-
18
- ```ruby
19
- include LamyResult
20
-
21
- result = Lamy.ok('Lamy is awesome')
22
-
23
- if result.ok?
24
- do_something_cool result.value
25
- end
26
-
27
- # or
28
-
29
- # Will only evaluate if the status is :ok
30
- result.ok_then do |v|
31
- do_something_cool v
32
- end
33
-
34
- result.to_a
35
- # Output: [:ok, 'Lamy is awesome']
36
-
37
- result.to_h
38
- # Output: { status: :ok, value: 'Lamy is awesome' }
39
-
40
-
41
- # Aliases allow for natural expression.
42
-
43
- def do_another_cool_thing
44
- # Report success
45
- Lamy.success('It worked')
46
- end
47
-
48
- another_result = do_another_cool_thing
49
-
50
- # As in "Was the operation successful?"
51
- another_result.successful?
52
-
53
- # As in "Has the operation succeeded?"
54
- another_result.succeeded?
55
- ```
17
+ '
56
18
  email:
57
19
  - opensource@evrnetwork.co.za
58
20
  executables: []
@@ -73,11 +35,11 @@ files:
73
35
  - lib/lamy_result.rb
74
36
  - lib/lamy_result/lamy.rb
75
37
  - lib/lamy_result/version.rb
76
- homepage: https://evrnetwork.co.za
38
+ homepage: https://github.com/JedBurke/LamyResult
77
39
  licenses:
78
40
  - Apache-2.0
79
41
  metadata:
80
- homepage_uri: https://evrnetwork.co.za
42
+ homepage_uri: https://github.com/JedBurke/LamyResult
81
43
  source_code_uri: https://github.com/JedBurke/LamyResult
82
44
  changelog_uri: https://github.com/JedBurke/LamyResult/blob/master/CHANGELOG.md
83
45
  post_install_message: