-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 7d2d129
Showing
25 changed files
with
697 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
name: Ruby | ||
|
||
on: [push,pull_request] | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v2 | ||
|
||
- name: Set up Ruby | ||
uses: ruby/setup-ruby@v1 | ||
with: | ||
ruby-version: 2.7.0 | ||
|
||
- name: Bundling | ||
run: | | ||
gem install bundler -v 2.2.3 | ||
bundle install | ||
- name: Linting | ||
run: bin/rubocop | ||
|
||
- name: Testing | ||
run: COVER=1 bin/rspec | ||
|
||
- name: Coveralls | ||
uses: coverallsapp/github-action@master | ||
with: | ||
github-token: ${{ secrets.GITHUB_TOKEN }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/.bundle/ | ||
/.yardoc | ||
/_yardoc/ | ||
/coverage/ | ||
/doc/ | ||
/pkg/ | ||
/spec/reports/ | ||
/tmp/ | ||
|
||
# rspec failure tracking | ||
.rspec_status |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
--format documentation | ||
--color | ||
--require spec_helper |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
AllCops: | ||
TargetRubyVersion: 2.6 | ||
NewCops: enable | ||
|
||
Style/StringLiterals: | ||
Enabled: true | ||
EnforcedStyle: double_quotes | ||
|
||
Style/StringLiteralsInInterpolation: | ||
Enabled: true | ||
EnforcedStyle: double_quotes | ||
|
||
Style/RaiseArgs: | ||
Enabled: false | ||
|
||
Style/Documentation: | ||
Enabled: false | ||
|
||
Layout/LineLength: | ||
Max: 100 | ||
|
||
Layout/FirstArrayElementIndentation: | ||
EnforcedStyle: consistent |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# frozen_string_literal: true | ||
|
||
source "https://rubygems.org" | ||
|
||
gemspec |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
PATH | ||
remote: . | ||
specs: | ||
resol (0.1.0) | ||
smart_initializer | ||
uber | ||
|
||
GEM | ||
remote: https://rubygems.org/ | ||
specs: | ||
ast (2.4.2) | ||
coderay (1.1.3) | ||
diff-lcs (1.4.4) | ||
docile (1.3.5) | ||
method_source (1.0.0) | ||
parallel (1.20.1) | ||
parser (3.0.1.0) | ||
ast (~> 2.4.1) | ||
pry (0.14.0) | ||
coderay (~> 1.1) | ||
method_source (~> 1.0) | ||
qonfig (0.25.0) | ||
rainbow (3.0.0) | ||
rake (13.0.3) | ||
regexp_parser (2.1.1) | ||
rexml (3.2.5) | ||
rspec (3.10.0) | ||
rspec-core (~> 3.10.0) | ||
rspec-expectations (~> 3.10.0) | ||
rspec-mocks (~> 3.10.0) | ||
rspec-core (3.10.1) | ||
rspec-support (~> 3.10.0) | ||
rspec-expectations (3.10.1) | ||
diff-lcs (>= 1.2.0, < 2.0) | ||
rspec-support (~> 3.10.0) | ||
rspec-mocks (3.10.2) | ||
diff-lcs (>= 1.2.0, < 2.0) | ||
rspec-support (~> 3.10.0) | ||
rspec-support (3.10.2) | ||
rubocop (1.12.1) | ||
parallel (~> 1.10) | ||
parser (>= 3.0.0.0) | ||
rainbow (>= 2.2.2, < 4.0) | ||
regexp_parser (>= 1.8, < 3.0) | ||
rexml | ||
rubocop-ast (>= 1.2.0, < 2.0) | ||
ruby-progressbar (~> 1.7) | ||
unicode-display_width (>= 1.4.0, < 3.0) | ||
rubocop-ast (1.4.1) | ||
parser (>= 2.7.1.5) | ||
ruby-progressbar (1.11.0) | ||
simplecov (0.21.2) | ||
docile (~> 1.1) | ||
simplecov-html (~> 0.11) | ||
simplecov_json_formatter (~> 0.1) | ||
simplecov-html (0.12.3) | ||
simplecov-lcov (0.8.0) | ||
simplecov_json_formatter (0.1.2) | ||
smart_engine (0.11.0) | ||
smart_initializer (0.5.0) | ||
qonfig (~> 0.24) | ||
smart_engine (~> 0.11) | ||
smart_types (~> 0.4) | ||
smart_types (0.4.0) | ||
smart_engine (~> 0.11) | ||
uber (0.1.0) | ||
unicode-display_width (2.0.0) | ||
|
||
PLATFORMS | ||
x86_64-darwin-19 | ||
|
||
DEPENDENCIES | ||
pry | ||
rake | ||
resol! | ||
rspec | ||
rubocop | ||
simplecov | ||
simplecov-lcov | ||
|
||
BUNDLED WITH | ||
2.2.3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2021 appuser | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
# Resol | ||
|
||
 | ||
[](https://coveralls.io/github/umbrellio/resol?branch=master) | ||
|
||
Ruby Gem for creating simple service objects and other any object ruby patterns. | ||
|
||
## Installation | ||
|
||
Add this line to your application's Gemfile: | ||
|
||
```ruby | ||
gem 'resol' | ||
``` | ||
|
||
And then execute: | ||
|
||
$ bundle install | ||
|
||
Or install it yourself as: | ||
|
||
$ gem install resol | ||
|
||
## Usage | ||
|
||
### Sample usage | ||
|
||
```ruby | ||
class FindUser < Resol::Service | ||
param :id | ||
|
||
def call | ||
user = User.find(id) | ||
if user | ||
success!(user) | ||
else | ||
fail!(:not_found) | ||
end | ||
end | ||
end | ||
|
||
# when user exists | ||
FindUser.call(1) # => Resol::Success(<user object>) | ||
FindUser.call!(1) # => <user object> | ||
|
||
# when user doesn't exist | ||
FindUser.call(100) # => Resol::Failure(:not_found) | ||
FindUser.call!(100) # => raised FindUser::Failure: :not_found | ||
``` | ||
|
||
So method `.call` always returns the [result object](#result-objects) | ||
and method `.call!` returns a value or throws an error (in case of `fail!` has been called). | ||
|
||
#### Params defining | ||
|
||
All incoming params and options can be defined using a [smart_initializer](https://github.com/smart-rb/smart_initializer) gem interface. | ||
|
||
#### Return a result | ||
|
||
**Note** – calling `success!`/`fail!` methods interrupts `call` method execution. | ||
|
||
- `success!(value)` – finish with a success value | ||
- `fail!(code, data = nil)` – fail with any error code and optional data | ||
|
||
### Result objects | ||
|
||
Methods: | ||
|
||
- `success?` – returns `true` for success result and `false` for failure result | ||
- `failure?` – returns `true` for failure result and `false` for success result | ||
- `value!` – unwraps a result object, returns the value for success result, and throws an error for failure result | ||
- `value_or(other_value, &block)` – returns a value for success result or `other_value` for failure result (either calls `block` in case it given) | ||
|
||
## Development | ||
|
||
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/rspec` to run the tests. | ||
You can also run `bin/console` for an interactive prompt that will allow you to experiment. | ||
|
||
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). | ||
|
||
## Contributing | ||
|
||
Bug reports and pull requests are welcome on GitHub at https://github.com/umbrellio/resol. | ||
|
||
## License | ||
|
||
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT). | ||
|
||
## Authors | ||
|
||
Created by [Aleksei Bespalov](https://github.com/nulldef). | ||
|
||
<a href="https://github.com/umbrellio/"> | ||
<img style="float: left;" src="https://umbrellio.github.io/Umbrellio/supported_by_umbrellio.svg" alt="Supported by Umbrellio" width="439" height="72"> | ||
</a> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# frozen_string_literal: true | ||
|
||
require "bundler/gem_tasks" | ||
require "rspec/core/rake_task" | ||
|
||
RSpec::Core::RakeTask.new(:spec) | ||
|
||
require "rubocop/rake_task" | ||
|
||
RuboCop::RakeTask.new | ||
|
||
task default: %i[spec rubocop] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#!/usr/bin/env ruby | ||
# frozen_string_literal: true | ||
|
||
require "bundler/setup" | ||
require "resol" | ||
require "pry" | ||
|
||
Pry.start |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#!/usr/bin/env bash | ||
set -euo pipefail | ||
IFS=$'\n\t' | ||
set -vx | ||
|
||
bundle exec rspec |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#!/usr/bin/env bash | ||
set -euo pipefail | ||
IFS=$'\n\t' | ||
set -vx | ||
|
||
bundle exec rubocop |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#!/usr/bin/env bash | ||
set -euo pipefail | ||
IFS=$'\n\t' | ||
set -vx | ||
|
||
bundle install |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# frozen_string_literal: true | ||
|
||
require "uber/builder" | ||
require "smart_core/initializer" | ||
|
||
require_relative "resol/version" | ||
require_relative "resol/service" | ||
|
||
module Resol | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# frozen_string_literal: true | ||
|
||
module Resol | ||
module Builder | ||
def self.included(base) | ||
base.include(Uber::Builder) | ||
base.extend(ClassMethods) | ||
end | ||
|
||
module ClassMethods | ||
def build_klass(*args, **kwargs) | ||
klass = self | ||
|
||
loop do | ||
new_klass = klass.build!(klass, *args, **kwargs) | ||
break if new_klass == klass | ||
|
||
klass = new_klass | ||
end | ||
|
||
klass | ||
end | ||
|
||
def build(*args) | ||
build_klass(*args).new(*args) | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.