seam 0.0.15 → 0.0.16

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -132,6 +132,17 @@ The history is available through:
132
132
  effort.history
133
133
  ````
134
134
 
135
+ ####Waiting####
136
+
137
+ Seam comes with a default worker for waiting. It can be defined by calling "wait" on a flow, like this.
138
+
139
+ ````
140
+ flow = Seam::Flow.new
141
+ flow.send_order_to_warehouse
142
+ flow.wait 2.days
143
+ flow.check_if_the_order_has_been_fulfilled
144
+ ````
145
+
135
146
  ## Installation
136
147
 
137
148
  Add this line to your application's Gemfile:
data/lib/seam/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Seam
2
- VERSION = "0.0.15"
2
+ VERSION = "0.0.16"
3
3
  end
@@ -0,0 +1,25 @@
1
+ module Seam
2
+
3
+ class WaitWorker < ::Seam::Worker
4
+
5
+ def initialize
6
+ handles :wait
7
+ end
8
+
9
+ def process
10
+ move_to_next_step( { on: the_time_to_move_on } )
11
+ end
12
+
13
+ private
14
+
15
+ def the_time_to_move_on
16
+ Time.now + the_amount_of_time_to_wait
17
+ end
18
+
19
+ def the_amount_of_time_to_wait
20
+ current_step[:arguments][0]
21
+ end
22
+
23
+ end
24
+
25
+ end
data/lib/seam/worker.rb CHANGED
@@ -19,8 +19,9 @@ module Seam
19
19
  @operation_to_execute = :eject
20
20
  end
21
21
 
22
- def move_to_next_step
22
+ def move_to_next_step(options = {})
23
23
  @operation_to_execute = :move_to_next_step
24
+ operation_args[:next_execute_at] = (options[:on] || Time.now)
24
25
  end
25
26
 
26
27
  def try_again_in seconds
@@ -53,6 +54,8 @@ module Seam
53
54
  effort.next_execute_at = operation_args[:time]
54
55
  end,
55
56
  move_to_next_step: -> do
57
+ effort.next_execute_at = operation_args[:next_execute_at] if operation_args[:next_execute_at]
58
+
56
59
  effort.completed_steps << effort.next_step
57
60
 
58
61
  steps = effort.flow['steps'].map { |x| x['name'] }
@@ -60,6 +63,7 @@ module Seam
60
63
 
61
64
  effort.next_step = next_step
62
65
  mark_effort_as_complete if next_step.nil?
66
+
63
67
  end,
64
68
  eject: -> { mark_effort_as_complete }
65
69
  }
data/lib/seam.rb CHANGED
@@ -2,6 +2,7 @@ require 'active_support/all'
2
2
  require 'active_support/time'
3
3
  require 'securerandom'
4
4
  require 'json'
5
+ require_relative 'seam/worker'
5
6
  Dir[File.dirname(__FILE__) + '/seam/*.rb'].each {|file| require file }
6
7
 
7
8
  module Seam
@@ -0,0 +1,65 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ class DoSomething < Seam::Worker
4
+ def process
5
+ end
6
+ end
7
+
8
+ describe Seam::WaitWorker do
9
+
10
+ it "should be a worker" do
11
+ Seam::WaitWorker.new.is_a? Seam::Worker
12
+ end
13
+
14
+ it "should handle the wait step" do
15
+ Seam::WaitWorker.new.step.must_equal 'wait'
16
+ end
17
+
18
+ describe "using it in a flow" do
19
+
20
+ let(:effort_id) do
21
+ effort = flow.start
22
+ effort.id
23
+ end
24
+
25
+ let(:today) do
26
+ Time.parse '1/1/2011'
27
+ end
28
+
29
+ [:length_of_time].to_objects { [
30
+ [3.days],
31
+ [1.day],
32
+ [30.minutes]
33
+ ] }.each do |test|
34
+
35
+ describe "a simple situation" do
36
+
37
+ let(:flow) do
38
+ f = Seam::Flow.new
39
+ f.wait test.length_of_time
40
+ f.do_something
41
+ f
42
+ end
43
+
44
+ before do
45
+ Timecop.freeze today
46
+ effort_id
47
+ end
48
+
49
+ it "should move to the next step" do
50
+ Seam::WaitWorker.new.execute_all
51
+ Seam::Effort.find(effort_id).next_step.must_equal "do_something"
52
+ end
53
+ j
54
+ it "should set the next execute date" do
55
+ Seam::WaitWorker.new.execute_all
56
+ Seam::Effort.find(effort_id).next_execute_at.must_equal (today + test.length_of_time)
57
+ end
58
+
59
+ end
60
+
61
+ end
62
+
63
+ end
64
+
65
+ end
@@ -20,26 +20,82 @@ describe "worker" do
20
20
  end
21
21
 
22
22
  describe "move_to_next_step" do
