commandos 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +55 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/commandos.gemspec +28 -0
- data/examples/command_and_handler.md +42 -0
- data/examples/command_registry_and_dispatcher.md +56 -0
- data/lib/commandos/command.rb +23 -0
- data/lib/commandos/dispatcher.rb +29 -0
- data/lib/commandos/registry.rb +52 -0
- data/lib/commandos/version.rb +3 -0
- data/lib/commandos.rb +5 -0
- metadata +133 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 04d5fabaf654bfc5af0033d44699f2866ad9b106
|
4
|
+
data.tar.gz: 30695bc925651a7ad32901473b83e91727a8aab3
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c1b0d7836a60793e313d545940e906c9b34f1456c242c2a7f30e21921f3811a4b5119971fba9165b4482f7069cb7bc8a4a92614c2603bee02a9aee914ad9ca19
|
7
|
+
data.tar.gz: 9118577a2ac712a0df402ca6b56d259e1a1104763a87c30d819c8998deb92cc439e725fa608c82dc667ad335767695c781a6322a0035177b255a60cb29fd88e2
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 Michael Elkins
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
# COMMANDOS
|
2
|
+
|
3
|
+
Commandos is a project designed to help any ruby app use the command pattern to
|
4
|
+
change state.
|
5
|
+
|
6
|
+
## PREREQUISITES
|
7
|
+
* ruby-2.3.1
|
8
|
+
|
9
|
+
## DEPENDENCIES
|
10
|
+
* ActiveModel::Validations
|
11
|
+
|
12
|
+
## INSTALLATION
|
13
|
+
|
14
|
+
In your Gemfile
|
15
|
+
|
16
|
+
```
|
17
|
+
gem 'Commandos'
|
18
|
+
```
|
19
|
+
|
20
|
+
## EXAMPLES
|
21
|
+
|
22
|
+
1. [Command & Handler](./examples/command_and_handler.md)
|
23
|
+
2. [Registry & Dispatcher](./examples/command_registry_and_dispatcher.md)
|
24
|
+
|
25
|
+
## PLUGINS
|
26
|
+
|
27
|
+
Commandos uses a plugin system similar to
|
28
|
+
[sequel](https://github.com/jeremyevans/sequel) or
|
29
|
+
[roda](https://github.com/jeremyevans/roda) to extend the behavior of your
|
30
|
+
command objects.
|
31
|
+
|
32
|
+
### EXAMPLE OF A CUSTOM PLUGIN
|
33
|
+
|
34
|
+
```ruby
|
35
|
+
module Commandos
|
36
|
+
module Plugins
|
37
|
+
module ExamplePlugin
|
38
|
+
module ClassMethods
|
39
|
+
end
|
40
|
+
|
41
|
+
module InstanceMethods
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
```
|
47
|
+
|
48
|
+
Any method in the `ClassMethods` module will be injected into the `IAmACommand`
|
49
|
+
class. Any method in the `InstanceMethods` module will be injected into
|
50
|
+
instances of `IAmACommand` objects.
|
51
|
+
|
52
|
+
|
53
|
+
### Commandos::Plugins::ActiveModelPlugin
|
54
|
+
|
55
|
+
This plugin is used to give commands active model like validation behavior.
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "commando"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/commandos.gemspec
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'commandos/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "commandos"
|
8
|
+
spec.version = Commandos::VERSION
|
9
|
+
spec.authors = ["Michael Elkins"]
|
10
|
+
spec.email = ["mike@mdelkins.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Commandos is a library to support the command design pattern}
|
13
|
+
spec.description = %q{Commandos is a library to help build the series of objects needed for the command design pattern. It can help wtih building commands, handlers to execute commands, and command registries.}
|
14
|
+
spec.homepage = "https://github.com/mdelkins/commando"
|
15
|
+
spec.license = "MIT"
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
17
|
+
f.match(%r{^(test|spec|features)/})
|
18
|
+
end
|
19
|
+
spec.bindir = "exe"
|
20
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
21
|
+
spec.require_paths = ["lib"]
|
22
|
+
|
23
|
+
spec.add_development_dependency "bump", "~> 0.5"
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.14"
|
25
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
26
|
+
spec.add_development_dependency "minitest", "~> 5.0"
|
27
|
+
spec.add_development_dependency "minitest-color", "~> 0.0"
|
28
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
# COMMAND & HANDLER
|
2
|
+
|
3
|
+
```ruby
|
4
|
+
class RegisterAccountCommand < Commandos::IAmACommand
|
5
|
+
values do
|
6
|
+
string :identity
|
7
|
+
string :password
|
8
|
+
string :password_confirmation
|
9
|
+
end
|
10
|
+
|
11
|
+
validates_presence_of \
|
12
|
+
:identity,
|
13
|
+
:password,
|
14
|
+
:password_confirmation
|
15
|
+
|
16
|
+
validate \
|
17
|
+
:password_confirmation!
|
18
|
+
|
19
|
+
private
|
20
|
+
def password_confirmation!
|
21
|
+
unless password_confirmation == password
|
22
|
+
errors.add :password_confirmation, "doesn't match password"
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
class RegisterAccountHandler
|
28
|
+
def initialize(command)
|
29
|
+
@command = command
|
30
|
+
end
|
31
|
+
|
32
|
+
def call
|
33
|
+
return command unless command.valid?
|
34
|
+
account_to_register = Account.new command.attributes
|
35
|
+
account_to_register.save!
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
command = RegisterAccountCommand.new identity: 'test@test.test', password: 'Password123123!@#', password_confirmation: 'Password123123!@#'
|
40
|
+
handler = RegisterAccountHandler.new command
|
41
|
+
handler.call
|
42
|
+
```
|
@@ -0,0 +1,56 @@
|
|
1
|
+
```ruby
|
2
|
+
class RegisterAccountCommand < Commandos::IAmACommand
|
3
|
+
values do
|
4
|
+
string :identity
|
5
|
+
string :password
|
6
|
+
string :password_confirmation
|
7
|
+
end
|
8
|
+
|
9
|
+
validates_presence_of \
|
10
|
+
:identity,
|
11
|
+
:password,
|
12
|
+
:password_confirmation
|
13
|
+
|
14
|
+
validate \
|
15
|
+
:password_confirmation!
|
16
|
+
|
17
|
+
private
|
18
|
+
def password_confirmation!
|
19
|
+
unless password_confirmation == password
|
20
|
+
errors.add :password_confirmation, "doesn't match password"
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
class RegisterAccountHandler
|
26
|
+
def initialize(command)
|
27
|
+
@command = command
|
28
|
+
end
|
29
|
+
|
30
|
+
def call
|
31
|
+
return command unless command.valid?
|
32
|
+
account_to_register = Account.new command.attributes
|
33
|
+
account_to_register.save!
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
|
38
|
+
class AccountCommandRegistry < IAmACommandRegistry
|
39
|
+
def initialize
|
40
|
+
register RegisterAccountCommand, handler: RegisterAccountHandler
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
class AccountService
|
45
|
+
def initialize
|
46
|
+
@dispatcher = Commandos::Dispatcher.new registry: AccountCommandRegistry.new
|
47
|
+
end
|
48
|
+
|
49
|
+
def register_account(safe_params)
|
50
|
+
dispatcher.dispatch RegisterAccountCommand.new safe_params
|
51
|
+
end
|
52
|
+
|
53
|
+
private
|
54
|
+
attr_reader :dispatcher
|
55
|
+
end
|
56
|
+
```
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module Commandos
|
2
|
+
class IAmACommand
|
3
|
+
module ClassMethods
|
4
|
+
def use(plugin, *args, &block)
|
5
|
+
unless plugins.include? plugin
|
6
|
+
plugins << plugin
|
7
|
+
extend plugin::ClassMethods if plugin.const_defined? :ClassMethods
|
8
|
+
include plugin::InstanceMethods if plugin.const_defined? :InstanceMethods
|
9
|
+
end
|
10
|
+
|
11
|
+
self
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
def plugins
|
16
|
+
@@pluglins ||= []
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
extend ClassMethods
|
21
|
+
use self
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module Commandos
|
2
|
+
UnknownCommand = Class.new StandardError
|
3
|
+
RegistryNotFound = Class.new StandardError
|
4
|
+
|
5
|
+
class Dispatcher
|
6
|
+
def initialize(registry: nil)
|
7
|
+
@registry = registry
|
8
|
+
end
|
9
|
+
|
10
|
+
def dispatch(command)
|
11
|
+
raise RegistryNotFound if registry.nil?
|
12
|
+
raise UnknownCommand unless command.kind_of? IAmACommand
|
13
|
+
|
14
|
+
registry.find_by command do |handler|
|
15
|
+
handler = handler.new command
|
16
|
+
result = handler.call
|
17
|
+
|
18
|
+
if block_given?
|
19
|
+
yield result
|
20
|
+
end
|
21
|
+
|
22
|
+
return result
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
attr_reader :registry
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
module Commandos
|
2
|
+
class IAmACommandRegistry
|
3
|
+
def initialize(storage: DefaultStorageStrategy)
|
4
|
+
@storage = storage
|
5
|
+
end
|
6
|
+
|
7
|
+
def [](command)
|
8
|
+
handlers.fetch(storage_strategy.call command) { NullHandler }.tap do |handler|
|
9
|
+
yield handler if block_given?
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
alias_method :find_by, :[]
|
14
|
+
|
15
|
+
protected
|
16
|
+
|
17
|
+
def handlers
|
18
|
+
@handlers ||= {}
|
19
|
+
end
|
20
|
+
|
21
|
+
def storage_strategy
|
22
|
+
@storage ||= DefaultStorageStrategy
|
23
|
+
end
|
24
|
+
|
25
|
+
def register(command, handler: nil)
|
26
|
+
unless handler.nil?
|
27
|
+
handlers[storage_strategy.call command] = handler
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
DefaultStorageStrategy = ->(command) do
|
33
|
+
return command.class unless command.kind_of? Class
|
34
|
+
command
|
35
|
+
end
|
36
|
+
|
37
|
+
class NullHandler
|
38
|
+
def initialize(command)
|
39
|
+
@command = command
|
40
|
+
end
|
41
|
+
|
42
|
+
def call
|
43
|
+
puts "-" * 65
|
44
|
+
puts " NO HANDLER REGISTERED FOR #{command.class.upcase}"
|
45
|
+
puts "-" * 65
|
46
|
+
puts command.inspect
|
47
|
+
end
|
48
|
+
|
49
|
+
private
|
50
|
+
attr_reader :command
|
51
|
+
end
|
52
|
+
end
|
data/lib/commandos.rb
ADDED
metadata
ADDED
@@ -0,0 +1,133 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: commandos
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.4
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Michael Elkins
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-02-27 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bump
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0.5'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0.5'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.14'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.14'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: minitest
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '5.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '5.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: minitest-color
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0.0'
|
83
|
+
description: Commandos is a library to help build the series of objects needed for
|
84
|
+
the command design pattern. It can help wtih building commands, handlers to execute
|
85
|
+
commands, and command registries.
|
86
|
+
email:
|
87
|
+
- mike@mdelkins.com
|
88
|
+
executables: []
|
89
|
+
extensions: []
|
90
|
+
extra_rdoc_files: []
|
91
|
+
files:
|
92
|
+
- ".gitignore"
|
93
|
+
- ".rspec"
|
94
|
+
- ".travis.yml"
|
95
|
+
- Gemfile
|
96
|
+
- LICENSE.txt
|
97
|
+
- README.md
|
98
|
+
- Rakefile
|
99
|
+
- bin/console
|
100
|
+
- bin/setup
|
101
|
+
- commandos.gemspec
|
102
|
+
- examples/command_and_handler.md
|
103
|
+
- examples/command_registry_and_dispatcher.md
|
104
|
+
- lib/commandos.rb
|
105
|
+
- lib/commandos/command.rb
|
106
|
+
- lib/commandos/dispatcher.rb
|
107
|
+
- lib/commandos/registry.rb
|
108
|
+
- lib/commandos/version.rb
|
109
|
+
homepage: https://github.com/mdelkins/commando
|
110
|
+
licenses:
|
111
|
+
- MIT
|
112
|
+
metadata: {}
|
113
|
+
post_install_message:
|
114
|
+
rdoc_options: []
|
115
|
+
require_paths:
|
116
|
+
- lib
|
117
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
118
|
+
requirements:
|
119
|
+
- - ">="
|
120
|
+
- !ruby/object:Gem::Version
|
121
|
+
version: '0'
|
122
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
123
|
+
requirements:
|
124
|
+
- - ">="
|
125
|
+
- !ruby/object:Gem::Version
|
126
|
+
version: '0'
|
127
|
+
requirements: []
|
128
|
+
rubyforge_project:
|
129
|
+
rubygems_version: 2.6.8
|
130
|
+
signing_key:
|
131
|
+
specification_version: 4
|
132
|
+
summary: Commandos is a library to support the command design pattern
|
133
|
+
test_files: []
|