resol 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: f71f97e92852c4b25525cf1388c8cc671bc5587d7535fa844e3a681c686c14d1
4
+ data.tar.gz: 996140397a4d0c0bd04a426c30f0da88a423495e9e80715e2722a1089fd3656e
5
+ SHA512:
6
+ metadata.gz: 638959dfb203d736a85c79c3c271b716d08be263e88b8509210ff712715f12fddcfd27d2600dfbee1e0cf39fd84b89def3a0ee1323bb27451d90cbb407cc8edb
7
+ data.tar.gz: 1b89eb69a71e3d43da7520e4f90789b7e6b2a33716dabc9b163a8d6fc1ce70a424c40430b5e982ee3adec3e7fd0fed1c17921adf1f1e1706199febaa7f8fbcfd
@@ -0,0 +1,25 @@
1
+ name: Ruby
2
+
3
+ on: [push,pull_request]
4
+
5
+ jobs:
6
+ build:
7
+ runs-on: ubuntu-latest
8
+ steps:
9
+ - uses: actions/checkout@v2
10
+
11
+ - name: Set up Ruby
12
+ uses: ruby/setup-ruby@v1
13
+ with:
14
+ ruby-version: 2.7.0
15
+
16
+ - name: Bundling
17
+ run: |
18
+ gem install bundler -v 2.2.3
19
+ bundle install
20
+
21
+ - name: Linting
22
+ run: bin/rubocop
23
+
24
+ - name: Testing
25
+ run: COVER=1 bin/rspec
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rubocop.yml ADDED
@@ -0,0 +1,23 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.6
3
+ NewCops: enable
4
+
5
+ Style/StringLiterals:
6
+ Enabled: true
7
+ EnforcedStyle: double_quotes
8
+
9
+ Style/StringLiteralsInInterpolation:
10
+ Enabled: true
11
+ EnforcedStyle: double_quotes
12
+
13
+ Style/RaiseArgs:
14
+ Enabled: false
15
+
16
+ Style/Documentation:
17
+ Enabled: false
18
+
19
+ Layout/LineLength:
20
+ Max: 100
21
+
22
+ Layout/FirstArrayElementIndentation:
23
+ EnforcedStyle: consistent
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,97 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ resol (0.1.0)
5
+ smart_initializer
6
+ uber
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ ast (2.4.2)
12
+ coderay (1.1.3)
13
+ coveralls (0.7.2)
14
+ multi_json (~> 1.3)
15
+ rest-client (= 1.6.7)
16
+ simplecov (>= 0.7)
17
+ term-ansicolor (= 1.2.2)
18
+ thor (= 0.18.1)
19
+ diff-lcs (1.4.4)
20
+ docile (1.3.5)
21
+ method_source (1.0.0)
22
+ mime-types (3.3.1)
23
+ mime-types-data (~> 3.2015)
24
+ mime-types-data (3.2021.0225)
25
+ multi_json (1.15.0)
26
+ parallel (1.20.1)
27
+ parser (3.0.1.0)
28
+ ast (~> 2.4.1)
29
+ pry (0.14.0)
30
+ coderay (~> 1.1)
31
+ method_source (~> 1.0)
32
+ qonfig (0.25.0)
33
+ rainbow (3.0.0)
34
+ rake (13.0.3)
35
+ regexp_parser (2.1.1)
36
+ rest-client (1.6.7)
37
+ mime-types (>= 1.16)
38
+ rexml (3.2.5)
39
+ rspec (3.10.0)
40
+ rspec-core (~> 3.10.0)
41
+ rspec-expectations (~> 3.10.0)
42
+ rspec-mocks (~> 3.10.0)
43
+ rspec-core (3.10.1)
44
+ rspec-support (~> 3.10.0)
45
+ rspec-expectations (3.10.1)
46
+ diff-lcs (>= 1.2.0, < 2.0)
47
+ rspec-support (~> 3.10.0)
48
+ rspec-mocks (3.10.2)
49
+ diff-lcs (>= 1.2.0, < 2.0)
50
+ rspec-support (~> 3.10.0)
51
+ rspec-support (3.10.2)
52
+ rubocop (0.93.1)
53
+ parallel (~> 1.10)
54
+ parser (>= 2.7.1.5)
55
+ rainbow (>= 2.2.2, < 4.0)
56
+ regexp_parser (>= 1.8)
57
+ rexml
58
+ rubocop-ast (>= 0.6.0)
59
+ ruby-progressbar (~> 1.7)
60
+ unicode-display_width (>= 1.4.0, < 2.0)
61
+ rubocop-ast (1.4.1)
62
+ parser (>= 2.7.1.5)
63
+ ruby-progressbar (1.11.0)
64
+ simplecov (0.21.2)
65
+ docile (~> 1.1)
66
+ simplecov-html (~> 0.11)
67
+ simplecov_json_formatter (~> 0.1)
68
+ simplecov-html (0.12.3)
69
+ simplecov_json_formatter (0.1.2)
70
+ smart_engine (0.11.0)
71
+ smart_initializer (0.5.0)
72
+ qonfig (~> 0.24)
73
+ smart_engine (~> 0.11)
74
+ smart_types (~> 0.4)
75
+ smart_types (0.4.0)
76
+ smart_engine (~> 0.11)
77
+ term-ansicolor (1.2.2)
78
+ tins (~> 0.8)
79
+ thor (0.18.1)
80
+ tins (0.13.2)
81
+ uber (0.1.0)
82
+ unicode-display_width (1.7.0)
83
+
84
+ PLATFORMS
85
+ x86_64-darwin-19
86
+
87
+ DEPENDENCIES
88
+ coveralls
89
+ pry
90
+ rake
91
+ resol!
92
+ rspec
93
+ rubocop
94
+ simplecov
95
+
96
+ BUNDLED WITH
97
+ 2.2.3
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 appuser
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,94 @@
1
+ # Resol
2
+
3
+ ![https://github.com/umbrellio/resol](https://github.com/umbrellio/resol/actions/workflows/main.yml/badge.svg)
4
+
5
+ Ruby Gem for creating simple service objects and other any object ruby patterns.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'resol'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install resol
22
+
23
+ ## Usage
24
+
25
+ ### Sample usage
26
+
27
+ ```ruby
28
+ class FindUser < Resol::Service
29
+ param :id
30
+
31
+ def call
32
+ user = User.find(id)
33
+ if user
34
+ success!(user)
35
+ else
36
+ fail!(:not_found)
37
+ end
38
+ end
39
+ end
40
+
41
+ # when user exists
42
+ FindUser.call(1) # => Resol::Success(<user object>)
43
+ FindUser.call!(1) # => <user object>
44
+
45
+ # when user doesn't exist
46
+ FindUser.call(100) # => Resol::Failure(:not_found)
47
+ FindUser.call!(100) # => raised FindUser::Failure: :not_found
48
+ ```
49
+
50
+ So method `.call` always returns the [result object](#result-objects)
51
+ and method `.call!` returns a value or throws an error (in case of `fail!` has been called).
52
+
53
+ #### Params defining
54
+
55
+ All incoming params and options can be defined using a [smart_initializer](https://github.com/smart-rb/smart_initializer) gem interface.
56
+
57
+ #### Return a result
58
+
59
+ **Note** – calling `success!`/`fail!` methods interrupts `call` method execution.
60
+
61
+ - `success!(value)` – finish with a success value
62
+ - `fail!(code, data = nil)` – fail with any error code and optional data
63
+
64
+ ### Result objects
65
+
66
+ Methods:
67
+
68
+ - `success?` – returns `true` for success result and `false` for failure result
69
+ - `failure?` – returns `true` for failure result and `false` for success result
70
+ - `value!` – unwraps a result object, returns the value for success result, and throws an error for failure result
71
+ - `value_or(other_value, &block)` – returns a value for success result or `other_value` for failure result (either calls `block` in case it given)
72
+
73
+ ## Development
74
+
75
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/rspec` to run the tests.
76
+ You can also run `bin/console` for an interactive prompt that will allow you to experiment.
77
+
78
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
79
+
80
+ ## Contributing
81
+
82
+ Bug reports and pull requests are welcome on GitHub at https://github.com/umbrellio/resol.
83
+
84
+ ## License
85
+
86
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
87
+
88
+ ## Authors
89
+
90
+ Created by [Aleksei Bespalov](https://github.com/nulldef).
91
+
92
+ <a href="https://github.com/umbrellio/">
93
+ <img style="float: left;" src="https://umbrellio.github.io/Umbrellio/supported_by_umbrellio.svg" alt="Supported by Umbrellio" width="439" height="72">
94
+ </a>
data/Rakefile ADDED
@@ -0,0 +1,12 @@
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 "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
data/bin/console ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "resol"
6
+ require "pry"
7
+
8
+ Pry.start
data/bin/rubocop ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle exec rubocop
data/bin/setup ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
data/lib/resol.rb ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "uber/builder"
4
+ require "smart_core/initializer"
5
+
6
+ require_relative "resol/version"
7
+ require_relative "resol/service"
8
+
9
+ module Resol
10
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Resol
4
+ module Builder
5
+ def self.included(base)
6
+ base.include(Uber::Builder)
7
+ base.extend(ClassMethods)
8
+ end
9
+
10
+ module ClassMethods
11
+ def build_klass(*args, **kwargs)
12
+ klass = self
13
+
14
+ loop do
15
+ new_klass = klass.build!(klass, *args, **kwargs)
16
+ break if new_klass == klass
17
+
18
+ klass = new_klass
19
+ end
20
+
21
+ klass
22
+ end
23
+
24
+ def build(*args)
25
+ build_klass(*args).new(*args)
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,72 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Resol
4
+ class UnwrapError < StandardError; end
5
+
6
+ class Result
7
+ # @!method success?
8
+ # @!method failure?
9
+ # @!method value_or
10
+ # @!method value!
11
+
12
+ def initialize(*); end
13
+ end
14
+
15
+ class Success < Result
16
+ def initialize(value)
17
+ super
18
+ @value = value
19
+ end
20
+
21
+ def success?
22
+ true
23
+ end
24
+
25
+ def failure?
26
+ false
27
+ end
28
+
29
+ def value_or(*)
30
+ @value
31
+ end
32
+
33
+ def value!
34
+ @value
35
+ end
36
+ end
37
+
38
+ class Failure < Result
39
+ def initialize(error)
40
+ super
41
+ @value = error
42
+ end
43
+
44
+ def success?
45
+ false
46
+ end
47
+
48
+ def failure?
49
+ true
50
+ end
51
+
52
+ def value_or(other_value = nil)
53
+ if block_given?
54
+ yield(@value)
55
+ else
56
+ other_value
57
+ end
58
+ end
59
+
60
+ def value!
61
+ raise UnwrapError, "Failure result #{@value.inspect}"
62
+ end
63
+ end
64
+
65
+ def self.Success(*args)
66
+ Success.new(*args)
67
+ end
68
+
69
+ def self.Failure(*args)
70
+ Failure.new(*args)
71
+ end
72
+ end
@@ -0,0 +1,81 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "builder"
4
+ require_relative "result"
5
+
6
+ module Resol
7
+ class Service
8
+ class InvalidCommandImplementation < StandardError; end
9
+
10
+ class Interruption < StandardError
11
+ attr_accessor :data
12
+
13
+ def initialize(data)
14
+ self.data = data
15
+ super
16
+ end
17
+
18
+ def inspect
19
+ "#{self.class.name}: #{message}"
20
+ end
21
+
22
+ def message
23
+ data.inspect
24
+ end
25
+ end
26
+
27
+ class Failure < Interruption
28
+ attr_accessor :code
29
+
30
+ def initialize(code, data)
31
+ self.code = code
32
+ super(data)
33
+ end
34
+
35
+ def message
36
+ data ? "#{code.inspect} => #{data.inspect}" : code.inspect
37
+ end
38
+ end
39
+
40
+ class Success < Interruption; end
41
+
42
+ include SmartCore::Initializer
43
+ include Resol::Builder
44
+
45
+ class << self
46
+ def inherited(klass)
47
+ klass.const_set(:Failure, Class.new(klass::Failure))
48
+ klass.const_set(:Success, Class.new(klass::Success))
49
+ super
50
+ end
51
+
52
+ def call(*args, **options, &block)
53
+ command = build(*args, **options)
54
+ command.call(&block)
55
+
56
+ error_message = "No success! or fail! called in the #call method in #{command.class}"
57
+ raise InvalidCommandImplementation, error_message
58
+ rescue self::Success => e
59
+ Resol::Success(e.data)
60
+ rescue self::Failure => e
61
+ Resol::Failure(e)
62
+ end
63
+
64
+ def call!(*args)
65
+ call(*args).value_or { |error| raise error }
66
+ end
67
+ end
68
+
69
+ # @!method call
70
+
71
+ private
72
+
73
+ def fail!(code, data = nil)
74
+ raise self.class::Failure.new(code, data)
75
+ end
76
+
77
+ def success!(data = nil)
78
+ raise self.class::Success.new(data)
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Resol
4
+ VERSION = "0.1.0"
5
+ end
metadata ADDED
@@ -0,0 +1,172 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: resol
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Aleksei Bespalov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-04-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: smart_initializer
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: uber
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
+ - !ruby/object:Gem::Dependency
42
+ name: coveralls
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rubocop
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: simplecov
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ description: Gem for creating (any) object patterns
126
+ email:
127
+ - nulldefiner@gmail.com
128
+ - oss@umbrellio.biz
129
+ executables: []
130
+ extensions: []
131
+ extra_rdoc_files: []
132
+ files:
133
+ - ".github/workflows/main.yml"
134
+ - ".gitignore"
135
+ - ".rubocop.yml"
136
+ - Gemfile
137
+ - Gemfile.lock
138
+ - LICENSE.txt
139
+ - README.md
140
+ - Rakefile
141
+ - bin/console
142
+ - bin/rubocop
143
+ - bin/setup
144
+ - lib/resol.rb
145
+ - lib/resol/builder.rb
146
+ - lib/resol/result.rb
147
+ - lib/resol/service.rb
148
+ - lib/resol/version.rb
149
+ homepage: https://github.com/umbrellio/resol
150
+ licenses:
151
+ - MIT
152
+ metadata: {}
153
+ post_install_message:
154
+ rdoc_options: []
155
+ require_paths:
156
+ - lib
157
+ required_ruby_version: !ruby/object:Gem::Requirement
158
+ requirements:
159
+ - - ">="
160
+ - !ruby/object:Gem::Version
161
+ version: 2.6.0
162
+ required_rubygems_version: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - ">="
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
167
+ requirements: []
168
+ rubygems_version: 3.1.2
169
+ signing_key:
170
+ specification_version: 4
171
+ summary: Gem for creating (any) object patterns
172
+ test_files: []