skywalker 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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 189ad007dad735d22002e8e676be20d37202e427
4
+ data.tar.gz: 49633c02c8e18e1572ed5d570b3ad49e1ca71f85
5
+ SHA512:
6
+ metadata.gz: 32d48d08a9c6bd7a25af17485f2f0202fcee026b32133c3851093e2c530959148578c01dc29b012ea8f6ff08692c212b7eb8b912bdd695c1b4bc0c3bf3d77cc3
7
+ data.tar.gz: 4103d5ba416a5e403d2c00390c92f62d8001e7712a56b9c692e186ddc6d78e8eba589bae6b201ae4521e252cac066a391739d4dc98255a03834beb16eed91208
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
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
15
+
16
+ vendor/bundle
17
+ vendor/gems
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in skywalker.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Rob Yurkowski
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,89 @@
1
+ # Skywalker
2
+
3
+ Skywalker is a gem that provides a simple command pattern for applications that use transactions.
4
+
5
+ ## Why Skywalker?
6
+
7
+ It's impossible to come up with a single-word clever name for a gem about commands. If you can't
8
+ achieve clever, achieve topicality.
9
+
10
+ ## What is a command?
11
+
12
+ A command is simply a series of instructions that should be run in sequence
13
+ and considered a single unit. If one instruction fails, they should all fail.
14
+
15
+ That's a transaction, you say? You're correct! But there are some benefits of
16
+ considering transactional blocks as objects:
17
+
18
+ ### Testability
19
+
20
+ With a command, you inject most any argument, which means that you can simulate
21
+ the run of the command without providing real arguments. Best practice is to
22
+ describe the operations in methods, which can then be stubbed out to test small
23
+ portions in isolation.
24
+
25
+ This also allows you to make the reasonable inference that the command will abort
26
+ properly if one step raises an error, and by convention, the same method (`on_failure`)
27
+ will be called. In most cases, you can thereby verify happy path and a single bad path
28
+ through integration specs, and that will suffice.
29
+
30
+ ### Reasonability
31
+
32
+ The benefit of abstraction means that you can easily reason about a command without
33
+ having to know its internals. Standard caveats apply, but if you have a `CreateGroup`
34
+ command, you should be able to infer that calling the command with the correct arguments
35
+ will produce the expected result.
36
+
37
+ ### A gateway to harder architectures
38
+
39
+ It's not hard to create an `Event` class and step up toward full event sourcing, or to
40
+ go a bit further and implement full CQRS. This is the architectural pattern your parents
41
+ warned you about.
42
+
43
+ ## Installation
44
+
45
+ Add this line to your application's Gemfile:
46
+
47
+ ```ruby
48
+ gem 'skywalker'
49
+ ```
50
+
51
+ And then execute:
52
+
53
+ $ bundle
54
+
55
+ Or install it yourself as:
56
+
57
+ $ gem install skywalker
58
+
59
+ ## Usage
60
+
61
+ Compose your commands:
62
+
63
+ ```ruby
64
+ class AddGroupCommand < Skywalker::Command
65
+ def execute!
66
+ # Your transactional operations go here. No need to open a transaction.
67
+ # Simply make sure each method raises an error when it fails.
68
+ end
69
+ end
70
+ ```
71
+
72
+ Then call your commands:
73
+
74
+ ```ruby
75
+ command = AddGroupCommand.call(
76
+ on_success: method(:on_success),
77
+ on_failure: method(:on_failure)
78
+ )
79
+
80
+ You can pass any object responding to `#call` to the `on_success` and `on_failure` handlers, including procs, lambdas, controller methods, or other commands themselves.
81
+ ```
82
+
83
+ ## Contributing
84
+
85
+ 1. Fork it ( https://github.com/robyurkowski/skywalker/fork )
86
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
87
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
88
+ 4. Push to the branch (`git push origin my-new-feature`)
89
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
data/lib/skywalker.rb ADDED
@@ -0,0 +1,5 @@
1
+ require "skywalker/version"
2
+
3
+ module Skywalker
4
+ # Your code goes here...
5
+ end
@@ -0,0 +1,72 @@
1
+ require 'active_record'
2
+
3
+ module Skywalker
4
+ class Command
5
+ ################################################################################
6
+ # Class interface
7
+ ################################################################################
8
+ def self.call(*args)
9
+ new(*args).call
10
+ end
11
+
12
+
13
+ ################################################################################
14
+ # Instantiates command, setting all arguments.
15
+ ################################################################################
16
+ def initialize(on_success: nil, on_failure: nil)
17
+ self.on_success = on_success
18
+ self.on_failure = on_failure
19
+ end
20
+
21
+
22
+ attr_accessor :on_success,
23
+ :on_failure,
24
+ :error
25
+
26
+
27
+ ################################################################################
28
+ # Call: runs the transaction and all operations.
29
+ ################################################################################
30
+ def call
31
+ transaction do
32
+ execute!
33
+
34
+ confirm_success
35
+ end
36
+
37
+ rescue Exception => error
38
+ confirm_failure error
39
+ end
40
+
41
+
42
+ ################################################################################
43
+ # Operations should be defined in this method.
44
+ ################################################################################
45
+ private def execute!
46
+ end
47
+
48
+ ################################################################################
49
+ # Override to customize.
50
+ ################################################################################
51
+ private def transaction(&block)
52
+ ::ActiveRecord::Base.transaction(&block)
53
+ end
54
+
55
+
56
+ ################################################################################
57
+ # Trigger the given callback on success
58
+ ################################################################################
59
+ private def confirm_success
60
+ on_success.call(self)
61
+ end
62
+
63
+
64
+ ################################################################################
65
+ # Set the error so we can get it with `command.error`, and trigger error.
66
+ ################################################################################
67
+ private def confirm_failure(error)
68
+ self.error = error
69
+ on_failure.call(self)
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,3 @@
1
+ module Skywalker
2
+ VERSION = "1.0.0"
3
+ end
data/skywalker.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 'skywalker/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "skywalker"
8
+ spec.version = Skywalker::VERSION
9
+ spec.authors = ["Rob Yurkowski"]
10
+ spec.email = ["rob@yurkowski.net"]
11
+ spec.summary = %q{A simple command pattern implementation for transactional operations.}
12
+ spec.description = %q{A simple command pattern implementation for transactional operations.}
13
+ spec.homepage = "https://github.com/robyurkowski/skywalker"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency 'activerecord', '~> 4.1'
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.7"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "rspec"
26
+ spec.add_development_dependency "pry"
27
+ spec.add_development_dependency "pry-byebug"
28
+ end
@@ -0,0 +1,90 @@
1
+ require 'spec_helper'
2
+ require 'skywalker/command'
3
+
4
+ module Skywalker
5
+ RSpec.describe Command do
6
+ describe "convenience" do
7
+ it "provides a class call method that instantiates and calls" do
8
+ arg = 'blah'
9
+
10
+ expect(Command).to receive_message_chain('new.call')
11
+ Command.call(arg)
12
+ end
13
+ end
14
+
15
+
16
+ describe "instantiation" do
17
+ it "accepts an on_success callback" do
18
+ expect { Command.new(on_success: ->{ nil }) }.not_to raise_error
19
+ end
20
+
21
+ it "accepts an on_failure callback" do
22
+ expect { Command.new(on_failure: ->{ nil }) }.not_to raise_error
23
+ end
24
+ end
25
+
26
+
27
+ describe "validity control" do
28
+ let(:command) { Command.new }
29
+
30
+ it "executes in a transaction" do
31
+ expect(command).to receive(:transaction)
32
+ command.call
33
+ end
34
+ end
35
+
36
+
37
+ describe "execution" do
38
+ before do
39
+ allow(command).to receive(:transaction).and_yield
40
+ end
41
+
42
+
43
+ describe "success handling" do
44
+ let(:on_success) { double("on_success callback") }
45
+ let(:command) { Command.new(on_success: on_success) }
46
+
47
+ before do
48
+ allow(command).to receive(:execute!).and_return(true)
49
+ end
50
+
51
+ it "triggers the confirm_success method" do
52
+ expect(command).to receive(:confirm_success)
53
+ command.call
54
+ end
55
+
56
+ it "calls the on_success callback with itself" do
57
+ expect(on_success).to receive(:call).with(command)
58
+ command.call
59
+ end
60
+ end
61
+
62
+
63
+ describe "failure handling" do
64
+ let(:on_failure) { double("on_failure callback") }
65
+ let(:command) { Command.new(on_failure: on_failure) }
66
+
67
+ before do
68
+ allow(command).to receive(:execute!).and_raise(ScriptError)
69
+ end
70
+
71
+ it "triggers the confirm_failure method" do
72
+ expect(command).to receive(:confirm_failure)
73
+ command.call
74
+ end
75
+
76
+ it "sets the error on the command" do
77
+ allow(on_failure).to receive(:call)
78
+ expect(command).to receive(:error=)
79
+ command.call
80
+ end
81
+
82
+ it "calls the on_failure callback with itself" do
83
+ allow(command).to receive(:error=)
84
+ expect(on_failure).to receive(:call).with(command)
85
+ command.call
86
+ end
87
+ end
88
+ end
89
+ end
90
+ end
@@ -0,0 +1,85 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # The generated `.rspec` file contains `--require spec_helper` which will cause this
4
+ # file to always be loaded, without a need to explicitly require it in any files.
5
+ #
6
+ # Given that it is always loaded, you are encouraged to keep this file as
7
+ # light-weight as possible. Requiring heavyweight dependencies from this file
8
+ # will add to the boot time of your test suite on EVERY test run, even for an
9
+ # individual file that may not need all of that loaded. Instead, consider making
10
+ # a separate helper file that requires the additional dependencies and performs
11
+ # the additional setup, and require it from the spec files that actually need it.
12
+ #
13
+ # The `.rspec` file also contains a few flags that are not defaults but that
14
+ # users commonly want.
15
+ #
16
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
17
+ RSpec.configure do |config|
18
+ # rspec-expectations config goes here. You can use an alternate
19
+ # assertion/expectation library such as wrong or the stdlib/minitest
20
+ # assertions if you prefer.
21
+ config.expect_with :rspec do |expectations|
22
+ # This option will default to `true` in RSpec 4. It makes the `description`
23
+ # and `failure_message` of custom matchers include text for helper methods
24
+ # defined using `chain`, e.g.:
25
+ # be_bigger_than(2).and_smaller_than(4).description
26
+ # # => "be bigger than 2 and smaller than 4"
27
+ # ...rather than:
28
+ # # => "be bigger than 2"
29
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
30
+ end
31
+
32
+ # rspec-mocks config goes here. You can use an alternate test double
33
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
34
+ config.mock_with :rspec do |mocks|
35
+ # Prevents you from mocking or stubbing a method that does not exist on
36
+ # a real object. This is generally recommended, and will default to
37
+ # `true` in RSpec 4.
38
+ mocks.verify_partial_doubles = true
39
+ end
40
+
41
+ # These two settings work together to allow you to limit a spec run
42
+ # to individual examples or groups you care about by tagging them with
43
+ # `:focus` metadata. When nothing is tagged with `:focus`, all examples
44
+ # get run.
45
+ config.filter_run :focus
46
+ config.run_all_when_everything_filtered = true
47
+
48
+ # Limits the available syntax to the non-monkey patched syntax that is recommended.
49
+ # For more details, see:
50
+ # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
51
+ # - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
52
+ # - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
53
+ config.disable_monkey_patching!
54
+
55
+ # This setting enables warnings. It's recommended, but in some cases may
56
+ # be too noisy due to issues in dependencies.
57
+ config.warnings = true
58
+
59
+ # Many RSpec users commonly either run the entire suite or an individual
60
+ # file, and it's useful to allow more verbose output when running an
61
+ # individual spec file.
62
+ if config.files_to_run.one?
63
+ # Use the documentation formatter for detailed output,
64
+ # unless a formatter has already been configured
65
+ # (e.g. via a command-line flag).
66
+ config.default_formatter = 'doc'
67
+ end
68
+
69
+ # Print the 10 slowest examples and example groups at the
70
+ # end of the spec run, to help surface which specs are running
71
+ # particularly slow.
72
+ # config.profile_examples = 10
73
+
74
+ # Run specs in random order to surface order dependencies. If you find an
75
+ # order dependency and want to debug it, you can fix the order by providing
76
+ # the seed, which is printed after each run.
77
+ # --seed 1234
78
+ config.order = :random
79
+
80
+ # Seed global randomization in this process using the `--seed` CLI option.
81
+ # Setting this allows you to use `--seed` to deterministically reproduce
82
+ # test failures related to randomization by passing the same `--seed` value
83
+ # as the one that triggered the failure.
84
+ Kernel.srand config.seed
85
+ end
metadata ADDED
@@ -0,0 +1,142 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: skywalker
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Rob Yurkowski
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-11-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '4.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '4.1'
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.7'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.7'
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: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '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'
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry-byebug
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: A simple command pattern implementation for transactional operations.
98
+ email:
99
+ - rob@yurkowski.net
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".rspec"
106
+ - Gemfile
107
+ - LICENSE.txt
108
+ - README.md
109
+ - Rakefile
110
+ - lib/skywalker.rb
111
+ - lib/skywalker/command.rb
112
+ - lib/skywalker/version.rb
113
+ - skywalker.gemspec
114
+ - spec/lib/skywalker/command_spec.rb
115
+ - spec/spec_helper.rb
116
+ homepage: https://github.com/robyurkowski/skywalker
117
+ licenses:
118
+ - MIT
119
+ metadata: {}
120
+ post_install_message:
121
+ rdoc_options: []
122
+ require_paths:
123
+ - lib
124
+ required_ruby_version: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - ">="
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ required_rubygems_version: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - ">="
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ requirements: []
135
+ rubyforge_project:
136
+ rubygems_version: 2.2.2
137
+ signing_key:
138
+ specification_version: 4
139
+ summary: A simple command pattern implementation for transactional operations.
140
+ test_files:
141
+ - spec/lib/skywalker/command_spec.rb
142
+ - spec/spec_helper.rb