simple_command 0.0.7 → 0.0.8
Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e1e044db947afd477d7b8042fbd679fdffbd8a63
|
4
|
+
data.tar.gz: 021cfb1817dd007895a76184af0db53095c0c9a1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0a88837defef4d2cc6f3c7167e0f891f73dd09ff6378a5719df8772e8a4f1bdd7cd22043ca29b3596d9e53d619c9a714d03c2107a27d3746150c8f3d167d2730
|
7
|
+
data.tar.gz: ea8a66ec330153ba06932837a9795d7931c3741faed743c3ebdaa40e211ffde726a173121a84c6a91c89cca5561308373d04aca9d0a776764ce195d8f75694f2
|
data/README.md
CHANGED
@@ -38,9 +38,9 @@ class AuthenticateUser
|
|
38
38
|
@password = password
|
39
39
|
end
|
40
40
|
|
41
|
-
# mandatory: define a #
|
41
|
+
# mandatory: define a #call method. its return value will be available
|
42
42
|
# through #result
|
43
|
-
def
|
43
|
+
def call
|
44
44
|
if user = User.authenticate(@email, @password)
|
45
45
|
return user
|
46
46
|
else
|
@@ -57,8 +57,8 @@ Then, in your controller:
|
|
57
57
|
class SessionsController < ApplicationController
|
58
58
|
def create
|
59
59
|
# initialize and execute the command
|
60
|
-
# NOTE: `.
|
61
|
-
command = AuthenticateUser.
|
60
|
+
# NOTE: `.call` is a shortcut for `.new(args).call)`
|
61
|
+
command = AuthenticateUser.call(session_params[:user], session_params[:password])
|
62
62
|
|
63
63
|
# check command outcome
|
64
64
|
if command.success?
|
data/lib/simple_command.rb
CHANGED
@@ -5,8 +5,8 @@ module SimpleCommand
|
|
5
5
|
attr_reader :result
|
6
6
|
|
7
7
|
module ClassMethods
|
8
|
-
def
|
9
|
-
new(*args).
|
8
|
+
def call(*args)
|
9
|
+
new(*args).call
|
10
10
|
end
|
11
11
|
end
|
12
12
|
|
@@ -14,21 +14,21 @@ module SimpleCommand
|
|
14
14
|
base.extend ClassMethods
|
15
15
|
end
|
16
16
|
|
17
|
-
def
|
17
|
+
def call
|
18
18
|
fail NotImplementedError unless defined?(super)
|
19
19
|
|
20
|
-
@
|
20
|
+
@called = true
|
21
21
|
@result = super
|
22
22
|
|
23
23
|
self
|
24
24
|
end
|
25
25
|
|
26
26
|
def success?
|
27
|
-
|
27
|
+
called? && !failure?
|
28
28
|
end
|
29
29
|
|
30
30
|
def failure?
|
31
|
-
|
31
|
+
called? && errors.any?
|
32
32
|
end
|
33
33
|
|
34
34
|
def errors
|
@@ -37,7 +37,7 @@ module SimpleCommand
|
|
37
37
|
|
38
38
|
private
|
39
39
|
|
40
|
-
def
|
41
|
-
@
|
40
|
+
def called?
|
41
|
+
@called ||= false
|
42
42
|
end
|
43
43
|
end
|
data/spec/simple_command_spec.rb
CHANGED
@@ -3,48 +3,48 @@ require 'spec_helper'
|
|
3
3
|
describe SimpleCommand do
|
4
4
|
let(:command) { SuccessCommand.new(2) }
|
5
5
|
|
6
|
-
describe '.
|
6
|
+
describe '.call' do
|
7
7
|
before do
|
8
8
|
allow(SuccessCommand).to receive(:new).and_return(command)
|
9
|
-
allow(command).to receive(:
|
9
|
+
allow(command).to receive(:call)
|
10
10
|
|
11
|
-
SuccessCommand.
|
11
|
+
SuccessCommand.call 2
|
12
12
|
end
|
13
13
|
|
14
14
|
it 'initializes the command' do
|
15
15
|
expect(SuccessCommand).to have_received(:new)
|
16
16
|
end
|
17
17
|
|
18
|
-
it 'calls #
|
19
|
-
expect(command).to have_received(:
|
18
|
+
it 'calls #call method' do
|
19
|
+
expect(command).to have_received(:call)
|
20
20
|
end
|
21
21
|
end
|
22
22
|
|
23
|
-
describe '#
|
24
|
-
let(:
|
23
|
+
describe '#call' do
|
24
|
+
let(:missed_call_command) { MissedCallCommand.new(2) }
|
25
25
|
|
26
26
|
it 'returns the command object' do
|
27
|
-
expect(command.
|
27
|
+
expect(command.call).to be_a(SuccessCommand)
|
28
28
|
end
|
29
29
|
|
30
30
|
it 'raises an exception if the method is not defined in the command' do
|
31
31
|
expect do
|
32
|
-
|
32
|
+
missed_call_command.call
|
33
33
|
end.to raise_error(SimpleCommand::NotImplementedError)
|
34
34
|
end
|
35
35
|
end
|
36
36
|
|
37
37
|
describe '#success?' do
|
38
38
|
it 'is true by default' do
|
39
|
-
expect(command.
|
39
|
+
expect(command.call.success?).to be_truthy
|
40
40
|
end
|
41
41
|
|
42
42
|
it 'is false if something went wrong' do
|
43
43
|
command.errors.add_error(:some_error, 'some message')
|
44
|
-
expect(command.
|
44
|
+
expect(command.call.success?).to be_falsy
|
45
45
|
end
|
46
46
|
|
47
|
-
context 'when
|
47
|
+
context 'when call is not called yet' do
|
48
48
|
it 'is false by default' do
|
49
49
|
expect(command.success?).to be_falsy
|
50
50
|
end
|
@@ -53,10 +53,10 @@ describe SimpleCommand do
|
|
53
53
|
|
54
54
|
describe '#result' do
|
55
55
|
it 'returns the result of command execution' do
|
56
|
-
expect(command.
|
56
|
+
expect(command.call.result).to eq(4)
|
57
57
|
end
|
58
58
|
|
59
|
-
context 'when
|
59
|
+
context 'when call is not called yet' do
|
60
60
|
it 'returns nil' do
|
61
61
|
expect(command.result).to be_nil
|
62
62
|
end
|
@@ -65,15 +65,15 @@ describe SimpleCommand do
|
|
65
65
|
|
66
66
|
describe '#failure?' do
|
67
67
|
it 'is false by default' do
|
68
|
-
expect(command.
|
68
|
+
expect(command.call.failure?).to be_falsy
|
69
69
|
end
|
70
70
|
|
71
71
|
it 'is true if something went wrong' do
|
72
72
|
command.errors.add_error(:some_error, 'some message')
|
73
|
-
expect(command.
|
73
|
+
expect(command.call.failure?).to be_truthy
|
74
74
|
end
|
75
75
|
|
76
|
-
context 'when
|
76
|
+
context 'when call is not called yet' do
|
77
77
|
it 'is false by default' do
|
78
78
|
expect(command.failure?).to be_falsy
|
79
79
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simple_command
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrea Pavoni
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-01-
|
11
|
+
date: 2015-01-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -71,7 +71,7 @@ files:
|
|
71
71
|
- lib/simple_command/errors.rb
|
72
72
|
- lib/simple_command/version.rb
|
73
73
|
- simple_command.gemspec
|
74
|
-
- spec/factories/
|
74
|
+
- spec/factories/missed_call_command.rb
|
75
75
|
- spec/factories/success_command.rb
|
76
76
|
- spec/simple_command/errors_spec.rb
|
77
77
|
- spec/simple_command_spec.rb
|
@@ -101,7 +101,7 @@ signing_key:
|
|
101
101
|
specification_version: 4
|
102
102
|
summary: Easy way to build and manage commands (service objects)
|
103
103
|
test_files:
|
104
|
-
- spec/factories/
|
104
|
+
- spec/factories/missed_call_command.rb
|
105
105
|
- spec/factories/success_command.rb
|
106
106
|
- spec/simple_command/errors_spec.rb
|
107
107
|
- spec/simple_command_spec.rb
|