simple_operation 0.1.2 → 1.0.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 +4 -4
- data/README.md +41 -2
- data/lib/simple_operation/version.rb +1 -1
- data/lib/simple_operation.rb +16 -7
- data/test/simple_operation_test.rb +20 -1
- metadata +3 -4
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 32d773cfd369dfe0f15ab8ee90b2f73b66dec5e3
|
|
4
|
+
data.tar.gz: a20f16a09077c99747b61f99961e937815e637e4
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: e2559b6cae03f2b3de34361c604e4cce3b769b20836473471266528be4503462a4f3078284e6eaf2d68fe24396a1332c45dc36ba0c22b34071e99fc245232e40
|
|
7
|
+
data.tar.gz: 427d88c82e640db9c389249e00be447d844ac525039c971213420b30f2b20324897ce8aacc2a872e2e08339a5f9eb39e39249cd864d6736727a1453966d0a015
|
data/README.md
CHANGED
|
@@ -25,15 +25,17 @@ Example usage for SimpleOperation is creating user:
|
|
|
25
25
|
```ruby
|
|
26
26
|
class CreateUser < SimpleOperation.new(:login, :password)
|
|
27
27
|
|
|
28
|
+
InvalidUser = Class.new(RuntimeError)
|
|
29
|
+
|
|
28
30
|
def call
|
|
29
31
|
user = User.new(login, password)
|
|
30
|
-
|
|
32
|
+
raise InvalidUser unless valid_user?(user)
|
|
31
33
|
UserRepository.persist(user)
|
|
32
34
|
user
|
|
33
35
|
end
|
|
34
36
|
|
|
35
37
|
private
|
|
36
|
-
def
|
|
38
|
+
def valid_user?(user)
|
|
37
39
|
!UserRepository.fetch_all_logins.include?(user.login)
|
|
38
40
|
end
|
|
39
41
|
|
|
@@ -49,6 +51,43 @@ CreateUser.new('Grzegorz', 'arnvald.to@gmail.com').()
|
|
|
49
51
|
CreateUser.new('Grzegorz', 'arnvald.to@gmail.com').perform
|
|
50
52
|
```
|
|
51
53
|
|
|
54
|
+
If you need output that consists of more than one field, instead of returning multiple values,
|
|
55
|
+
you can use `result` methods:
|
|
56
|
+
|
|
57
|
+
```ruby
|
|
58
|
+
class CreateCompanyAndUser < SimpleOperation(:name, :login, :password)
|
|
59
|
+
# result on class-level defines output structure
|
|
60
|
+
result :company, :user
|
|
61
|
+
|
|
62
|
+
def call
|
|
63
|
+
# result in call method returns defined structure
|
|
64
|
+
result create_company(name), create_user(login, password)
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
There's a sugar syntax for creating classes:
|
|
70
|
+
|
|
71
|
+
```ruby
|
|
72
|
+
CreateUser = SimpleOperation.new(:login, :password) do
|
|
73
|
+
def call
|
|
74
|
+
...
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
or
|
|
80
|
+
|
|
81
|
+
```ruby
|
|
82
|
+
require 'simple_operation/ext'
|
|
83
|
+
|
|
84
|
+
CreateUser = SimpleOperation(:login, :password)
|
|
85
|
+
def call
|
|
86
|
+
...
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
```
|
|
90
|
+
|
|
52
91
|
|
|
53
92
|
## Contributing
|
|
54
93
|
|
data/lib/simple_operation.rb
CHANGED
|
@@ -2,9 +2,11 @@ require_relative './simple_operation/version'
|
|
|
2
2
|
|
|
3
3
|
class SimpleOperation
|
|
4
4
|
|
|
5
|
-
def self.new(*args)
|
|
5
|
+
def self.new(*args, &block)
|
|
6
6
|
args_list_with_nils = args.empty? ? '' : "#{args.join('=nil,')}=nil"
|
|
7
7
|
Class.new do
|
|
8
|
+
class_eval(&block) if block_given?
|
|
9
|
+
|
|
8
10
|
class_eval <<-code
|
|
9
11
|
def self.call #{args_list_with_nils}
|
|
10
12
|
new(#{args.join(',')}).call
|
|
@@ -13,15 +15,22 @@ class SimpleOperation
|
|
|
13
15
|
def initialize #{args_list_with_nils}
|
|
14
16
|
#{args.map { |arg| "@#{arg}= #{arg}" }.join(';') }
|
|
15
17
|
end
|
|
18
|
+
code
|
|
16
19
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
+
def perform
|
|
21
|
+
call
|
|
22
|
+
end
|
|
20
23
|
|
|
21
|
-
|
|
22
|
-
|
|
24
|
+
private
|
|
25
|
+
attr_reader(*args)
|
|
23
26
|
|
|
24
|
-
|
|
27
|
+
def self.result(*args)
|
|
28
|
+
@result_class = Struct.new(*args)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def result(*args)
|
|
32
|
+
self.class.instance_variable_get(:@result_class).new(*args)
|
|
33
|
+
end
|
|
25
34
|
end
|
|
26
35
|
end
|
|
27
36
|
|
|
@@ -51,6 +51,13 @@ class SimpleOperationTest < Minitest::Test
|
|
|
51
51
|
refute klass.('Grzegorz')
|
|
52
52
|
end
|
|
53
53
|
|
|
54
|
+
def test_returns_result
|
|
55
|
+
assert result_klass.('').is_a? Struct
|
|
56
|
+
assert result_klass.('').respond_to? :found
|
|
57
|
+
assert result_klass.('Arnvald').found
|
|
58
|
+
refute result_klass.('Grzegorz').found
|
|
59
|
+
end
|
|
60
|
+
|
|
54
61
|
|
|
55
62
|
def object
|
|
56
63
|
klass.new('Arnvald')
|
|
@@ -60,10 +67,22 @@ class SimpleOperationTest < Minitest::Test
|
|
|
60
67
|
FindUser
|
|
61
68
|
end
|
|
62
69
|
|
|
63
|
-
|
|
70
|
+
def result_klass
|
|
71
|
+
FindUserResult
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
FindUser = SimpleOperation.new(:login) do
|
|
64
75
|
def call
|
|
65
76
|
login == 'Arnvald'
|
|
66
77
|
end
|
|
67
78
|
end
|
|
68
79
|
|
|
80
|
+
class FindUserResult < SimpleOperation.new(:login)
|
|
81
|
+
result :found
|
|
82
|
+
|
|
83
|
+
def call
|
|
84
|
+
result(login == 'Arnvald')
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
|
|
69
88
|
end
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: simple_operation
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 1.0.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Grzegorz Witek
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date:
|
|
11
|
+
date: 2016-02-22 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: bundler
|
|
@@ -77,11 +77,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
77
77
|
version: '0'
|
|
78
78
|
requirements: []
|
|
79
79
|
rubyforge_project:
|
|
80
|
-
rubygems_version: 2.
|
|
80
|
+
rubygems_version: 2.5.1
|
|
81
81
|
signing_key:
|
|
82
82
|
specification_version: 4
|
|
83
83
|
summary: Create simple service object
|
|
84
84
|
test_files:
|
|
85
85
|
- test/simple_operation_ext_test.rb
|
|
86
86
|
- test/simple_operation_test.rb
|
|
87
|
-
has_rdoc:
|