mu-result 1.0.0 → 2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 66c2f073332bdb1c48fdb2f64230c8f4d6f00b4f7131ae12b2d88fede4409aa8
4
- data.tar.gz: 90f6420883ca8d40ed5cc8f0b1d006d27f8b88e441e927dce4c2261923448a3c
3
+ metadata.gz: fac87f8bfeb00091ef1bf1e340c905d57e8c1153ea860e4b59e609fe1a3b39fa
4
+ data.tar.gz: 7a92f8b7a12a0894e1ee3a50f712fdab236c36d119f8f9686b5fc2e79ed00586
5
5
  SHA512:
6
- metadata.gz: 9d56cc528a0e37f3c96487e679b6276649738069005b2495b78740257116d2ecef1720417c2b0dc8b324e6dd3ddf88121d4a3fe7940507ffceb0e81497ed957d
7
- data.tar.gz: b74a9e5440305dc532cb0b265d3a9d93c8ff79fcac940e1c52c9b40c1fa16ba445312da9306e41c8aca6ff5e97f040e7e9e740095d253525d59a29b2ab7b8039
6
+ metadata.gz: ddc000eda0bdface21287f5e8bb3e32849898a2b270cdf698db350a00adfaf69d9e65fc1be090233022045ba75040b82d60f2c09fb11fa4b8c784c904f62c161
7
+ data.tar.gz: 447eb2d9cc8bb51456b576f026894cb0e3cc900fbcce7e414772037ff89ad4e3512e0ab32eb7819fdb5349752703ffad90e132e538a26d18f99940dde02363b7
data/CHANGELOG.md ADDED
@@ -0,0 +1,6 @@
1
+ # Changelog
2
+
3
+ ## 1.2.0
4
+
5
+ - Adds `BaseResult#unwrap`.
6
+
data/Gemfile CHANGED
@@ -1,6 +1,4 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
- git_source(:github) {|repo_name| 'https://github.com/#{repo_name}' }
4
-
5
3
  # Specify your gem's dependencies in mu-result.gemspec
6
4
  gemspec