23
- it "should work" do
24
- flow = Seam::Flow.new
25
- flow.apple
26
- flow.orange
27
23
 
28
- effort = flow.start( { first_name: 'John' } )
29
- effort = Seam::Effort.find(effort.id)
24
+ [:date].to_objects {[
25
+ ['1/1/2011'],
26
+ ['3/4/2015']
27
+ ]}.each do |test|
28
+
29
+ describe "move immediately" do
30
30
 
31
- effort.next_step.must_equal "apple"
31
+ before { Timecop.freeze Time.parse(test.date) }
32
+ after { Timecop.return }
33
+
34
+ it "should move to the next step and set the date to now" do
35
+ flow = Seam::Flow.new
36
+ flow.apple
37
+ flow.orange
38
+
39
+ effort = flow.start( { first_name: 'John' } )
40
+ effort = Seam::Effort.find(effort.id)
41
+
42
+ effort.next_step.must_equal "apple"
43
+
44
+ apple_worker = Seam::Worker.new
45
+ apple_worker.handles(:apple)
46
+ def apple_worker.process
47
+ move_to_next_step
48
+ end
49
+
50
+ apple_worker.execute effort
51
+
52
+ effort = Seam::Effort.find(effort.id)
53
+ effort.next_step.must_equal "orange"
54
+ effort.next_execute_at.must_equal Time.parse(test.date)
55
+ end
32
56
 
33
- apple_worker = Seam::Worker.new
34
- apple_worker.handles(:apple)
35
- def apple_worker.process
36
- move_to_next_step
37
57
  end
38
58
 
39
- apple_worker.execute effort
59
+ end
60
+
61
+ [:date, :next_date].to_objects {[
62
+ ['1/1/2011', '2/1/2011'],
63
+ ['1/1/2011', '2/2/2011'],
64
+ ['3/4/2015', '4/5/2016']
65
+ ]}.each do |test|
66
+
67
+ describe "move to some point in the future" do
68
+
69
+ before { Timecop.freeze Time.parse(test.date) }
70
+ after { Timecop.return }
71
+
72
+ it "should move to the next step and set the date to now" do
73
+ flow = Seam::Flow.new
74
+ flow.apple
75
+ flow.orange
76
+
77
+ effort = flow.start( { first_name: 'John' } )
78
+ effort = Seam::Effort.find(effort.id)
79
+
80
+ effort.next_step.must_equal "apple"
81
+
82
+ apple_worker = Seam::Worker.new
83
+ apple_worker.handles(:apple)
84
+ eval("
85
+ def apple_worker.process
86
+ move_to_next_step( { on: Time.parse('#{test.next_date}') } )
87
+ end
88
+ ")
89
+
90
+ apple_worker.execute effort
91
+
92
+ effort = Seam::Effort.find(effort.id)
93
+ effort.next_step.must_equal "orange"
94
+ effort.next_execute_at.must_equal Time.parse(test.next_date)
95
+ end
96
+
97
+ end
40
98
 
41
- effort = Seam::Effort.find(effort.id)
42
- effort.next_step.must_equal "orange"
43
99
  end
44
100
  end
45
101
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: seam
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.15
4
+ version: 0.0.16
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-09-16 00:00:00.000000000 Z
12
+ date: 2013-11-22 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: json
@@ -206,11 +206,13 @@ files:
206
206
  - lib/seam/persistence.rb
207
207
  - lib/seam/step.rb
208
208
  - lib/seam/version.rb
209
+ - lib/seam/wait_worker.rb
209
210
  - lib/seam/worker.rb
210
211
  - seam.gemspec
211
212
  - spec/seam/effort_spec.rb
212
213
  - spec/seam/flow_spec.rb
213
214
  - spec/seam/step_spec.rb
215
+ - spec/seam/wait_worker_spec.rb
214
216
  - spec/seam/worker_spec.rb
215
217
  - spec/spec_helper.rb
216
218
  homepage: ''
@@ -228,7 +230,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
228
230
  version: '0'
229
231
  segments:
230
232
  - 0
231
- hash: -3794428632218759846
233
+ hash: -4363898252696775104
232
234
  required_rubygems_version: !ruby/object:Gem::Requirement
233
235
  none: false
234
236
  requirements:
@@ -237,7 +239,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
237
239
  version: '0'
238
240
  segments:
239
241
  - 0
240
- hash: -3794428632218759846
242
+ hash: -4363898252696775104
241
243
  requirements: []
242
244
  rubyforge_project:
243
245
  rubygems_version: 1.8.25
@@ -248,5 +250,6 @@ test_files:
248
250
  - spec/seam/effort_spec.rb
249
251
  - spec/seam/flow_spec.rb
250
252
  - spec/seam/step_spec.rb
253
+ - spec/seam/wait_worker_spec.rb
251
254
  - spec/seam/worker_spec.rb
252
255
  - spec/spec_helper.rb