yyks_simple_command 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +16 -0
- data/.rspec +1 -0
- data/.rubocop.yml +14 -0
- data/.travis.yml +4 -0
- data/Gemfile +9 -0
- data/LICENSE.txt +22 -0
- data/README.md +120 -0
- data/Rakefile +6 -0
- data/lib/yyks_simple_command/errors.rb +23 -0
- data/lib/yyks_simple_command/version.rb +3 -0
- data/lib/yyks_simple_command.rb +43 -0
- data/spec/factories/missed_call_command.rb +7 -0
- data/spec/factories/success_command.rb +11 -0
- data/spec/spec_helper.rb +13 -0
- data/spec/yyks_simple_command/errors_spec.rb +58 -0
- data/spec/yyks_simple_command_spec.rb +104 -0
- data/yyks_simple_command.gemspec +24 -0
- metadata +94 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 5908aee2a7893aca88b4424b777548d47464dd8b7f9f47d902b743917d63f509
|
4
|
+
data.tar.gz: 169f8262fdc7531cf8e19a2e5eff2cb180452d537263d38f017c786f32789905
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 020005e1308bc8b4d048c7c58ac8b26dba4ef8eaa9dc90d95456a5405d69d455d32d8d650ccf1a94e7c5b20a06135d9db5f42f3a03347f24a585950a659b608e
|
7
|
+
data.tar.gz: 32b42c1752d64255f4668f5699aa093fb871310912a0af2a960059db1759ab22fd2729ad0bbbd08ce6650e8b30c7e13df48e62c2480d55158bb50b9b1e0423cc
|
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/.rubocop.yml
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
# This is the configuration used to check the rubocop source code.
|
2
|
+
|
3
|
+
# inherit_from: .rubocop_todo.yml
|
4
|
+
|
5
|
+
# AllCops:
|
6
|
+
# Exclude:
|
7
|
+
# - 'yyks_simple_command.gemspec'
|
8
|
+
# - 'spec/factories/**/*'
|
9
|
+
|
10
|
+
# Style/Encoding:
|
11
|
+
# Enabled: when_needed
|
12
|
+
|
13
|
+
Style/Documentation:
|
14
|
+
Enabled: false
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Nebulab S.r.l. (http://nebulab.it)
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,120 @@
|
|
1
|
+
[![Code Climate](https://codeclimate.com/github/nebulab/yyks_simple_command/badges/gpa.svg)](https://codeclimate.com/github/nebulab/yyks_simple_command)
|
2
|
+
[ ![Codeship Status for nebulab/yyks_simple_command](https://app.codeship.com/projects/45ce7790-8daf-0132-1412-669677a474c3/status?branch=master)](https://app.codeship.com/projects/60741)
|
3
|
+
|
4
|
+
# SimpleCommand
|
5
|
+
|
6
|
+
A simple, standardized way to build and use _Service Objects_ (aka _Commands_) in Ruby
|
7
|
+
|
8
|
+
## Requirements
|
9
|
+
|
10
|
+
* Ruby 2.0+
|
11
|
+
|
12
|
+
## Installation
|
13
|
+
|
14
|
+
Add this line to your application's Gemfile:
|
15
|
+
|
16
|
+
```ruby
|
17
|
+
gem 'yyks_simple_command'
|
18
|
+
```
|
19
|
+
|
20
|
+
And then execute:
|
21
|
+
|
22
|
+
$ bundle
|
23
|
+
|
24
|
+
Or install it yourself as:
|
25
|
+
|
26
|
+
$ gem install yyks_simple_command
|
27
|
+
|
28
|
+
## Usage
|
29
|
+
|
30
|
+
Here's a basic example of a command that authenticates a user
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
# define a command class
|
34
|
+
class AuthenticateUser
|
35
|
+
# put SimpleCommand before the class' ancestors chain
|
36
|
+
prepend SimpleCommand
|
37
|
+
|
38
|
+
# optional, initialize the command with some arguments
|
39
|
+
def initialize(email, password)
|
40
|
+
@email = email
|
41
|
+
@password = password
|
42
|
+
end
|
43
|
+
|
44
|
+
# mandatory: define a #call method. its return value will be available
|
45
|
+
# through #result
|
46
|
+
def call
|
47
|
+
if user = User.authenticate(@email, @password)
|
48
|
+
return user
|
49
|
+
else
|
50
|
+
errors.add(:authentication, I18n.t "authenticate_user.failure")
|
51
|
+
end
|
52
|
+
nil
|
53
|
+
end
|
54
|
+
end
|
55
|
+
```
|
56
|
+
|
57
|
+
Then, in your controller:
|
58
|
+
|
59
|
+
```ruby
|
60
|
+
class SessionsController < ApplicationController
|
61
|
+
def create
|
62
|
+
# initialize and execute the command
|
63
|
+
# NOTE: `.call` is a shortcut for `.new(args).call)`
|
64
|
+
command = AuthenticateUser.call(session_params[:email], session_params[:password])
|
65
|
+
|
66
|
+
# check command outcome
|
67
|
+
if command.success?
|
68
|
+
# command#result will contain the user instance, if found
|
69
|
+
session[:user_token] = command.result.secret_token
|
70
|
+
redirect_to root_path
|
71
|
+
else
|
72
|
+
flash.now[:alert] = t(command.errors[:authentication])
|
73
|
+
render :new
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
private
|
78
|
+
|
79
|
+
def session_params
|
80
|
+
params.require(:session).permit(:email, :password)
|
81
|
+
end
|
82
|
+
end
|
83
|
+
```
|
84
|
+
|
85
|
+
## Test with Rspec
|
86
|
+
Make the spec file `spec/commands/authenticate_user_spec.rb` like:
|
87
|
+
|
88
|
+
```ruby
|
89
|
+
describe AuthenticateUser do
|
90
|
+
subject(:context) { described_class.call(username, password) }
|
91
|
+
|
92
|
+
describe '.call' do
|
93
|
+
context 'when the context is successful' do
|
94
|
+
let(:username) { 'correct_user' }
|
95
|
+
let(:password) { 'correct_password' }
|
96
|
+
|
97
|
+
it 'succeeds' do
|
98
|
+
expect(context).to be_success
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
context 'when the context is not successful' do
|
103
|
+
let(:username) { 'wrong_user' }
|
104
|
+
let(:password) { 'wrong_password' }
|
105
|
+
|
106
|
+
it 'fails' do
|
107
|
+
expect(context).to be_failure
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
112
|
+
```
|
113
|
+
|
114
|
+
## Contributing
|
115
|
+
|
116
|
+
1. Fork it ( https://github.com/nebulab/yyks_simple_command/fork )
|
117
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
118
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
119
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
120
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
module SimpleCommand
|
2
|
+
class NotImplementedError < ::StandardError; end
|
3
|
+
|
4
|
+
class Errors < Hash
|
5
|
+
def add(key, value, _opts = {})
|
6
|
+
self[key] ||= []
|
7
|
+
self[key] << value
|
8
|
+
self[key].uniq!
|
9
|
+
end
|
10
|
+
|
11
|
+
def add_multiple_errors(errors_hash)
|
12
|
+
errors_hash.each do |key, values|
|
13
|
+
values.each { |value| add key, value }
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def each
|
18
|
+
each_key do |field|
|
19
|
+
self[field].each { |message| yield field, message }
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'yyks_simple_command/version'
|
2
|
+
require 'yyks_simple_command/errors'
|
3
|
+
|
4
|
+
module SimpleCommand
|
5
|
+
attr_reader :result
|
6
|
+
|
7
|
+
module ClassMethods
|
8
|
+
def call(*args)
|
9
|
+
new(*args).call
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.prepended(base)
|
14
|
+
base.extend ClassMethods
|
15
|
+
end
|
16
|
+
|
17
|
+
def call
|
18
|
+
fail NotImplementedError unless defined?(super)
|
19
|
+
|
20
|
+
@called = true
|
21
|
+
@result = super
|
22
|
+
|
23
|
+
self
|
24
|
+
end
|
25
|
+
|
26
|
+
def success?
|
27
|
+
called? && !failure?
|
28
|
+
end
|
29
|
+
|
30
|
+
def failure?
|
31
|
+
called? && errors.any?
|
32
|
+
end
|
33
|
+
|
34
|
+
def errors
|
35
|
+
@errors ||= Errors.new
|
36
|
+
end
|
37
|
+
|
38
|
+
private
|
39
|
+
|
40
|
+
def called?
|
41
|
+
@called ||= false
|
42
|
+
end
|
43
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'simplecov'
|
2
|
+
|
3
|
+
SimpleCov.start do
|
4
|
+
add_filter '/spec/'
|
5
|
+
end
|
6
|
+
|
7
|
+
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
|
8
|
+
|
9
|
+
require 'yyks_yyks_simple_command'
|
10
|
+
|
11
|
+
Dir[File.join(File.dirname(__FILE__), 'factories', '**/*.rb')].each do |factory|
|
12
|
+
require factory
|
13
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe SimpleCommand::Errors do
|
4
|
+
let(:errors) { SimpleCommand::Errors.new }
|
5
|
+
|
6
|
+
describe '#add' do
|
7
|
+
before do
|
8
|
+
errors.add :some_error, 'some error description'
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'adds the error' do
|
12
|
+
expect(errors[:some_error]).to eq(['some error description'])
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'adds the same error only once' do
|
16
|
+
errors.add :some_error, 'some error description'
|
17
|
+
expect(errors[:some_error]).to eq(['some error description'])
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
describe '#add_multiple_errors' do
|
22
|
+
let(:errors_list) do
|
23
|
+
{
|
24
|
+
some_error: ['some error description'],
|
25
|
+
another_error: ['another error description']
|
26
|
+
}
|
27
|
+
end
|
28
|
+
|
29
|
+
before do
|
30
|
+
errors.add_multiple_errors errors_list
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'populates itself with the added errors' do
|
34
|
+
expect(errors[:some_error]).to eq(errors_list[:some_error])
|
35
|
+
expect(errors[:another_error]).to eq(errors_list[:another_error])
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
describe '#each' do
|
40
|
+
let(:errors_list) do
|
41
|
+
{
|
42
|
+
email: ['taken'],
|
43
|
+
password: ['blank', 'too short']
|
44
|
+
}
|
45
|
+
end
|
46
|
+
|
47
|
+
before { errors.add_multiple_errors(errors_list) }
|
48
|
+
|
49
|
+
it 'yields each message for the same key independently' do
|
50
|
+
expect { |b| errors.each(&b) }.to yield_control.exactly(3).times
|
51
|
+
expect { |b| errors.each(&b) }.to yield_successive_args(
|
52
|
+
[:email, 'taken'],
|
53
|
+
[:password, 'blank'],
|
54
|
+
[:password, 'too short']
|
55
|
+
)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,104 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe SimpleCommand do
|
4
|
+
let(:command) { SuccessCommand.new(2) }
|
5
|
+
|
6
|
+
describe '.call' do
|
7
|
+
before do
|
8
|
+
allow(SuccessCommand).to receive(:new).and_return(command)
|
9
|
+
allow(command).to receive(:call)
|
10
|
+
|
11
|
+
SuccessCommand.call 2
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'initializes the command' do
|
15
|
+
expect(SuccessCommand).to have_received(:new)
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'calls #call method' do
|
19
|
+
expect(command).to have_received(:call)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
describe '#call' do
|
24
|
+
let(:missed_call_command) { MissedCallCommand.new(2) }
|
25
|
+
|
26
|
+
it 'returns the command object' do
|
27
|
+
expect(command.call).to be_a(SuccessCommand)
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'raises an exception if the method is not defined in the command' do
|
31
|
+
expect do
|
32
|
+
missed_call_command.call
|
33
|
+
end.to raise_error(SimpleCommand::NotImplementedError)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
describe '#success?' do
|
38
|
+
it 'is true by default' do
|
39
|
+
expect(command.call.success?).to be_truthy
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'is false if something went wrong' do
|
43
|
+
command.errors.add(:some_error, 'some message')
|
44
|
+
expect(command.call.success?).to be_falsy
|
45
|
+
end
|
46
|
+
|
47
|
+
context 'when call is not called yet' do
|
48
|
+
it 'is false by default' do
|
49
|
+
expect(command.success?).to be_falsy
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
describe '#result' do
|
55
|
+
it 'returns the result of command execution' do
|
56
|
+
expect(command.call.result).to eq(4)
|
57
|
+
end
|
58
|
+
|
59
|
+
context 'when call is not called yet' do
|
60
|
+
it 'returns nil' do
|
61
|
+
expect(command.result).to be_nil
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
describe '#failure?' do
|
67
|
+
it 'is false by default' do
|
68
|
+
expect(command.call.failure?).to be_falsy
|
69
|
+
end
|
70
|
+
|
71
|
+
it 'is true if something went wrong' do
|
72
|
+
command.errors.add(:some_error, 'some message')
|
73
|
+
expect(command.call.failure?).to be_truthy
|
74
|
+
end
|
75
|
+
|
76
|
+
context 'when call is not called yet' do
|
77
|
+
it 'is false by default' do
|
78
|
+
expect(command.failure?).to be_falsy
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
describe '#errors' do
|
84
|
+
it 'returns an SimpleCommand::Errors' do
|
85
|
+
expect(command.errors).to be_a(SimpleCommand::Errors)
|
86
|
+
end
|
87
|
+
|
88
|
+
context 'with no errors' do
|
89
|
+
it 'is empty' do
|
90
|
+
expect(command.errors).to be_empty
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
context 'with errors' do
|
95
|
+
before do
|
96
|
+
command.errors.add(:some_error, 'some message')
|
97
|
+
end
|
98
|
+
|
99
|
+
it 'has a key with error message' do
|
100
|
+
expect(command.errors[:some_error]).to eq(['some message'])
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'yyks_simple_command/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.required_ruby_version = '>= 2.0'
|
8
|
+
s.name = 'yyks_simple_command'
|
9
|
+
s.version = SimpleCommand::VERSION
|
10
|
+
s.authors = ['Andrea Pavoni']
|
11
|
+
s.email = ['andrea.pavoni@gmail.com']
|
12
|
+
s.summary = 'Easy way to build and manage commands (service objects)'
|
13
|
+
s.description = 'Easy way to build and manage commands (service objects)'
|
14
|
+
s.homepage = 'http://github.com/nebulab/yyks_simple_command'
|
15
|
+
s.license = 'MIT'
|
16
|
+
|
17
|
+
s.files = `git ls-files -z`.split("\x0")
|
18
|
+
s.executables = s.files.grep(/^bin\//) { |f| File.basename(f) }
|
19
|
+
s.test_files = s.files.grep(/^(test|spec|features)\//)
|
20
|
+
s.require_paths = ['lib']
|
21
|
+
|
22
|
+
s.add_development_dependency 'rake', '>= 10.0'
|
23
|
+
s.add_development_dependency 'rspec', '>= 3.1'
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: yyks_simple_command
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Andrea Pavoni
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-01-05 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rake
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '10.0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '10.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rspec
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '3.1'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '3.1'
|
41
|
+
description: Easy way to build and manage commands (service objects)
|
42
|
+
email:
|
43
|
+
- andrea.pavoni@gmail.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- ".gitignore"
|
49
|
+
- ".rspec"
|
50
|
+
- ".rubocop.yml"
|
51
|
+
- ".travis.yml"
|
52
|
+
- Gemfile
|
53
|
+
- LICENSE.txt
|
54
|
+
- README.md
|
55
|
+
- Rakefile
|
56
|
+
- lib/yyks_simple_command.rb
|
57
|
+
- lib/yyks_simple_command/errors.rb
|
58
|
+
- lib/yyks_simple_command/version.rb
|
59
|
+
- spec/factories/missed_call_command.rb
|
60
|
+
- spec/factories/success_command.rb
|
61
|
+
- spec/spec_helper.rb
|
62
|
+
- spec/yyks_simple_command/errors_spec.rb
|
63
|
+
- spec/yyks_simple_command_spec.rb
|
64
|
+
- yyks_simple_command.gemspec
|
65
|
+
homepage: http://github.com/nebulab/yyks_simple_command
|
66
|
+
licenses:
|
67
|
+
- MIT
|
68
|
+
metadata: {}
|
69
|
+
post_install_message:
|
70
|
+
rdoc_options: []
|
71
|
+
require_paths:
|
72
|
+
- lib
|
73
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
74
|
+
requirements:
|
75
|
+
- - ">="
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '2.0'
|
78
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
requirements: []
|
84
|
+
rubyforge_project:
|
85
|
+
rubygems_version: 2.7.6
|
86
|
+
signing_key:
|
87
|
+
specification_version: 4
|
88
|
+
summary: Easy way to build and manage commands (service objects)
|
89
|
+
test_files:
|
90
|
+
- spec/factories/missed_call_command.rb
|
91
|
+
- spec/factories/success_command.rb
|
92
|
+
- spec/spec_helper.rb
|
93
|
+
- spec/yyks_simple_command/errors_spec.rb
|
94
|
+
- spec/yyks_simple_command_spec.rb
|