commandoes 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 01232f3d19184508a987df015c4dc04c22dae0c7
4
+ data.tar.gz: c8a05f7e1aa4349255e4376619d3763e82ac5640
5
+ SHA512:
6
+ metadata.gz: 71d40f8d35e901e9b1aca4525390b3e6b1c2db31158f81c3d79191215283625823ed66a1e7eecd92ac198215ff88b0281e4681e7a506b9de9fe68152b29a01dc
7
+ data.tar.gz: 6b631a98bc261237835acd06628163d1c9a43db0f955df21fbe7638ea431ce3c0f3e55e6b36cf19424229c13e57775e545b488ed1799e6777121fd0987729ccb
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
13
+
14
+ .ruby-version
15
+ .ruby-gemset
16
+ .DS_Store
17
+ *.swp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.14.4
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+ ruby '2.3.1'
3
+
4
+ gemspec
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,44 @@
1
+ # COMMANDO
2
+
3
+ Commandoes 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
+ * Virtus (https://github.com/solnic/virtus)
11
+ * ActiveModel::Validations
12
+
13
+ ## INSTALLATION
14
+
15
+ In your Gemfile
16
+
17
+ ```
18
+ gem 'commando', github: 'mdelkins/commando'
19
+ ```
20
+
21
+ ## EXAMPLES
22
+
23
+ 1. [Command & Handler](./examples/command_and_handler.md)
24
+ 2. [Registry & Dispatcher](./examples/command_registry_and_dispatcher.md)
25
+
26
+ ## PLUGINS
27
+
28
+ Commandoes uses a plugin system similar to
29
+ [sequel](https://github.com/jeremyevans/sequel) or
30
+ [roda](https://github.com/jeremyevans/roda) to extend the behavior of your
31
+ command objects. Currently their are two dependent plugins:
32
+
33
+ * Commandoes::Plugins::VirtusPlugin
34
+ * Commandoes::Plugins::ActiveModelPlugin
35
+
36
+ ### Commandoes::Plugins::VirtusPlugin
37
+
38
+ This plugin injects the `Virtus.value_object` behavior into
39
+ `Commandoes::IAmACommand` as well as a few helper methods for easily defining
40
+ typed attributes.
41
+
42
+ ### Commandoes::Plugins::ActiveModelPlugin
43
+
44
+ This plugin is used to give commands active model like validation behavior.
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
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
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'commandoes/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "commandoes"
8
+ spec.version = Commandoes::VERSION
9
+ spec.authors = ["Michael Elkins"]
10
+ spec.email = ["mike@mdelkins.com"]
11
+
12
+ spec.summary = %q{Commandoes is a library to support the command design pattern}
13
+ spec.description = %q{Commandoes 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_dependency "virtus", "~> 1.0"
24
+ spec.add_dependency "activemodel", "~> 5.0"
25
+
26
+ spec.add_development_dependency "bump", "~> 0.5"
27
+ spec.add_development_dependency "bundler", "~> 1.14"
28
+ spec.add_development_dependency "rake", "~> 10.0"
29
+ spec.add_development_dependency "minitest", "~> 5.0"
30
+ spec.add_development_dependency "minitest-color", "~> 0.0"
31
+ end
@@ -0,0 +1,42 @@
1
+ # COMMAND & HANDLER
2
+
3
+ ```ruby
4
+ class RegisterAccountCommand < Commandoes::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 < Commandoes::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 = Commandoes::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,29 @@
1
+ module Commandoes
2
+ class IAmACommand
3
+ include ActiveModel::Validations
4
+
5
+ module ClassMethods
6
+ def use(plugin, *args, &block)
7
+ unless plugins.include? plugin
8
+ plugins << plugin
9
+ extend plugin::ClassMethods if plugin.const_defined? :ClassMethods
10
+ include plugin::InstanceMethods if plugin.const_defined? :InstanceMethods
11
+ end
12
+
13
+ self
14
+ end
15
+
16
+ private
17
+ def plugins
18
+ @@pluglins ||= []
19
+ end
20
+ end
21
+
22
+ extend ClassMethods
23
+
24
+ use self
25
+ use Plugins::VirtusPlugin
26
+ use Plugins::ActiveModelPlugin
27
+
28
+ end
29
+ end
@@ -0,0 +1,29 @@
1
+ module Commandoes
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 unless registry.present?
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,23 @@
1
+ module Commandoes
2
+ module Plugins
3
+ module ActiveModelPlugin
4
+
5
+ module ClassMethods
6
+ def self.extended(object)
7
+ object.send(:include, ActiveModel::Validations)
8
+ end
9
+ end
10
+
11
+ module InstanceMethods
12
+ def valid?
13
+ @valid ||= super
14
+ end
15
+
16
+ def invalid?
17
+ not valid?
18
+ end
19
+ end
20
+
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,47 @@
1
+ module Commandoes
2
+ module Plugins
3
+ module VirtusPlugin
4
+
5
+ module ClassMethods
6
+ def self.extended(object)
7
+ object.send(:include, Virtus.value_object)
8
+ end
9
+
10
+ def bool(value, options={})
11
+ attribute value, Axiom::Types::Boolean, options
12
+ end
13
+
14
+ def date(value, options={})
15
+ attribute value, Date, options
16
+ end
17
+
18
+ def datetime(value, options={})
19
+ attribute value, DateTime, options
20
+ end
21
+
22
+ def decimal(value, options={})
23
+ attribute value, BigDecimal, options
24
+ end
25
+
26
+ def float(value, options={})
27
+ attribute value, Float, options
28
+ end
29
+
30
+ def integer(value, options={})
31
+ attribute value, Integer, options
32
+ end
33
+
34
+ def string(value, options={})
35
+ attribute value, String, options
36
+ end
37
+
38
+ def time(value, options={})
39
+ attribute value, Time, options
40
+ end
41
+ end
42
+
43
+ module InstanceMethods
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,52 @@
1
+ module Commandoes
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
@@ -0,0 +1,3 @@
1
+ module Commandoes
2
+ VERSION = "0.1.1"
3
+ end
data/lib/commandoes.rb ADDED
@@ -0,0 +1,13 @@
1
+
2
+ require 'active_model'
3
+ require 'virtus'
4
+
5
+ require "commandoes/plugins/virtus_plugin"
6
+ require "commandoes/plugins/activemodel_plugin"
7
+
8
+ require "commandoes/command"
9
+ require "commandoes/dispatcher"
10
+ require "commandoes/registry"
11
+
12
+ require "commandoes/version"
13
+
metadata ADDED
@@ -0,0 +1,163 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: commandoes
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Michael Elkins
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-02-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: virtus
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activemodel
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '5.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '5.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bump
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.5'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.5'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.14'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.14'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: minitest
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '5.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '5.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: minitest-color
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '0.0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '0.0'
111
+ description: Commandoes is a library to help build the series of objects needed for
112
+ the command design pattern. It can help wtih building commands, handlers to execute
113
+ commands, and command registries.
114
+ email:
115
+ - mike@mdelkins.com
116
+ executables: []
117
+ extensions: []
118
+ extra_rdoc_files: []
119
+ files:
120
+ - ".gitignore"
121
+ - ".rspec"
122
+ - ".travis.yml"
123
+ - Gemfile
124
+ - LICENSE.txt
125
+ - README.md
126
+ - Rakefile
127
+ - bin/console
128
+ - bin/setup
129
+ - commandoes.gemspec
130
+ - examples/command_and_handler.md
131
+ - examples/command_registry_and_dispatcher.md
132
+ - lib/commandoes.rb
133
+ - lib/commandoes/command.rb
134
+ - lib/commandoes/dispatcher.rb
135
+ - lib/commandoes/plugins/activemodel_plugin.rb
136
+ - lib/commandoes/plugins/virtus_plugin.rb
137
+ - lib/commandoes/registry.rb
138
+ - lib/commandoes/version.rb
139
+ homepage: https://github.com/mdelkins/commando
140
+ licenses:
141
+ - MIT
142
+ metadata: {}
143
+ post_install_message:
144
+ rdoc_options: []
145
+ require_paths:
146
+ - lib
147
+ required_ruby_version: !ruby/object:Gem::Requirement
148
+ requirements:
149
+ - - ">="
150
+ - !ruby/object:Gem::Version
151
+ version: '0'
152
+ required_rubygems_version: !ruby/object:Gem::Requirement
153
+ requirements:
154
+ - - ">="
155
+ - !ruby/object:Gem::Version
156
+ version: '0'
157
+ requirements: []
158
+ rubyforge_project:
159
+ rubygems_version: 2.6.6
160
+ signing_key:
161
+ specification_version: 4
162
+ summary: Commandoes is a library to support the command design pattern
163
+ test_files: []