composed_commands 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: e6f39ae0dbc85aa8f47d04a015bcb8b3210f6193
4
+ data.tar.gz: c3554d3c2cdaeee13d8aa3f946e68ede12e69e6e
5
+ SHA512:
6
+ metadata.gz: 955a066338679cbd5e3a8ff0b3758e8df5d6dcd3d98ce7d4f94a7dd276cde83e0fce7aaaa9f492fad521945250d7058e9170002c829aaf5380cdf464d9a1319e
7
+ data.tar.gz: f67ca830581ca40b2fafcfcd52c409e4495da284c9de07a6b80ffb591caa3587d368aaa92fafee1f8172ecb8b9a8bd6965283e3803a80f2503eb8486a82b2ff8
data/.gitignore ADDED
@@ -0,0 +1,14 @@
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
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in composed_commands.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,176 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
data/README.md ADDED
@@ -0,0 +1,186 @@
1
+ # ComposedCommands
2
+
3
+ Composed Commands is a tool set for creating commands and assembling
4
+ multiple of these commands in operation pipelines. A command is, at its
5
+ core, an implementation of the [strategy
6
+ pattern](http://en.wikipedia.org/wiki/Strategy_pattern) and in this sense an
7
+ encapsulation of an algorithm. An operation pipeline is an assembly of multiple
8
+ commands and useful for implementing complex algorithms. Pipelines themselves
9
+ can be part of other pipelines.
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ gem 'composed_commands'
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install composed_commands
24
+
25
+ ## Usage
26
+
27
+ Operations can be defined by subclassing `ComposedCommands::Command` and
28
+ operation pipelines by subclassing `ComposedCommands::ComposedCommand`.
29
+
30
+ ### Defining an Command
31
+
32
+ To define an command, two steps are necessary:
33
+
34
+ 1. create a new subclass of `ComposedCommands::Command`, and
35
+ 2. implement the `#execute` method.
36
+
37
+ The listing below shows an operation that extracts a timestamp in the format
38
+ `yyyy-mm-dd` from a string.
39
+
40
+ ```ruby
41
+ class DateExtractor < ComposedCommands::Command
42
+ def execute(text)
43
+ text.scan(/(\d{4})-(\d{2})-(\d{2})/)
44
+ end
45
+
46
+ end
47
+ ```
48
+
49
+ There are two ways to execute this operation:
50
+
51
+ 1. create a new instance of this command and call `#perform`, or
52
+ 2. directly call `.perform` on the command class.
53
+
54
+ Please note that directly calling the `#execute`
55
+ method is prohibited. To enforce this constraint, the method is automatically
56
+ marked as protected upon definition.
57
+
58
+ The listing below demonstrates how to execute the command defined above.
59
+
60
+ ```ruby
61
+ text = "This gem was first published on 2013-06-10."
62
+
63
+ extractor = DateExtractor.new
64
+ extractor.perform(text) # => [["2013", "06", "10"]]
65
+
66
+ DateExtractor.perform(text) # => [["2013", "06", "10"]]
67
+ ```
68
+
69
+ ### Defining an Command Pipeline
70
+
71
+ Assume that we are provided a command that converts these arrays of strings
72
+ into actual `Time` objects. The following listing provides a potential
73
+ implementation of such an operation.
74
+
75
+ ```ruby
76
+ class DateArrayToTimeObjectConverter < ComposedCommands::Command
77
+
78
+ def execute(collection_of_date_arrays)
79
+ collection_of_date_arrays.map do |date_array|
80
+ Time.new(*(date_array.map(&:to_i)))
81
+ end
82
+ end
83
+
84
+ end
85
+ ```
86
+
87
+ Using these two commands, it is possible to create a composed command that
88
+ extracts dates from a string and directly converts them into `Time` objects. To
89
+ define a composed command, two steps are necessary:
90
+
91
+ 1. create a subclass of `ComposedCommands::ComposedCommand`, and
92
+ 2. use the macro method `use` to assemble the command.
93
+
94
+ The listing below shows how to assemble the two commands, `DateExtractor` and
95
+ `DateArrayToTimeObjectConverter`, into a composed command named `DateParser`.
96
+
97
+ ```ruby
98
+ class DateParser < ComposedCommands::ComposedCommand
99
+
100
+ use DateExtractor
101
+ use DateArrayToTimeObjectConverter
102
+
103
+ end
104
+ ```
105
+
106
+ Composed commands provide the same interface as normal commands. Hence,
107
+ they can be invoked the same way. For the sake of completeness, the listing
108
+ below shows how to use the `DateParser` operation.
109
+
110
+ ```ruby
111
+ text = "This gem was first published on 2013-06-10."
112
+
113
+ parser = DateParser.new
114
+ parser.perform(text) # => 2013-06-07 00:00:00 +0200
115
+
116
+ DateParser.perform(text) # => 2013-06-07 00:00:00 +0200
117
+ ```
118
+
119
+ ### Control Flow
120
+
121
+ A command can be *aborted* if a successful execution is not
122
+ possible. The listing below provides examples on how to access an command's state.
123
+
124
+ ```ruby
125
+ class StrictDateParser < DateParser
126
+
127
+ def execute
128
+ result = super
129
+ fail! "no timestamp found" if result.empty?
130
+ result
131
+ end
132
+
133
+ end
134
+ parser = StrictDateParser.new("")
135
+ parser.message # => "no timestamp found"
136
+ parser.perform
137
+ parser.succeed? # => false
138
+ parser.failed? # => true
139
+ ```
140
+
141
+ ### Configuring Commands
142
+
143
+ Commands and composed commands support
144
+ [Virtus](https://github.com/solnic/virtus) to conveniently
145
+ provide additional settings upon initialization of a command. In the
146
+ example below, a command is defined that indents a given string. The indent
147
+ is set to 2 by default but can easily be changed by supplying an options hash
148
+ to the initializer.
149
+
150
+ ```ruby
151
+ class Indention < ComposedCommands::Command
152
+ attribute :indent, Integer, default: 2, required: true
153
+
154
+ def execute(text)
155
+ text.split("\n").map { |line| " " * indent + line }.join("\n")
156
+ end
157
+
158
+ end
159
+
160
+ command = Indention.perform("Hello World", indent: 4)
161
+ command.result = # => " Hello World"
162
+ ```
163
+
164
+ Commands that are part of a composed command can be configured by calling
165
+ the `.use` method with a hash of options as the second argument. See the
166
+ listing below for an example.
167
+
168
+ ```ruby
169
+ class SomeComposedCommand < ComposedCommands::ComposedCommand
170
+
171
+ # ...
172
+ use Indention, indent: 4
173
+ # ...
174
+
175
+ end
176
+ ```
177
+
178
+ This based on [Composable Operations](https://github.com/t6d/composable_operations) written by Konstantin Tennhard
179
+
180
+ ## Contributing
181
+
182
+ 1. Fork it
183
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
184
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
185
+ 4. Push to the branch (`git push origin my-new-feature`)
186
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'composed_commands/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'composed_commands'
8
+ spec.version = ComposedCommands::VERSION
9
+ spec.authors = ['Tema Bolshakov']
10
+ spec.email = ['abolshakov@spbtv.com']
11
+ spec.summary = "Tool for creating commands and commands chains."
12
+ spec.description = "Tool for creating commands and commands chains."
13
+ spec.homepage = ""
14
+ spec.license = 'Apache License, Version 2.0'
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_runtime_dependency 'activesupport', '~> 4'
22
+ spec.add_runtime_dependency 'workflow', '~> 1.1'
23
+ spec.add_runtime_dependency 'virtus', '~> 1'
24
+ spec.add_development_dependency 'bundler', '~> 1.7'
25
+ spec.add_development_dependency 'rake', '~> 10.0'
26
+ spec.add_development_dependency 'rspec', '~> 3.1'
27
+ end
@@ -0,0 +1,22 @@
1
+ require 'workflow'
2
+ require 'active_support/core_ext/module/delegation'
3
+ require 'active_support/dependencies/autoload'
4
+
5
+ module ComposedCommands
6
+ class Command
7
+ class Execution
8
+ include Workflow
9
+ workflow do
10
+ state :pending do
11
+ event :perform, transitions_to: :performing
12
+ end
13
+ state :performing do
14
+ event :interrupt, transitions_to: :interrupted
15
+ event :done, transitions_to: :performed
16
+ end
17
+ state :interrupted
18
+ state :performed
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,17 @@
1
+ require 'workflow'
2
+
3
+ module ComposedCommands
4
+ class Command
5
+ class State
6
+ include Workflow
7
+ workflow do
8
+ state :undefined do
9
+ event :fail, transitions_to: :failed
10
+ event :success, transitions_to: :succeed
11
+ end
12
+ state :failed
13
+ state :succeed
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,66 @@
1
+ require 'virtus'
2
+ require 'active_support/core_ext/module/delegation'
3
+ require 'active_support/core_ext/array/extract_options'
4
+ require 'active_support/dependencies/autoload'
5
+
6
+ module ComposedCommands
7
+ class Command
8
+ extend ActiveSupport::Autoload
9
+ include Virtus.model
10
+ autoload :Execution
11
+ autoload :State
12
+
13
+ attr_reader :result
14
+ attr_reader :message
15
+ attr_accessor :execution
16
+ attr_accessor :state
17
+ delegate :failed?, :succeed?, :current_state, to: :state
18
+
19
+ def initialize(options = {})
20
+ @execution = Execution.new
21
+ @state = State.new
22
+
23
+ super(options)
24
+ end
25
+
26
+ def self.perform(*args)
27
+ options = args.extract_options!
28
+ new(options).perform(*args)
29
+ end
30
+
31
+ def perform(*args)
32
+ @result = catch(:halt) do
33
+ execution.perform!
34
+ result = execute(*args)
35
+ state.success!
36
+ execution.done!
37
+ result
38
+ end
39
+ self
40
+ end
41
+
42
+ def halted?
43
+ execution.interrupted?
44
+ end
45
+
46
+ protected
47
+
48
+ def execute(*_)
49
+ raise NotImplementedError, "#{self.class.name}#execute not implemented"
50
+ end
51
+
52
+ def fail!(message)
53
+ # TODO: Move message to result?
54
+ @message = message
55
+ state.fail!
56
+ execution.interrupt!
57
+ throw :halt
58
+ end
59
+
60
+ def success!(result)
61
+ state.success!
62
+ execution.interrupt!
63
+ throw :halt, result
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,39 @@
1
+ require 'composed_commands/command'
2
+
3
+ module ComposedCommands
4
+ class ComposedCommand < Command
5
+
6
+ def self.commands
7
+ inherited_commands = if self.superclass.respond_to?(:commands)
8
+ self.superclass.commands
9
+ else
10
+ []
11
+ end
12
+ Array(inherited_commands) + Array(@commands)
13
+ end
14
+
15
+ def self.use(klass, options = {})
16
+ (@commands ||= []).push(klass.new(options))
17
+ end
18
+
19
+ protected
20
+ def execute(*args)
21
+ self.class.commands.inject(args) do |data, command|
22
+ command.perform(*Array(data))
23
+
24
+ if command.halted?
25
+ case
26
+ when command.failed?
27
+ fail! command.message
28
+ when command.succeed?
29
+ success! command.result
30
+ else
31
+ raise "Unexpected state for interrupted command: `#{command.current_state.name}`"
32
+ end
33
+ end
34
+
35
+ command.result
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,3 @@
1
+ module ComposedCommands
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,10 @@
1
+ require "composed_commands/version"
2
+ require 'active_support/dependencies/autoload'
3
+
4
+ module ComposedCommands
5
+ extend ActiveSupport::Autoload
6
+
7
+ autoload :Command
8
+ autoload :ComposedCommand
9
+ end
10
+
@@ -0,0 +1,11 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe ComposedCommands::Command, 'attributes' do
4
+ let(:string) { 'chunky bacon' }
5
+ subject(:command) { StringMultiplier.new(separator: '/') }
6
+
7
+ it 'initialize attributes' do
8
+ command.perform(string)
9
+ expect(command.result).to eq "#{string}/#{string}/#{string}"
10
+ end
11
+ end
@@ -0,0 +1,106 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe ComposedCommands::Command do
4
+ let(:string) { 'Chunky Bacon' }
5
+ let(:execution) { ComposedCommands::Command::Execution.new }
6
+ let(:state) { ComposedCommands::Command::State.new }
7
+
8
+ subject(:command) { StringCapitalizer.new }
9
+
10
+ context 'delagations' do
11
+ before do
12
+ command.execution = execution
13
+ command.state = state
14
+ end
15
+
16
+ it 'delegate #interrupted? to #execution' do
17
+ expect(execution).to receive(:interrupted?)
18
+
19
+ command.halted?
20
+ end
21
+
22
+ it 'delegate #failed? to #state' do
23
+ expect(state).to receive(:failed?)
24
+
25
+ command.failed?
26
+ end
27
+
28
+ it 'delegate #succeed? to #state' do
29
+ expect(state).to receive(:succeed?)
30
+
31
+ command.succeed?
32
+ end
33
+
34
+ it 'delegate #current_state to #state' do
35
+ expect(state).to receive(:current_state)
36
+
37
+ command.current_state
38
+ end
39
+ end
40
+
41
+ context '#perform' do
42
+ before :example do
43
+ expect(command).to receive(:execute).and_wrap_original do |method, *args|
44
+ expect(command.execution).to be_performing
45
+ expect(command.state).to be_undefined
46
+
47
+ method.call(*args)
48
+ end
49
+ end
50
+
51
+ before :example do
52
+ expect(command.execution).to be_pending
53
+ expect(command.state).to be_undefined
54
+ end
55
+
56
+ after :example do
57
+ expect(command.execution).to be_performed
58
+ expect(command.state).to be_succeed
59
+ end
60
+
61
+ it 'yield result' do
62
+ res = command.perform(string)
63
+ expect(res).to eq command
64
+ expect(command.result).to eq 'CHUNKY BACON'
65
+ end
66
+ end
67
+
68
+ context '.perform' do
69
+ let(:multiplicator) { 2 }
70
+ let(:separator) { '|' }
71
+
72
+ subject(:command) { StringMultiplier.perform(string, multiplicator: multiplicator, separator: separator) }
73
+ it { expect(command.multiplicator).to eq multiplicator }
74
+ it { expect(command.separator).to eq separator }
75
+ it { expect(command.result).to eq "#{string}|#{string}" }
76
+ it { expect(command).to be_succeed }
77
+
78
+ end
79
+ context '#fail!' do
80
+ subject(:failing_command) { FailingCommand.new }
81
+
82
+ before :example do
83
+ failing_command.perform
84
+ end
85
+
86
+ it { expect(failing_command.result).to be_nil }
87
+ it { expect(failing_command.message).to eq 'failure message' }
88
+ it { expect(failing_command).to be_failed }
89
+ it { expect(failing_command).to be_halted }
90
+ it { expect(failing_command).not_to be_succeed }
91
+ end
92
+
93
+ context '#success!' do
94
+ subject(:succeed_command) { SucceedCommand.new }
95
+
96
+ before :example do
97
+ succeed_command.perform
98
+ end
99
+
100
+ it { expect(succeed_command.result).to eq 'successive result' }
101
+ it { expect(succeed_command.message).to be_nil }
102
+ it { expect(succeed_command).not_to be_failed }
103
+ it { expect(succeed_command).to be_halted }
104
+ it { expect(succeed_command).to be_succeed }
105
+ end
106
+ end
@@ -0,0 +1,12 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe ComposedCommands::ComposedCommand, 'attributes' do
4
+ let(:input) { 'chunky bacon' }
5
+
6
+ subject(:command) { ConfigurableChunkyBaconProcessor.new }
7
+
8
+ it 'count attributes, passed to StringMultiplier' do
9
+ command.perform(input)
10
+ expect(command.result).to eq "#{input}|#{input}"
11
+ end
12
+ end
@@ -0,0 +1,58 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe ComposedCommands::ComposedCommand do
4
+ let(:input) { 'chunky bacon' }
5
+
6
+ context '#perfrorm' do
7
+ it 'performs successfully' do
8
+ command = ChunkyBaconCapitalizer.new
9
+ command.perform(input)
10
+
11
+ expect(command.result).to eq 'CHUNKY BACON'
12
+ expect(command.message).to be_nil
13
+ expect(command).not_to be_failed
14
+ expect(command).not_to be_halted
15
+ expect(command).to be_succeed
16
+ end
17
+
18
+ it 'performs with failure! in first command' do
19
+ command = CompoundCommandWithFailingCommand.new
20
+
21
+ command.perform
22
+
23
+ expect(command.result).to be_nil
24
+ expect(command.message).to eq 'failure message'
25
+ expect(command).to be_failed
26
+ expect(command).to be_halted
27
+ expect(command).not_to be_succeed
28
+ end
29
+
30
+ it 'performs with success! in first command' do
31
+ command = CompoundCommandWithSuccessCommand.new
32
+
33
+ command.perform
34
+
35
+ expect(command.result).to eq 'successive result'
36
+ expect(command.message).to be_nil
37
+ expect(command).not_to be_failed
38
+ expect(command).to be_halted
39
+ expect(command).to be_succeed
40
+ end
41
+ end
42
+
43
+ context '.use' do
44
+ subject(:command) { ChunkyBaconCapitalizer }
45
+ it { expect(command.commands.size).to eq 2 }
46
+ it { expect(command.commands.first).to be_kind_of StringGenerator }
47
+ it { expect(command.commands.last).to be_kind_of StringCapitalizer }
48
+
49
+ context 'inherited compound commands' do
50
+ subject(:child_command) { ChunkyBaconCapitalizerWithMuliplication }
51
+
52
+ it { expect(child_command.commands.size).to eq 3 }
53
+ it { expect(child_command.commands[0]).to be_kind_of StringGenerator }
54
+ it { expect(child_command.commands[1]).to be_kind_of StringCapitalizer }
55
+ it { expect(child_command.commands[2]).to be_kind_of StringMultiplier }
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,61 @@
1
+ require 'composed_commands'
2
+
3
+ class StringGenerator < ComposedCommands::Command
4
+ def execute(string)
5
+ string
6
+ end
7
+ end
8
+
9
+ class StringCapitalizer < ComposedCommands::Command
10
+ def execute(text)
11
+ text.upcase
12
+ end
13
+ end
14
+
15
+ class StringMultiplier < ComposedCommands::Command
16
+ attribute :multiplicator, Integer, default: '3'
17
+ attribute :separator, String
18
+
19
+ def execute(text)
20
+ (Array(text) * multiplicator).join(separator)
21
+ end
22
+ end
23
+
24
+ class FailingCommand < ComposedCommands::Command
25
+ def execute
26
+ fail! 'failure message'
27
+ input
28
+ end
29
+ end
30
+
31
+ class SucceedCommand < ComposedCommands::Command
32
+ def execute
33
+ success! 'successive result'
34
+ fail! 'failure message'
35
+ end
36
+ end
37
+
38
+
39
+ class ChunkyBaconCapitalizer < ComposedCommands::ComposedCommand
40
+ use StringGenerator
41
+ use StringCapitalizer
42
+ end
43
+
44
+ class ChunkyBaconCapitalizerWithMuliplication < ChunkyBaconCapitalizer
45
+ use StringMultiplier
46
+ end
47
+
48
+ class CompoundCommandWithFailingCommand < ComposedCommands::ComposedCommand
49
+ use FailingCommand
50
+ use StringGenerator
51
+ end
52
+
53
+ class CompoundCommandWithSuccessCommand < ComposedCommands::ComposedCommand
54
+ use SucceedCommand
55
+ use StringGenerator
56
+ end
57
+
58
+ class ConfigurableChunkyBaconProcessor < ComposedCommands::ComposedCommand
59
+ use StringGenerator
60
+ use StringMultiplier, multiplicator: 2, separator: '|'
61
+ end
@@ -0,0 +1,91 @@
1
+ require 'composed_commands'
2
+ require 'fixtures/commans'
3
+
4
+ # This file was generated by the `rspec --init` command. Conventionally, all
5
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
6
+ # The generated `.rspec` file contains `--require spec_helper` which will cause this
7
+ # file to always be loaded, without a need to explicitly require it in any files.
8
+ #
9
+ # Given that it is always loaded, you are encouraged to keep this file as
10
+ # light-weight as possible. Requiring heavyweight dependencies from this file
11
+ # will add to the boot time of your test suite on EVERY test run, even for an
12
+ # individual file that may not need all of that loaded. Instead, consider making
13
+ # a separate helper file that requires the additional dependencies and performs
14
+ # the additional setup, and require it from the spec files that actually need it.
15
+ #
16
+ # The `.rspec` file also contains a few flags that are not defaults but that
17
+ # users commonly want.
18
+ #
19
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
20
+ RSpec.configure do |config|
21
+ # rspec-expectations config goes here. You can use an alternate
22
+ # assertion/expectation library such as wrong or the stdlib/minitest
23
+ # assertions if you prefer.
24
+ config.expect_with :rspec do |expectations|
25
+ # This option will default to `true` in RSpec 4. It makes the `description`
26
+ # and `failure_message` of custom matchers include text for helper methods
27
+ # defined using `chain`, e.g.:
28
+ # be_bigger_than(2).and_smaller_than(4).description
29
+ # # => "be bigger than 2 and smaller than 4"
30
+ # ...rather than:
31
+ # # => "be bigger than 2"
32
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
33
+ end
34
+
35
+ # rspec-mocks config goes here. You can use an alternate test double
36
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
37
+ config.mock_with :rspec do |mocks|
38
+ # Prevents you from mocking or stubbing a method that does not exist on
39
+ # a real object. This is generally recommended, and will default to
40
+ # `true` in RSpec 4.
41
+ mocks.verify_partial_doubles = true
42
+ end
43
+
44
+ # The settings below are suggested to provide a good initial experience
45
+ # with RSpec, but feel free to customize to your heart's content.
46
+
47
+ # These two settings work together to allow you to limit a spec run
48
+ # to individual examples or groups you care about by tagging them with
49
+ # `:focus` metadata. When nothing is tagged with `:focus`, all examples
50
+ # get run.
51
+ config.filter_run :focus
52
+ config.run_all_when_everything_filtered = true
53
+
54
+ # Limits the available syntax to the non-monkey patched syntax that is recommended.
55
+ # For more details, see:
56
+ # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
57
+ # - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
58
+ # - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
59
+ config.disable_monkey_patching!
60
+
61
+ # This setting enables warnings. It's recommended, but in some cases may
62
+ # be too noisy due to issues in dependencies.
63
+ config.warnings = true
64
+
65
+ # Many RSpec users commonly either run the entire suite or an individual
66
+ # file, and it's useful to allow more verbose output when running an
67
+ # individual spec file.
68
+ if config.files_to_run.one?
69
+ # Use the documentation formatter for detailed output,
70
+ # unless a formatter has already been configured
71
+ # (e.g. via a command-line flag).
72
+ config.default_formatter = 'doc'
73
+ end
74
+
75
+ # Print the 10 slowest examples and example groups at the
76
+ # end of the spec run, to help surface which specs are running
77
+ # particularly slow.
78
+ config.profile_examples = 10
79
+
80
+ # Run specs in random order to surface order dependencies. If you find an
81
+ # order dependency and want to debug it, you can fix the order by providing
82
+ # the seed, which is printed after each run.
83
+ # --seed 1234
84
+ config.order = :random
85
+
86
+ # Seed global randomization in this process using the `--seed` CLI option.
87
+ # Setting this allows you to use `--seed` to deterministically reproduce
88
+ # test failures related to randomization by passing the same `--seed` value
89
+ # as the one that triggered the failure.
90
+ Kernel.srand config.seed
91
+ end
metadata ADDED
@@ -0,0 +1,153 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: composed_commands
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Tema Bolshakov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-10-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '4'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '4'
27
+ - !ruby/object:Gem::Dependency
28
+ name: workflow
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.1'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.1'
41
+ - !ruby/object:Gem::Dependency
42
+ name: virtus
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1'
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.7'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.7'
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: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3.1'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3.1'
97
+ description: Tool for creating commands and commands chains.
98
+ email:
99
+ - abolshakov@spbtv.com
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
+ - composed_commands.gemspec
111
+ - lib/composed_commands.rb
112
+ - lib/composed_commands/command.rb
113
+ - lib/composed_commands/command/execution.rb
114
+ - lib/composed_commands/command/state.rb
115
+ - lib/composed_commands/composed_command.rb
116
+ - lib/composed_commands/version.rb
117
+ - spec/compound_commands/command_attributes_spec.rb
118
+ - spec/compound_commands/command_spec.rb
119
+ - spec/compound_commands/compound_command_attributes_spec.rb
120
+ - spec/compound_commands/compound_command_spec.rb
121
+ - spec/fixtures/commans.rb
122
+ - spec/spec_helper.rb
123
+ homepage: ''
124
+ licenses:
125
+ - Apache License, Version 2.0
126
+ metadata: {}
127
+ post_install_message:
128
+ rdoc_options: []
129
+ require_paths:
130
+ - lib
131
+ required_ruby_version: !ruby/object:Gem::Requirement
132
+ requirements:
133
+ - - ">="
134
+ - !ruby/object:Gem::Version
135
+ version: '0'
136
+ required_rubygems_version: !ruby/object:Gem::Requirement
137
+ requirements:
138
+ - - ">="
139
+ - !ruby/object:Gem::Version
140
+ version: '0'
141
+ requirements: []
142
+ rubyforge_project:
143
+ rubygems_version: 2.4.1
144
+ signing_key:
145
+ specification_version: 4
146
+ summary: Tool for creating commands and commands chains.
147
+ test_files:
148
+ - spec/compound_commands/command_attributes_spec.rb
149
+ - spec/compound_commands/command_spec.rb
150
+ - spec/compound_commands/compound_command_attributes_spec.rb
151
+ - spec/compound_commands/compound_command_spec.rb
152
+ - spec/fixtures/commans.rb
153
+ - spec/spec_helper.rb