data/Gemfile.lock CHANGED
@@ -1,35 +1,35 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- mu-result (1.0.0)
4
+ mu-result (2.1.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
8
8
  specs:
9
- diff-lcs (1.3)
10
- rake (10.5.0)
11
- rspec (3.8.0)
12
- rspec-core (~> 3.8.0)
13
- rspec-expectations (~> 3.8.0)
14
- rspec-mocks (~> 3.8.0)
15
- rspec-core (3.8.0)
16
- rspec-support (~> 3.8.0)
17
- rspec-expectations (3.8.2)
9
+ diff-lcs (1.4.4)
10
+ rake (13.0.3)
11
+ rspec (3.10.0)
12
+ rspec-core (~> 3.10.0)
13
+ rspec-expectations (~> 3.10.0)
14
+ rspec-mocks (~> 3.10.0)
15
+ rspec-core (3.10.1)
16
+ rspec-support (~> 3.10.0)
17
+ rspec-expectations (3.10.1)
18
18
  diff-lcs (>= 1.2.0, < 2.0)
19
- rspec-support (~> 3.8.0)
20
- rspec-mocks (3.8.0)
19
+ rspec-support (~> 3.10.0)
20
+ rspec-mocks (3.10.2)
21
21
  diff-lcs (>= 1.2.0, < 2.0)
22
- rspec-support (~> 3.8.0)
23
- rspec-support (3.8.0)
22
+ rspec-support (~> 3.10.0)
23
+ rspec-support (3.10.2)
24
24
 
25
25
  PLATFORMS
26
26
  ruby
27
27
 
28
28
  DEPENDENCIES
29
- bundler (~> 1.16)
29
+ bundler (~> 2.2)
30
30
  mu-result!
31
- rake (~> 10.0)
31
+ rake (~> 13.0)
32
32
  rspec (~> 3.0)
33
33
 
34
34
  BUNDLED WITH
35
- 1.16.6
35
+ 2.2.15
data/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # Mu::Result
2
2
 
3
+ [![Gem Version](https://badge.fury.io/rb/mu-result.svg)](https://badge.fury.io/rb/mu-result)
4
+
3
5
  This is a really tiny implementation of the Result object.
4
6
 
5
7
  ## Installation
@@ -22,6 +24,7 @@ Or install it yourself as:
22
24
 
23
25
  ```ruby
24
26
  include 'mu'
27
+
25
28
  result = Result.success
26
29
  result.success? # => true
27
30
  result = Result.error
@@ -32,6 +35,25 @@ result = Result.success(name: 'Arthur Dent', answer: 42)
32
35
  result.data # => { name: 'Arthur Dent', answer: 42 }
33
36
  ```
34
37
 
38
+ ### Unwrap
39
+
40
+ When you want to access to a specific field of the result, you can _unwrap_ it:
41
+
42
+ ```ruby
43
+ result = Result.success(user: 'olistik', role: :developer)
44
+ result.unwrap(:user) # => 'olistik'
45
+ result.unwrap(:role) # => :developer
46
+ result.unwrap(:name) # => raises `StandardError (The symbol 'name' is not included in the result data object.)`
47
+ ```
48
+
49
+ This is a way to avoid getting unwanted nil values out of the result when passing the wrong field:
50
+
51
+ ```ruby
52
+ result = Result.success(user: 'olistik', role: :developer)
53
+ value = result.data[:users] # => nil, this could lead to an annoying bug later on because we're not aware there's a typo in `:users`, yet.
54
+ value = result.unwrap(:users) # This raises an exception instead, making it evident that we're using the wrong name for the field.
55
+ ```
56
+
35
57
  ## Development
36
58
 
37
59
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -40,8 +62,8 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
40
62
 
41
63
  ## Contributing
42
64
 
43
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/mu-result. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
65
+ Bug reports and pull requests are welcome at https://source.olisti.co/olistik/mu-result. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
44
66
 
45
67
  ## Code of Conduct
46
68
 
47
- Everyone interacting in the Mu::Result project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/mu-result/blob/master/CODE_OF_CONDUCT.md).
69
+ Everyone interacting in the Mu::Result project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://source.olisti.co/olistik/mu-result/blob/master/CODE_OF_CONDUCT.md).
data/lib/mu/result.rb CHANGED
@@ -1,41 +1,16 @@
1
1
  require 'mu/result/version'
2
+ require 'mu/result/base_result'
2
3
 
3
4
  module Mu
4
5
  module Result
5
- class BaseResult
6
- def initialize(code: :ok, data: nil)
7
- @code = code
8
- @data = data
9
- end
10
-
11
- def code!(value)
12
- @code = value
13
- self
14
- end
15
-
16
- def code
17
- @code
18
- end
19
-
20
- def data
21
- @data
22
- end
23
-
24
- def success?
25
- @code == :ok
26
- end
27
-
28
- def error?
29
- !success?
30
- end
31
- end
32
6
 
33
7
  def self.success(data = nil)
34
- BaseResult.new(code: :ok, data: data)
8
+ BaseResult.new(is_success: true, code: :ok, data: data)
35
9
  end
36
10
 
37
11
  def self.error(data = nil)
38
- BaseResult.new(code: :ko, data: data)
12
+ BaseResult.new(is_success: false, code: :ko, data: data)
39
13
  end
14
+
40
15
  end
41
16
  end
@@ -0,0 +1,51 @@
1
+ module Mu
2
+ module Result
3
+
4
+ class BaseResult
5
+ def initialize(is_success: true, code: :ok, data: nil)
6
+ @is_success = is_success
7
+ @code = code
8
+ @data = data
9
+ end
10
+
11
+ def code!(value)
12
+ @code = value
13
+ self
14
+ end
15
+
16
+ def code
17
+ @code
18
+ end
19
+
20
+ def data
21
+ @data
22
+ end
23
+
24
+ def success?
25
+ @is_success == true
26
+ end
27
+
28
+ def error?
29
+ !success?
30
+ end
31
+
32
+ def unwrap(symbol = nil)
33
+ return data if symbol.nil?
34
+
35
+ if !data.respond_to?(:include?) || !data.include?(symbol)
36
+ raise StandardError.new("The symbol '#{symbol}' is not included in the result data object.")
37
+ end
38
+
39
+ return data[symbol]
40
+ end
41
+
42
+ def to_hash
43
+ {
44
+ code: code,
45
+ data: data,
46
+ }
47
+ end
48
+ end
49
+
50
+ end
51
+ end
@@ -1,5 +1,5 @@
1
1
  module Mu
2
2
  module Result
3
- VERSION = '1.0.0'
3
+ VERSION = '2.1.0'
4
4
  end
5
5
  end
data/mu-result.gemspec CHANGED
@@ -12,7 +12,7 @@ Gem::Specification.new do |spec|
12
12
 
13
13
  spec.summary = %q{μResult is a small Result object}
14
14
  spec.description = %q{μ as in micro}
15
- spec.homepage = 'https://source.olisti.co/olistik/mu-result'
15
+ spec.homepage = 'https://github.com/olistik/mu-result'
16
16
 
17
17
  # Specify which files should be added to the gem when it is released.
18
18
  # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
@@ -23,7 +23,7 @@ Gem::Specification.new do |spec|
23
23
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
24
  spec.require_paths = ['lib']
25
25
 
26
- spec.add_development_dependency 'bundler', '~> 1.16'
27
- spec.add_development_dependency 'rake', '~> 10.0'
26
+ spec.add_development_dependency 'bundler', '~> 2.2'
27
+ spec.add_development_dependency 'rake', '~> 13.0'
28
28
  spec.add_development_dependency 'rspec', '~> 3.0'
29
29
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mu-result
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - olistik
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-11-09 00:00:00.000000000 Z
11
+ date: 2021-07-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -16,28 +16,28 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '1.16'
19
+ version: '2.2'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '1.16'
26
+ version: '2.2'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '10.0'
33
+ version: '13.0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '10.0'
40
+ version: '13.0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rspec
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -62,6 +62,7 @@ files:
62
62
  - ".gitignore"
63
63
  - ".rspec"
64
64
  - ".travis.yml"
65
+ - CHANGELOG.md
65
66
  - CODE_OF_CONDUCT.md
66
67
  - Gemfile
67
68
  - Gemfile.lock
@@ -70,9 +71,10 @@ files:
70
71
  - bin/console
71
72
  - bin/setup
72
73
  - lib/mu/result.rb
74
+ - lib/mu/result/base_result.rb
73
75
  - lib/mu/result/version.rb
74
76
  - mu-result.gemspec
75
- homepage: https://source.olisti.co/olistik/mu-result
77
+ homepage: https://github.com/olistik/mu-result
76
78
  licenses:
77
79
  - AGPL-3.0
78
80
  metadata: {}
@@ -91,8 +93,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
91
93
  - !ruby/object:Gem::Version
92
94
  version: '0'
93
95
  requirements: []
94
- rubyforge_project:
95
- rubygems_version: 2.7.8
96
+ rubygems_version: 3.2.15
96
97
  signing_key:
97
98
  specification_version: 4
98
99
  summary: μResult is a small Result object