bpmachine 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source :rubygems
2
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,31 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ bpmachine (1.0.3)
5
+ activesupport (~> 3.2.0)
6
+ i18n (~> 0.6.0)
7
+
8
+ GEM
9
+ remote: http://rubygems.org/
10
+ specs:
11
+ activesupport (3.2.3)
12
+ i18n (~> 0.6)
13
+ multi_json (~> 1.0)
14
+ diff-lcs (1.1.2)
15
+ i18n (0.6.0)
16
+ multi_json (1.3.2)
17
+ rspec (2.5.0)
18
+ rspec-core (~> 2.5.0)
19
+ rspec-expectations (~> 2.5.0)
20
+ rspec-mocks (~> 2.5.0)
21
+ rspec-core (2.5.1)
22
+ rspec-expectations (2.5.0)
23
+ diff-lcs (~> 1.1.2)
24
+ rspec-mocks (2.5.0)
25
+
26
+ PLATFORMS
27
+ ruby
28
+
29
+ DEPENDENCIES
30
+ bpmachine!
31
+ rspec
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 fabiokung
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,18 @@
1
+ = bpmachine
2
+
3
+ Description goes here.
4
+
5
+ == Note on Patches/Pull Requests
6
+
7
+ * Fork the project.
8
+ * Make your feature addition or bug fix.
9
+ * Add tests for it. This is important so I don't break it in a
10
+ future version unintentionally.
11
+ * Commit, do not mess with rakefile, version, or history.
12
+ (if you want to have your own version, that is fine but
13
+ bump version in a commit by itself I can ignore when I pull)
14
+ * Send me a pull request. Bonus points for topic branches.
15
+
16
+ == Copyright
17
+
18
+ Copyright (c) 2009 fabiokung. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require "rspec/core/rake_task"
5
+ RSpec::Core::RakeTask.new
6
+
7
+ require 'cucumber/rake/task'
8
+ Cucumber::Rake::Task.new(:features)
data/bpmachine.gemspec ADDED
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "bpmachine/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "bpmachine"
7
+ s.version = BPMachine::Version::STRING
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Cloud Team"]
10
+ s.email = ["willian.molinari@locaweb.com.br"]
11
+ s.homepage = "http://github.com/locaweb/bpmachine"
12
+ s.description = %Q{Includes a DSL for business process specification. The process state is persistent, which allows it to be be resumed if an error occurs.}
13
+ s.summary = s.description
14
+
15
+ s.files = Dir["./**/*"].reject {|file| file =~ /\.git|pkg/}
16
+ s.require_paths = ["lib"]
17
+
18
+ s.add_dependency "i18n", "~> 0.6.0"
19
+ s.add_dependency "activesupport", "~> 3.2.0"
20
+ s.add_development_dependency "rspec"
21
+ end
@@ -0,0 +1,135 @@
1
+ module BPMachine
2
+ module ProcessSpecification
3
+ def self.after_processes(&block)
4
+ after_process_actions << block
5
+ end
6
+
7
+ def self.after_process_actions
8
+ @after_process_actions ||= []
9
+ end
10
+
11
+ def self.included(klass)
12
+ klass.extend ClassMethods
13
+ end
14
+
15
+ def change_status(new_status)
16
+ self.status = new_status
17
+ self.save!
18
+ end
19
+
20
+ def read_status
21
+ status.is_a?(Symbol) ? status : status.downcase.to_sym
22
+ end
23
+
24
+ private
25
+ def execute_transitions_from(specification)
26
+ while true
27
+ state = read_status
28
+ transition = specification.transition_for state
29
+ return state if transition.nil?
30
+ return state unless (transition[:if].nil? || self.send(transition[:if]))
31
+ self.send transition[:method]
32
+ change_status transition[:target]
33
+ end
34
+ end
35
+
36
+ def execute_global_after_actions
37
+ ProcessSpecification.after_process_actions.each do |action|
38
+ action.call(self)
39
+ end
40
+ end
41
+
42
+ module ClassMethods
43
+ def process(options = {}, &block)
44
+ name = options[:of].to_sym
45
+ load_step_definitions_for(name)
46
+
47
+ specification = transitions_from block
48
+ class_eval do
49
+ define_method(name) do
50
+ state = read_status
51
+ self.send(specification.before_action) unless specification.before_action.nil?
52
+ raise InvalidInitialState.new(name, specification.pre_condition, state) unless specification.applies_to? state
53
+ execute_transitions_from specification
54
+ self.send(specification.after_action) unless specification.after_action.nil?
55
+ execute_global_after_actions
56
+ end
57
+ end
58
+ end
59
+
60
+ private
61
+ def load_step_definitions_for(process_name)
62
+ begin
63
+ require "#{process_name}_steps"
64
+ begin
65
+ module_name = "#{process_name.to_s.camelize}Steps"
66
+ steps_module = const_get(module_name)
67
+ include(steps_module)
68
+ rescue NameError
69
+ Kernel.puts "WARNING: Error while trying to load the #{module_name} module, because it doesn't exist."
70
+ end
71
+ rescue LoadError
72
+ nil
73
+ end
74
+ end
75
+
76
+ def transitions_from(block)
77
+ specification = SpecificationContext.new
78
+ specification.instance_eval(&block)
79
+ specification
80
+ end
81
+
82
+ class SpecificationContext
83
+ attr_reader :pre_condition, :before_action, :after_action
84
+
85
+ def initialize
86
+ @states = {}
87
+ @accepted_states = {}
88
+ end
89
+
90
+ def transition_for(state)
91
+ @states[state] || @states[@accepted_states[state]]
92
+ end
93
+
94
+ def applies_to?(state)
95
+ return true if @pre_condition.nil?
96
+ @pre_condition == state || has_state?(state) || accept_state?(state)
97
+ end
98
+
99
+ def has_state?(state)
100
+ not @states[state].nil?
101
+ end
102
+
103
+ def accept_state?(state)
104
+ not @accepted_states[state].nil?
105
+ end
106
+
107
+ private
108
+ def before(action)
109
+ @before_action = action.to_sym
110
+ end
111
+
112
+ def after(action)
113
+ @after_action = action.to_sym
114
+ end
115
+
116
+ def must_be(state)
117
+ @pre_condition = state.to_sym
118
+ end
119
+
120
+ def accept_state(states, opts)
121
+ [states].flatten.each {|state| @accepted_states[state] = opts[:as]}
122
+ end
123
+
124
+ def transition(name, options)
125
+ origin = options[:from].to_sym
126
+ target = options[:to].to_sym
127
+ condition = options[:if].to_sym unless options[:if].nil?
128
+ @states[origin] = { :target => target, :method => name, :if => condition }
129
+ end
130
+ end
131
+ end
132
+ end
133
+ end
134
+
135
+ ProcessSpecification = BPMachine::ProcessSpecification
@@ -0,0 +1,8 @@
1
+ module BPMachine
2
+ module Version
3
+ MAJOR = 1
4
+ MINOR = 1
5
+ PATCH = 0
6
+ STRING = "#{MAJOR}.#{MINOR}.#{PATCH}"
7
+ end
8
+ end
data/lib/bpmachine.rb ADDED
@@ -0,0 +1,14 @@
1
+ require 'active_support/core_ext/string/inflections'
2
+ require 'bpmachine/process_specification'
3
+
4
+ class InvalidInitialState < Exception
5
+ def initialize(name, expected_status, current_status)
6
+ @name = name
7
+ @expected_status = expected_status
8
+ @current_status = current_status
9
+ end
10
+
11
+ def message
12
+ "Process #{@name} requires object to have initial status #{@expected_status} or any transitional status, but it is #{@current_status}"
13
+ end
14
+ end
@@ -0,0 +1,248 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "the DSL for business process" do
4
+ describe "automatic module loading" do
5
+ it "should require a file named <process_name>_steps.rb if exists" do
6
+ Object.should be_const_defined(:UninstallSteps)
7
+ end
8
+
9
+ it "should include the steps definition module named <Process>Steps in the process declaring class" do
10
+ Machine.included_modules.should include(UninstallSteps)
11
+ end
12
+
13
+ it "should ignore step definition files that doesn't exist" do
14
+ declaration = lambda do
15
+ class ClassWithProcess
16
+ include ProcessSpecification
17
+ process :of => :anything do
18
+ transition :some_event, :from => :initial, :to => :final
19
+ end
20
+ end
21
+ end
22
+ declaration.should_not raise_error
23
+ end
24
+
25
+ it "should ignore step definition files that doesn't exist" do
26
+ declaration = lambda do
27
+ class ClassWithWrongProcess
28
+ include ProcessSpecification
29
+ process :of => :wrong do
30
+ transition :some_event, :from => :initial, :to => :final
31
+ end
32
+ end
33
+ end
34
+ Kernel.should_receive(:puts).with(/WrongSteps/)
35
+ declaration.should_not raise_error
36
+ end
37
+
38
+ end
39
+
40
+ it "should execute all transitions described in the process" do
41
+ machine = Machine.new
42
+ machine.status = :deactivated
43
+ machine.should_receive(:machine_exists?).and_return true
44
+ machine.should_receive :remove_disks
45
+ machine.should_receive :destroy_vm
46
+ machine.should_receive :erase_data
47
+
48
+ machine.uninstall
49
+ machine.status.should == :uninstalled
50
+ end
51
+
52
+ it "should not change state if condition fails" do
53
+ machine = Machine.new
54
+ machine.status = :deactivated
55
+ machine.should_receive(:machine_exists?).and_return false
56
+
57
+ machine.uninstall
58
+ machine.status = :deactivated
59
+ end
60
+
61
+ it "should stop execution when a transition fail" do
62
+ machine = Machine.new
63
+ machine.status = :deactivated
64
+ machine.should_receive(:machine_exists?).and_return true
65
+ machine.should_receive :remove_disks
66
+ machine.should_receive(:destroy_vm).and_raise("execution fail")
67
+
68
+ lambda { machine.uninstall }.should raise_error("execution fail")
69
+ machine.status.should == :diskless
70
+ end
71
+
72
+ it "should require the initial state" do
73
+ machine = Machine.new
74
+ machine.status = :activated
75
+
76
+ lambda { machine.uninstall }.should raise_error(InvalidInitialState,
77
+ "Process uninstall requires object to have initial status deactivated or any transitional status, but it is activated")
78
+ end
79
+
80
+ it "should accept other states as determined state" do
81
+ machine = Machine.new
82
+ machine.status = :activated
83
+
84
+ machine.should_receive(:create_disks)
85
+ machine.install
86
+ end
87
+
88
+ it "should not accept unexpected stated" do
89
+ machine = Machine.new
90
+ machine.status = :deactivated
91
+ lambda { machine.install }.should raise_error(InvalidInitialState,
92
+ "Process install requires object to have initial status initial_status or any transitional status, but it is deactivated")
93
+ end
94
+
95
+ it "should execute subprocess" do
96
+ machine = Machine.new
97
+ machine.status = :ready_for_subprocess
98
+ machine.should_receive(:step1).ordered
99
+ machine.should_receive(:step2).ordered
100
+ machine.should_receive(:step3).ordered
101
+ machine.execute_with_subprocess
102
+ machine.status.should == :all_done
103
+ end
104
+
105
+ it "should resume a process stopped in a subprocess" do
106
+ machine = Machine.new
107
+ machine.status = :step1_done
108
+ machine.should_not_receive(:step1)
109
+ machine.should_receive(:step2).ordered
110
+ machine.should_receive(:step3).ordered
111
+ machine.execute_with_subprocess
112
+ machine.status.should == :all_done
113
+ end
114
+
115
+ it "should allow the process to resume from a transitional state" do
116
+ machine = Machine.new
117
+ machine.status = :diskless
118
+ machine.should_not_receive(:remove_disks)
119
+ machine.should_receive(:destroy_vm).ordered
120
+ machine.should_receive(:erase_data).ordered
121
+ machine.uninstall
122
+ machine.status.should == :uninstalled
123
+ end
124
+
125
+ it "should be case insensitive with status" do
126
+ machine = Machine.new
127
+ machine.status = :deactivated
128
+ machine.should_receive(:machine_exists?).ordered.and_return true
129
+ machine.should_receive(:remove_disks).ordered
130
+ machine.should_receive(:destroy_vm).ordered
131
+ machine.should_receive(:erase_data).ordered
132
+
133
+ machine.uninstall
134
+ machine.status.should == :uninstalled
135
+ end
136
+
137
+ it "should support custom code to be run before process start" do
138
+ class ProcessWithBeforeAction
139
+ include ProcessSpecification
140
+
141
+ attr_accessor :status
142
+
143
+ process :of => :anything do
144
+ before :do_action
145
+ must_be :initial
146
+ transition :some_event, :from => :initial, :to => :final
147
+ end
148
+
149
+ def save!
150
+ end
151
+ end
152
+
153
+ process = ProcessWithBeforeAction.new
154
+ process.status = :initial
155
+ process.should_receive(:do_action).ordered
156
+ process.should_receive(:some_event).ordered
157
+ process.anything
158
+ end
159
+
160
+ it "should support custom code to be run after process start" do
161
+ class ProcessWithBeforeAction
162
+ include ProcessSpecification
163
+
164
+ process :of => :anything do
165
+ after :do_action
166
+ must_be :initial
167
+ transition :some_event, :from => :initial, :to => :final
168
+ end
169
+ end
170
+
171
+ process = ProcessWithBeforeAction.new
172
+ process.status = :initial
173
+ process.should_receive(:some_event).ordered
174
+ process.should_receive(:do_action).ordered
175
+ process.anything
176
+ end
177
+
178
+ it "should accept any initial state if there isn't a must_be rule" do
179
+ class InitialStateNotRequired
180
+ include ProcessSpecification
181
+
182
+ attr_accessor :status
183
+
184
+ process :of => :anything do
185
+ transition :some_event, :from => :initial, :to => :final
186
+ transition :other_event, :from => :other, :to => :final
187
+ end
188
+
189
+ def save!
190
+ end
191
+ end
192
+
193
+ process = InitialStateNotRequired.new
194
+ process.status = :initial
195
+ process.should_receive :some_event
196
+ process.anything
197
+ process.status.should == :final
198
+
199
+ second_process = InitialStateNotRequired.new
200
+ second_process.status = :other
201
+ second_process.should_receive :other_event
202
+ second_process.anything
203
+ second_process.status.should == :final
204
+ end
205
+
206
+ it "should accept global 'after' blocks, passing the object processing the flow" do
207
+ machine = Machine.new
208
+
209
+ called = false
210
+ ProcessSpecification.after_processes do |process_object|
211
+ called = true
212
+ process_object.should be(machine)
213
+ end
214
+
215
+ machine.status = :deactivated
216
+ machine.should_receive(:machine_exists?).and_return true
217
+ machine.should_receive :remove_disks
218
+ machine.should_receive :destroy_vm
219
+ machine.should_receive :erase_data
220
+ machine.uninstall
221
+
222
+ called.should be_true
223
+ end
224
+
225
+ it "should raise error when model can't be saved" do
226
+ class TheProcess
227
+ include ProcessSpecification
228
+
229
+ attr_accessor :status
230
+
231
+ process :of => :anything do
232
+ transition :some_event, :from => :initial, :to => :final
233
+ transition :other_event, :from => :other, :to => :final
234
+ end
235
+
236
+ def save!
237
+ raise "Big Error!"
238
+ end
239
+ end
240
+
241
+ process = TheProcess.new
242
+ process.status = :initial
243
+ process.stub!(:some_event)
244
+ process.stub!(:other_event)
245
+ lambda { process.anything }.should raise_error("Big Error!")
246
+ end
247
+
248
+ end
@@ -0,0 +1,3 @@
1
+ require "bpmachine"
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__) + "/support")
3
+ require "machine"
@@ -0,0 +1,59 @@
1
+ class Machine
2
+ include ProcessSpecification
3
+
4
+ attr_accessor :status
5
+
6
+ process :of => :uninstall do
7
+ must_be :deactivated
8
+
9
+ transition :remove_disks,
10
+ :from => :deactivated,
11
+ :to => :diskless,
12
+ :if => :machine_exists?
13
+
14
+ transition :destroy_vm,
15
+ :from => :diskless,
16
+ :to => :vm_destroyed
17
+
18
+ transition :erase_data,
19
+ :from => :vm_destroyed,
20
+ :to => :uninstalled
21
+ end
22
+
23
+ process :of => :install do
24
+ must_be :initial_status
25
+ accept_state [:activated, :installed], :as => :initial_status
26
+
27
+ transition :create_disks,
28
+ :from => :initial_status,
29
+ :to => :diskless
30
+ end
31
+
32
+ process :of => :subprocess do
33
+ must_be :ready_for_subprocess
34
+
35
+ transition :step1,
36
+ :from => :ready_for_subprocess,
37
+ :to => :step1_done
38
+
39
+ transition :step2,
40
+ :from => :step1_done,
41
+ :to => :subprocess_done
42
+ end
43
+
44
+ process :of => :execute_with_subprocess do
45
+ must_be :ready_for_subprocess
46
+ accept_state :step1_done, :as => :ready_for_subprocess
47
+
48
+ transition :subprocess,
49
+ :from => :ready_for_subprocess,
50
+ :to => :subprocess_done
51
+
52
+ transition :step3,
53
+ :from => :subprocess_done,
54
+ :to => :all_done
55
+ end
56
+
57
+ def save!
58
+ end
59
+ end
@@ -0,0 +1,2 @@
1
+ module UninstallSteps
2
+ end
@@ -0,0 +1,2 @@
1
+ module WrongNameSteps
2
+ end
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bpmachine
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Cloud Team
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-04-25 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: i18n
16
+ requirement: &10268920 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 0.6.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *10268920
25
+ - !ruby/object:Gem::Dependency
26
+ name: activesupport
27
+ requirement: &10268140 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: 3.2.0
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *10268140
36
+ - !ruby/object:Gem::Dependency
37
+ name: rspec
38
+ requirement: &10267500 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *10267500
47
+ description: Includes a DSL for business process specification. The process state
48
+ is persistent, which allows it to be be resumed if an error occurs.
49
+ email:
50
+ - willian.molinari@locaweb.com.br
51
+ executables: []
52
+ extensions: []
53
+ extra_rdoc_files: []
54
+ files:
55
+ - ./Gemfile.lock
56
+ - ./bpmachine.gemspec
57
+ - ./README.rdoc
58
+ - ./LICENSE
59
+ - ./spec/support/machine.rb
60
+ - ./spec/support/uninstall_steps.rb
61
+ - ./spec/support/wrong_steps.rb
62
+ - ./spec/spec_helper.rb
63
+ - ./spec/bpmachine_spec.rb
64
+ - ./Gemfile
65
+ - ./Rakefile
66
+ - ./lib/bpmachine.rb
67
+ - ./lib/bpmachine/process_specification.rb
68
+ - ./lib/bpmachine/version.rb
69
+ homepage: http://github.com/locaweb/bpmachine
70
+ licenses: []
71
+ post_install_message:
72
+ rdoc_options: []
73
+ require_paths:
74
+ - lib
75
+ required_ruby_version: !ruby/object:Gem::Requirement
76
+ none: false
77
+ requirements:
78
+ - - ! '>='
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ required_rubygems_version: !ruby/object:Gem::Requirement
82
+ none: false
83
+ requirements:
84
+ - - ! '>='
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ requirements: []
88
+ rubyforge_project:
89
+ rubygems_version: 1.8.16
90
+ signing_key:
91
+ specification_version: 3
92
+ summary: Includes a DSL for business process specification. The process state is persistent,
93
+ which allows it to be be resumed if an error occurs.
94
+ test_files: []