super_state 0.1.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/README CHANGED
@@ -44,6 +44,12 @@ a simple multi-state model
44
44
 
45
45
  end
46
46
 
47
+ this is actually already shipped as a magic mixin
48
+
49
+ class MyClass < ActiveRecord::Base
50
+ include SuperState::CommonStates
51
+ end
52
+
47
53
  we can also take arguments in our transitions
48
54
 
49
55
  class Loan < ActiveRecord::Base
data/Rakefile CHANGED
@@ -45,7 +45,7 @@ spec = Gem::Specification.new do |s|
45
45
 
46
46
  # Change these as appropriate
47
47
  s.name = "super_state"
48
- s.version = "0.1.0"
48
+ s.version = "0.2.1"
49
49
  s.summary = "Super Simple State Machine"
50
50
  s.author = "Matthew Rudy Jacobs"
51
51
  s.email = "MatthewRudyJacobs@gmail.com"
@@ -0,0 +1,42 @@
1
+ module SuperState
2
+
3
+ module CommonStates
4
+
5
+ def self.included(klass)
6
+ klass.class_eval do
7
+ include SuperState
8
+
9
+ super_state :pending, :initial => true
10
+ super_state :processing
11
+ super_state :completed
12
+ super_state :failed
13
+
14
+ super_state_group :outstanding, ["pending", "processing"]
15
+
16
+ # first part of a two stage transition
17
+ # eg.
18
+ # def process
19
+ # start_processing!
20
+ # do_the_stuff
21
+ # complete_processing!
22
+ # end
23
+ #
24
+ state_transition :start_processing, :pending => :processing
25
+
26
+ # second part of a two stage transition
27
+ state_transition :complete_processing, :processing => :completed
28
+
29
+ # transition direct from pending to complete
30
+ state_transition :complete, :pending => :completed
31
+
32
+ # failed to process
33
+ state_transition :fail, :processing => :failed
34
+
35
+ # back to processing
36
+ state_transition :restart, :failed => :processing
37
+ end
38
+ end
39
+
40
+ end
41
+
42
+ end
data/lib/super_state.rb CHANGED
@@ -1,3 +1,5 @@
1
+ require 'super_state/common_states'
2
+
1
3
  module SuperState
2
4
 
3
5
  def self.included(klass)
@@ -0,0 +1,92 @@
1
+ require 'test_helper'
2
+
3
+ class CommonStatesTest < Test::Unit::TestCase
4
+
5
+ class MyClass < ActiveRecord::Base
6
+ include SuperState::CommonStates
7
+ end
8
+
9
+ # a DD::Batch is a simple example
10
+ def setup
11
+ @object = MyClass.create!()
12
+ assert @object.pending?
13
+ assert @object.class.ancestors.include?(CommonStates), "mate, it's not a CommonState thing"
14
+ end
15
+
16
+ test "a new record is pending?" do
17
+ @object = MyClass.new
18
+ assert @object.pending?
19
+ end
20
+
21
+ test "start_processing! - raises" do
22
+ assert @object.pending?
23
+ @object.expects(:valid?).returns(false)
24
+
25
+ assert_raise(ActiveRecord::RecordInvalid) do
26
+ @object.start_processing!
27
+ end
28
+ assert !@object.pending? # doesnt roll back
29
+ end
30
+
31
+ test "start_processing - returns false" do
32
+ assert @object.pending?
33
+ @object.expects(:valid?).returns(false)
34
+
35
+ assert_equal false, @object.start_processing
36
+ assert @object.pending? # rolls back
37
+ end
38
+
39
+ test "start_processing!" do
40
+ assert @object.pending?
41
+
42
+ assert @object.start_processing!
43
+
44
+ before_and_after_reload @object do
45
+ assert @object.processing?
46
+ end
47
+ end
48
+
49
+ test "complete_processing!" do
50
+ @object.start_processing!
51
+ assert @object.processing?
52
+
53
+ assert @object.complete_processing!
54
+
55
+ before_and_after_reload @object do
56
+ assert @object.completed?
57
+ end
58
+ end
59
+
60
+ test "fail!" do
61
+ @object.start_processing!
62
+ assert @object.processing?
63
+
64
+ assert @object.fail!
65
+
66
+ before_and_after_reload @object do
67
+ assert @object.failed?
68
+ end
69
+ end
70
+
71
+ test "complete!" do
72
+ assert @object.pending?
73
+
74
+ assert @object.complete!
75
+
76
+ before_and_after_reload @object do
77
+ assert @object.completed?
78
+ end
79
+ end
80
+
81
+ test "outstanding?" do
82
+ assert @object.pending?
83
+ assert @object.outstanding?
84
+
85
+ @object.start_processing!
86
+ assert @object.outstanding?
87
+
88
+ @object.complete_processing!
89
+ assert !@object.outstanding?
90
+ end
91
+
92
+ end
@@ -1,6 +1,6 @@
1
1
  require 'test_helper'
2
2
 
3
- class SuperStateTest < ActiveSupport::TestCase
3
+ class SuperStateTest < Test::Unit::TestCase
4
4
 
5
5
  test "abstract the tests" do
6
6
  puts "this has been abstracted from a live project"
data/test/test_helper.rb CHANGED
@@ -1,3 +1,8 @@
1
1
  require 'rubygems'
2
2
  require 'test/unit'
3
- require 'active_support'
3
+ require 'active_support/testing/declarative'
4
+ $: << File.expand_path(File.dirname(__FILE__)+"/../lib")
5
+
6
+ class Test::Unit::TestCase
7
+ extend ActiveSupport::Testing::Declarative
8
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: super_state
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 21
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
+ - 2
8
9
  - 1
9
- - 0
10
- version: 0.1.0
10
+ version: 0.2.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - Matthew Rudy Jacobs
@@ -31,8 +31,10 @@ files:
31
31
  - MIT-LICENSE
32
32
  - Rakefile
33
33
  - README
34
+ - test/common_states_test.rb
34
35
  - test/super_state_test.rb
35
36
  - test/test_helper.rb
37
+ - lib/super_state/common_states.rb
36
38
  - lib/super_state.rb
37
39
  has_rdoc: true
38
40
  homepage: http://github.com/matthewrudy/super_state