mini_tools 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +55 -0
- data/lib/mini_tools/command.rb +12 -1
- data/lib/mini_tools/response.rb +17 -3
- data/lib/mini_tools/version.rb +1 -1
- data/spec/command_spec.rb +15 -1
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 38066dd567e600ea6bc076350fc34e04f9f5cab3
|
4
|
+
data.tar.gz: 110de1612b1ca108770492e6cebf6114dcb58088
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 64b66ea1f3c303a4c07b60dfe924390b988e8ef2be07bf23815583550eb11eaeb9fd84ffb3c1feee6b9f0a0496b05e1f821e10f2ce5567a574cd8bff726339e2
|
7
|
+
data.tar.gz: 4da682328539583bb0871b3b2847904971a646b153b7c1e0179c19c0971e15ea977e2dcf4dbbc3b70b3af7e83900526730e6b1ea8ef7e20bb5d7b53d0149a882
|
data/README.md
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
# MiniTools
|
2
|
+
|
3
|
+
[![Build Status](https://travis-ci.org/adamphillips/mini_tools.svg?branch=master)](https://travis-ci.org/adamphillips/mini_tools)
|
4
|
+
|
5
|
+
These are some examples of simple patterns in Ruby. Whilst they are installable
|
6
|
+
using RubyGems, the idea is that they are so simple that you probably just want
|
7
|
+
to roll your own derivatives rather than add another gem dependency to your
|
8
|
+
app.
|
9
|
+
|
10
|
+
Each contains an example of how to use it. You can also look at the tests for
|
11
|
+
each of the patterns.
|
12
|
+
|
13
|
+
### Command
|
14
|
+
|
15
|
+
[view](lib/mini_tools/command.rb)
|
16
|
+
|
17
|
+
A simple command object that can also be used for service objects or interactors.
|
18
|
+
|
19
|
+
### Configuration
|
20
|
+
|
21
|
+
[view](lib/mini_tools/configuration.rb)
|
22
|
+
|
23
|
+
A simple configuration store.
|
24
|
+
|
25
|
+
### ItemFactory
|
26
|
+
|
27
|
+
[view](lib/mini_tools/item_factory.rb)
|
28
|
+
|
29
|
+
A simple item factory.
|
30
|
+
|
31
|
+
### Response
|
32
|
+
|
33
|
+
[view](lib/mini_tools/response.rb)
|
34
|
+
|
35
|
+
Works in conjunction with the Command object and represents the response from a command.
|
36
|
+
|
37
|
+
## Installing from RubyGems
|
38
|
+
|
39
|
+
Install with
|
40
|
+
|
41
|
+
```
|
42
|
+
gem install mini_tools
|
43
|
+
```
|
44
|
+
|
45
|
+
or if you are using bundler, add it to your Gemfile
|
46
|
+
|
47
|
+
```
|
48
|
+
gem 'mini_tools'
|
49
|
+
```
|
50
|
+
|
51
|
+
then run
|
52
|
+
|
53
|
+
```
|
54
|
+
bundle
|
55
|
+
```
|
data/lib/mini_tools/command.rb
CHANGED
@@ -5,6 +5,8 @@ module MiniTools
|
|
5
5
|
#
|
6
6
|
# ```
|
7
7
|
# class SimpleCommand
|
8
|
+
# include MiniTools::Command
|
9
|
+
#
|
8
10
|
# def call value
|
9
11
|
# yield response :success, 'It worked' if value == true
|
10
12
|
# yield response :failure, 'It failed' if value == false
|
@@ -17,10 +19,19 @@ module MiniTools
|
|
17
19
|
# SimpleCommand.new.call some_value do |response|
|
18
20
|
# response.on(:success) ->(message) { puts 'Successful'; puts message }
|
19
21
|
# response.on(:failure) ->(message) { puts 'Unsuccessful'; puts message }
|
22
|
+
# response.else ->(message) { puts 'Unknown response type'; puts message }
|
20
23
|
# end
|
21
24
|
module Command
|
22
25
|
def response result, *args
|
23
|
-
Response.new(result, *args)
|
26
|
+
Response.new(self, result, *args)
|
27
|
+
end
|
28
|
+
|
29
|
+
def handled?
|
30
|
+
@handled
|
31
|
+
end
|
32
|
+
|
33
|
+
def handled= result
|
34
|
+
@handled = result
|
24
35
|
end
|
25
36
|
end
|
26
37
|
end
|
data/lib/mini_tools/response.rb
CHANGED
@@ -2,13 +2,27 @@ module MiniTools
|
|
2
2
|
class Response
|
3
3
|
attr_reader :result, :args
|
4
4
|
|
5
|
-
def initialize result, *args
|
5
|
+
def initialize command, result, *args
|
6
|
+
@command = command
|
6
7
|
@result = result
|
7
8
|
@args = args
|
8
9
|
end
|
9
10
|
|
10
|
-
def on *outcome
|
11
|
-
|
11
|
+
def on *outcome, &block
|
12
|
+
handle_response(&block) if outcome.include?(result)
|
13
|
+
end
|
14
|
+
|
15
|
+
def else &block
|
16
|
+
handle_response(&block) if !command.handled?
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
attr_reader :command
|
22
|
+
|
23
|
+
def handle_response
|
24
|
+
command.handled = true
|
25
|
+
yield(*args)
|
12
26
|
end
|
13
27
|
end
|
14
28
|
end
|
data/lib/mini_tools/version.rb
CHANGED
data/spec/command_spec.rb
CHANGED
@@ -9,7 +9,8 @@ describe MiniTools::Command do
|
|
9
9
|
def call param
|
10
10
|
yield response :success, 'OK' and return if param == 1
|
11
11
|
yield response :error, 'Fail' and return if param == 0
|
12
|
-
yield response :unknown, 'Other' if
|
12
|
+
yield response :unknown, 'Other' and return if param == 2
|
13
|
+
yield response :unhandled, 'Unhandled'
|
13
14
|
end
|
14
15
|
end
|
15
16
|
|
@@ -40,6 +41,14 @@ describe MiniTools::Command do
|
|
40
41
|
end
|
41
42
|
end
|
42
43
|
|
44
|
+
describe 'when the response is not explicitly handled' do
|
45
|
+
it 'should be handled by the else response' do
|
46
|
+
call_with 'whatever'
|
47
|
+
assert_equal 'else', @state
|
48
|
+
assert_equal 'Unhandled', @message
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
43
52
|
describe 'when handling multiple responses with the same block' do
|
44
53
|
it 'should work with the first case' do
|
45
54
|
subject.call 0 do |response|
|
@@ -92,6 +101,11 @@ describe MiniTools::Command do
|
|
92
101
|
@state = 'unknown'
|
93
102
|
@message = m
|
94
103
|
end
|
104
|
+
|
105
|
+
response.else do |m|
|
106
|
+
@state = 'else'
|
107
|
+
@message = m
|
108
|
+
end
|
95
109
|
end
|
96
110
|
end
|
97
111
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mini_tools
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Adam Phillips
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2018-01-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: minitest
|
@@ -45,6 +45,7 @@ executables: []
|
|
45
45
|
extensions: []
|
46
46
|
extra_rdoc_files: []
|
47
47
|
files:
|
48
|
+
- README.md
|
48
49
|
- lib/mini_tools.rb
|
49
50
|
- lib/mini_tools/command.rb
|
50
51
|
- lib/mini_tools/configuration.rb
|
@@ -75,7 +76,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
75
76
|
version: '0'
|
76
77
|
requirements: []
|
77
78
|
rubyforge_project:
|
78
|
-
rubygems_version: 2.
|
79
|
+
rubygems_version: 2.6.13
|
79
80
|
signing_key:
|
80
81
|
specification_version: 4
|
81
82
|
summary: A set of simple lightweight tools to demonstrate patterns in Ruby
|