big_machine 1.1.1 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (40) hide show
  1. checksums.yaml +7 -0
  2. data/README.md +38 -4
  3. data/Rakefile +4 -33
  4. data/lib/big_machine.rb +4 -4
  5. data/lib/big_machine/available_methods.rb +9 -0
  6. data/lib/big_machine/lock.rb +4 -0
  7. data/lib/big_machine/state.rb +4 -0
  8. data/lib/big_machine/version.rb +1 -1
  9. data/test/big_machine_test.rb +19 -19
  10. data/test/test_helper.rb +2 -15
  11. metadata +32 -78
  12. data/lib/big_machine/transition_methods.rb +0 -9
  13. data/test/dummy/README.rdoc +0 -261
  14. data/test/dummy/Rakefile +0 -7
  15. data/test/dummy/app/assets/javascripts/application.js +0 -15
  16. data/test/dummy/app/assets/stylesheets/application.css +0 -13
  17. data/test/dummy/app/controllers/application_controller.rb +0 -3
  18. data/test/dummy/app/helpers/application_helper.rb +0 -2
  19. data/test/dummy/app/views/layouts/application.html.erb +0 -14
  20. data/test/dummy/config.ru +0 -4
  21. data/test/dummy/config/application.rb +0 -65
  22. data/test/dummy/config/boot.rb +0 -10
  23. data/test/dummy/config/environment.rb +0 -5
  24. data/test/dummy/config/environments/development.rb +0 -31
  25. data/test/dummy/config/environments/production.rb +0 -64
  26. data/test/dummy/config/environments/test.rb +0 -35
  27. data/test/dummy/config/initializers/backtrace_silencers.rb +0 -7
  28. data/test/dummy/config/initializers/inflections.rb +0 -15
  29. data/test/dummy/config/initializers/mime_types.rb +0 -5
  30. data/test/dummy/config/initializers/secret_token.rb +0 -7
  31. data/test/dummy/config/initializers/session_store.rb +0 -8
  32. data/test/dummy/config/initializers/wrap_parameters.rb +0 -10
  33. data/test/dummy/config/locales/en.yml +0 -5
  34. data/test/dummy/config/routes.rb +0 -58
  35. data/test/dummy/log/test.log +0 -0
  36. data/test/dummy/public/404.html +0 -26
  37. data/test/dummy/public/422.html +0 -26
  38. data/test/dummy/public/500.html +0 -25
  39. data/test/dummy/public/favicon.ico +0 -0
  40. data/test/dummy/script/rails +0 -6
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 649fb8587b32627fa3ca4eff827629c607dd6938
4
+ data.tar.gz: c18ecd4816fef1e342ec3bc735b8f3f07b74d78d
5
+ SHA512:
6
+ metadata.gz: 95f53b5a31fb1b42693bbb99c04007f3368a3ebadc8f293bb0c4ff29a332ca978d3e9e13697c35518bda641c71e897de5177ee6f0f44d38df9d3176603cbf7ee
7
+ data.tar.gz: bc40ad97a54d263ed57a0ae76ed1b37ade44246a10bbd44dae63ac260d04713bab07b329b71a2428c7ebae95c7b044032ea3036252feeb4fb5222411ed431883
data/README.md CHANGED
@@ -27,13 +27,16 @@ Source
27
27
 
28
28
  Add this to your Gemfile
29
29
  ``` ruby
30
+
30
31
  gem 'big_machine'
32
+
31
33
  ```
32
34
 
33
35
  ## Usage
34
36
 
35
- Create your first states
37
+ Create your first state
36
38
  ``` ruby
39
+
37
40
  class Draft < BigMachine::State
38
41
  def publish
39
42
  transition_to Online
@@ -42,23 +45,45 @@ Create your first states
42
45
 
43
46
  class Online < BigMachine::State
44
47
  end
48
+
49
+ ```
50
+
51
+ All methods you override in you state can takes args and block
52
+ ``` ruby
53
+
54
+ class Draft < BigMachine::State
55
+ def publish(now = Time.now)
56
+ transition_to Online, now
57
+ end
58
+ end
59
+
60
+ class Online < BigMachine::State
61
+ def enter(*args)
62
+ stateful.publish_at args.first
63
+ end
64
+ end
65
+
45
66
  ```
46
67
 
47
68
  Make your object stateful
48
69
  ``` ruby
70
+
49
71
  class Car
50
72
  include BigMachine
51
73
 
52
74
  big_machine initial_state: :draft
53
75
  end
76
+
54
77
  ```
55
78
 
56
79
  Now it's possible to publish your car object:
57
80
  ``` ruby
81
+
58
82
  car = Car.new
59
83
  car.current_state # => Draft
60
84
  car.publish
61
85
  car.current_state # => Online
86
+
62
87
  ```
63
88
 
64
89
  Of course your object can be an ActiveRecord::Base object. In this case, the object must have a state column. ( if not, see the next section )
@@ -71,20 +96,23 @@ big_machine method can take several options:
71
96
  * workflow if you want to change the normal worklow
72
97
  It's possible to call workflow_is method in your different states
73
98
 
74
- ###example
99
+
100
+ ## Example
75
101
 
76
102
  ``` ruby
103
+
77
104
  big_machine initial_state: :dradt, state_attribute: :big_state, workflow: small
78
105
 
79
106
  class Draft < BigMachine::State
80
107
 
81
108
  def publish
82
109
  return if workflow_is :small
83
-
110
+
84
111
  transition_to Online
85
112
  end
86
113
 
87
114
  end
115
+
88
116
  ```
89
117
 
90
118
  ## Lock module
@@ -93,6 +121,7 @@ A state can include lock module, it will lock the state of your object when your
93
121
  The unlock method should be call to unlock the module
94
122
 
95
123
  ``` ruby
124
+
96
125
  class Draft < BigMachine::State
97
126
  include BigMachine::Lock
98
127
 
@@ -114,8 +143,13 @@ The unlock method should be call to unlock the module
114
143
  article.unlock
115
144
  article.publish
116
145
  article.current_state # => Online
146
+
117
147
  ```
118
148
 
119
149
  ## Contributors
120
150
 
121
- *Anthony Laibe
151
+ *Anthony Laibe
152
+
153
+
154
+ [![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/alaibe/big_machine/trend.png)](https://bitdeli.com/free "Bitdeli Badge")
155
+
data/Rakefile CHANGED
@@ -1,38 +1,9 @@
1
- #!/usr/bin/env rake
2
- begin
3
- require 'bundler/setup'
4
- rescue LoadError
5
- puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
- end
7
- begin
8
- require 'rdoc/task'
9
- rescue LoadError
10
- require 'rdoc/rdoc'
11
- require 'rake/rdoctask'
12
- RDoc::Task = Rake::RDocTask
13
- end
14
-
15
- RDoc::Task.new(:rdoc) do |rdoc|
16
- rdoc.rdoc_dir = 'rdoc'
17
- rdoc.title = 'BigMachine'
18
- rdoc.options << '--line-numbers'
19
- rdoc.rdoc_files.include('README.rdoc')
20
- rdoc.rdoc_files.include('lib/**/*.rb')
21
- end
22
-
23
-
24
-
25
-
26
- Bundler::GemHelper.install_tasks
27
-
28
1
  require 'rake/testtask'
29
2
 
30
- Rake::TestTask.new(:test) do |t|
31
- t.libs << 'lib'
32
- t.libs << 'test'
33
- t.pattern = 'test/**/*_test.rb'
34
- t.verbose = false
3
+ Rake::TestTask.new do |t|
4
+ t.libs << "test"
5
+ t.test_files = FileList['test/*test.rb']
6
+ t.verbose = true
35
7
  end
36
8
 
37
-
38
9
  task :default => :test
@@ -1,8 +1,8 @@
1
- require "active_support"
1
+ require "active_support/all"
2
2
  require "forwardable"
3
3
  require "big_machine/state"
4
4
  require "big_machine/active_record"
5
- require "big_machine/transition_methods"
5
+ require "big_machine/available_methods"
6
6
  require "big_machine/lock"
7
7
 
8
8
  module BigMachine
@@ -34,7 +34,7 @@ module BigMachine
34
34
 
35
35
  def set_initial_state_class
36
36
  @initial_state_class = self.initial_state.to_s.camelize.constantize
37
- include ::BigMachine::TransitionMethods
37
+ include ::BigMachine::AvailableMethods
38
38
  end
39
39
  end
40
40
 
@@ -52,7 +52,7 @@ module BigMachine
52
52
 
53
53
  def forward_current_state
54
54
  extend SingleForwardable
55
- def_delegators :current_state, *current_state.class.transition_methods
55
+ def_delegators :current_state, *current_state.class.available_methods
56
56
  end
57
57
 
58
58
  def transition_to(next_state_class, *args, &block)
@@ -0,0 +1,9 @@
1
+ module BigMachine
2
+ module AvailableMethods
3
+ extend Forwardable
4
+
5
+ def self.included(base)
6
+ def_delegators :current_state, *base.initial_state_class.available_methods
7
+ end
8
+ end
9
+ end
@@ -9,6 +9,10 @@ module BigMachine
9
9
  def transition_methods
10
10
  public_instance_methods - State.public_instance_methods - [:unlock, :locked?]
11
11
  end
12
+
13
+ def available_methods
14
+ public_instance_methods - State.public_instance_methods
15
+ end
12
16
  end
13
17
 
14
18
  def locked?
@@ -11,6 +11,10 @@ module BigMachine
11
11
  public_instance_methods - State.public_instance_methods
12
12
  end
13
13
 
14
+ def self.available_methods
15
+ public_instance_methods - State.public_instance_methods
16
+ end
17
+
14
18
  def self.human_name
15
19
  self.to_s.split('::').last.underscore
16
20
  end
@@ -1,3 +1,3 @@
1
1
  module BigMachine
2
- VERSION = "1.1.1"
2
+ VERSION = "1.2.0"
3
3
  end
@@ -104,88 +104,88 @@ class DummyWithActiveRecordAndOtherState < ActiveRecord::Base
104
104
  end
105
105
  end
106
106
 
107
- class BigMachineTest < ActiveSupport::TestCase
108
- setup do
107
+ class BigMachineTest < Test::Unit::TestCase
108
+ def setup
109
109
  @dummy = DummyMachine.new
110
110
  @dummyAR = DummyWithActiveRecord.new('Online')
111
111
  end
112
112
 
113
- test "big_machine set initial state" do
113
+ def test_big_machine_set_initial_state
114
114
  assert_equal 'Draft', @dummy.current_state.class.name
115
115
  end
116
116
 
117
- test "big_machine create new state when start transition" do
117
+ def test_big_machine_create_new_state_when_start_transition
118
118
  @dummy.publish
119
119
  assert_equal 'Online', @dummy.current_state.class.name
120
120
  @dummy.back_to_draft
121
121
  assert_equal 'Draft', @dummy.current_state.class.name
122
122
  end
123
123
 
124
- test "big machine must refuse unavailable action" do
124
+ def test_big_machine_must_refuse_unavailable_action
125
125
  @dummy.publish
126
126
  assert_raise NoMethodError do
127
127
  @dummy.publish
128
128
  end
129
129
  end
130
130
 
131
- test "big machine can lock state" do
131
+ def test_big_machine_can_lock_state
132
132
  @dummy.lock
133
- assert @dummy.current_state.locked?
133
+ assert @dummy.locked?
134
134
  @dummy.back_to_draft
135
135
  assert_equal 'LockState', @dummy.current_state.class.name
136
- assert @dummy.current_state.locked?
137
- @dummy.current_state.unlock
138
- assert !@dummy.current_state.locked?
136
+ assert @dummy.locked?
137
+ @dummy.unlock
138
+ assert !@dummy.locked?
139
139
  assert_equal 'LockState', @dummy.current_state.class.name
140
140
  @dummy.back_to_draft
141
141
  assert_equal 'Draft', @dummy.current_state.class.name
142
142
  end
143
143
 
144
- test "big_machine can have workflow to refuse some action" do
144
+ def test_big_machine_can_have_workflow_to_refuse_some_action
145
145
  @dummyW = DummyWithWorkflow.new
146
146
  @dummyW.publish
147
147
  assert_equal 'Draft', @dummyW.current_state.class.name
148
148
  end
149
149
 
150
- test "big_machine read state from database" do
150
+ def test_big_machine_read_state_from_database
151
151
  assert_equal 'Online', @dummyAR.current_state.class.name
152
152
  end
153
153
 
154
- test "big_machine read state from specify column in database" do
154
+ def test_big_machine_read_state_from_specify_column_in_database
155
155
  @dummyAROS = DummyWithActiveRecordAndOtherState.new
156
156
  assert_equal 'Online', @dummyAROS.current_state.class.name
157
157
  end
158
158
 
159
- test "big_machine must update attribute when state change" do
159
+ def test_big_machine_must_update_attribute_when_state_change
160
160
  @dummyAR.back_to_draft
161
161
  assert_equal 'Draft', @dummyAR.current_state.class.name
162
162
  assert_equal 'Draft', @dummyAR.state
163
163
  end
164
164
 
165
- test "big_machine must set initial state even if active record object" do
165
+ def test_big_machine_must_set_initial_state_even_if_active_record_object
166
166
  @dummyWS = DummyWithActiveRecord.new(nil)
167
167
  assert_equal 'Draft', @dummyWS.current_state.class.name
168
168
  end
169
169
 
170
- test "big machine does not transtion if it's not possible to exit" do
170
+ def test_big_machine_does_not_transtion_if_it_s_not_possible_to_exit
171
171
  @dummyCE = DummyWithActiveRecord.new('CannotExit')
172
172
  @dummyCE.publish
173
173
  assert_equal 'CannotExit', @dummyCE.current_state.class.name
174
174
  end
175
175
 
176
- test "big machine does not transtion if it's not possible to enter" do
176
+ def test_big_machine_does_not_transtion_if_it_s_not_possible_to_enter
177
177
  @dummyCE = DummyWithActiveRecord.new('Draft')
178
178
  @dummyCE.cannot_enter
179
179
  assert_equal 'Draft', @dummyCE.current_state.class.name
180
180
  end
181
181
 
182
- test "big machine can take args into transition" do
182
+ def test_big_machine_can_take_args_into_transition
183
183
  @dummyArgs = DummyWithActiveRecord.new('Args')
184
184
  @dummyArgs.publish
185
185
  assert_equal 'args', @dummyArgs.args
186
186
  end
187
187
 
188
- test "big machine can take block into transition" do
188
+ def test_big_machine_can_take_block_into_transition
189
189
  @dummyArgs = DummyWithActiveRecord.new('Args')
190
190
  @dummyArgs.back_to_draft
191
191
  assert_equal 'block', @dummyArgs.block
@@ -1,18 +1,5 @@
1
- # Configure Rails Environment
2
- ENV["RAILS_ENV"] = "test"
3
-
4
- require File.expand_path("../dummy/config/environment.rb", __FILE__)
5
- require "rails/test_help"
6
-
7
- Rails.backtrace_cleaner.remove_silencers!
8
-
9
- # Load support files
10
- Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
11
-
12
- # Load fixtures from the engine
13
- if ActiveSupport::TestCase.method_defined?(:fixture_path=)
14
- ActiveSupport::TestCase.fixture_path = File.expand_path("../fixtures", __FILE__)
15
- end
1
+ require 'big_machine'
2
+ require 'test/unit'
16
3
 
17
4
  module ActiveRecord
18
5
  class Base
metadata CHANGED
@@ -1,32 +1,43 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: big_machine
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.1
5
- prerelease:
4
+ version: 1.2.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Anthony Laibe
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2012-08-20 00:00:00.000000000 Z
11
+ date: 2014-03-05 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: activesupport
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
- - - ~>
17
+ - - "~>"
20
18
  - !ruby/object:Gem::Version
21
- version: 3.2.8
19
+ version: 4.0.3
22
20
  type: :runtime
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
- - - ~>
24
+ - - "~>"
28
25
  - !ruby/object:Gem::Version
29
- version: 3.2.8
26
+ version: 4.0.3
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
30
41
  description: Add support for creating state machine on any ruby object
31
42
  email:
32
43
  - anthony.laibe@gmail.com
@@ -34,98 +45,41 @@ executables: []
34
45
  extensions: []
35
46
  extra_rdoc_files: []
36
47
  files:
48
+ - MIT-LICENSE
49
+ - README.md
50
+ - Rakefile
37
51
  - lib/big_machine.rb
38
- - lib/big_machine/version.rb
39
52
  - lib/big_machine/active_record.rb
53
+ - lib/big_machine/available_methods.rb
40
54
  - lib/big_machine/lock.rb
41
55
  - lib/big_machine/state.rb
42
- - lib/big_machine/transition_methods.rb
56
+ - lib/big_machine/version.rb
43
57
  - lib/tasks/big_machine_tasks.rake
44
- - MIT-LICENSE
45
- - Rakefile
46
- - README.md
47
- - test/dummy/README.rdoc
48
- - test/dummy/log/test.log
49
- - test/dummy/config/environments/development.rb
50
- - test/dummy/config/environments/production.rb
51
- - test/dummy/config/environments/test.rb
52
- - test/dummy/config/initializers/wrap_parameters.rb
53
- - test/dummy/config/initializers/session_store.rb
54
- - test/dummy/config/initializers/inflections.rb
55
- - test/dummy/config/initializers/backtrace_silencers.rb
56
- - test/dummy/config/initializers/secret_token.rb
57
- - test/dummy/config/initializers/mime_types.rb
58
- - test/dummy/config/boot.rb
59
- - test/dummy/config/application.rb
60
- - test/dummy/config/environment.rb
61
- - test/dummy/config/routes.rb
62
- - test/dummy/config/locales/en.yml
63
- - test/dummy/Rakefile
64
- - test/dummy/public/422.html
65
- - test/dummy/public/favicon.ico
66
- - test/dummy/public/404.html
67
- - test/dummy/public/500.html
68
- - test/dummy/app/helpers/application_helper.rb
69
- - test/dummy/app/controllers/application_controller.rb
70
- - test/dummy/app/views/layouts/application.html.erb
71
- - test/dummy/app/assets/stylesheets/application.css
72
- - test/dummy/app/assets/javascripts/application.js
73
- - test/dummy/script/rails
74
- - test/dummy/config.ru
75
- - test/test_helper.rb
76
58
  - test/big_machine_test.rb
59
+ - test/test_helper.rb
77
60
  homepage: https://github.com/alaibe/big_machine
78
61
  licenses: []
62
+ metadata: {}
79
63
  post_install_message:
80
64
  rdoc_options: []
81
65
  require_paths:
82
66
  - lib
83
67
  required_ruby_version: !ruby/object:Gem::Requirement
84
- none: false
85
68
  requirements:
86
- - - ! '>='
69
+ - - ">="
87
70
  - !ruby/object:Gem::Version
88
71
  version: '0'
89
72
  required_rubygems_version: !ruby/object:Gem::Requirement
90
- none: false
91
73
  requirements:
92
- - - ! '>='
74
+ - - ">="
93
75
  - !ruby/object:Gem::Version
94
76
  version: '0'
95
77
  requirements: []
96
78
  rubyforge_project:
97
- rubygems_version: 1.8.23
79
+ rubygems_version: 2.2.1
98
80
  signing_key:
99
- specification_version: 3
81
+ specification_version: 4
100
82
  summary: State machine for any ruby object
101
83
  test_files:
102
- - test/dummy/README.rdoc
103
- - test/dummy/log/test.log
104
- - test/dummy/config/environments/development.rb
105
- - test/dummy/config/environments/production.rb
106
- - test/dummy/config/environments/test.rb
107
- - test/dummy/config/initializers/wrap_parameters.rb
108
- - test/dummy/config/initializers/session_store.rb
109
- - test/dummy/config/initializers/inflections.rb
110
- - test/dummy/config/initializers/backtrace_silencers.rb
111
- - test/dummy/config/initializers/secret_token.rb
112
- - test/dummy/config/initializers/mime_types.rb
113
- - test/dummy/config/boot.rb
114
- - test/dummy/config/application.rb
115
- - test/dummy/config/environment.rb
116
- - test/dummy/config/routes.rb
117
- - test/dummy/config/locales/en.yml
118
- - test/dummy/Rakefile
119
- - test/dummy/public/422.html
120
- - test/dummy/public/favicon.ico
121
- - test/dummy/public/404.html
122
- - test/dummy/public/500.html
123
- - test/dummy/app/helpers/application_helper.rb
124
- - test/dummy/app/controllers/application_controller.rb
125
- - test/dummy/app/views/layouts/application.html.erb
126
- - test/dummy/app/assets/stylesheets/application.css
127
- - test/dummy/app/assets/javascripts/application.js
128
- - test/dummy/script/rails
129
- - test/dummy/config.ru
130
- - test/test_helper.rb
131
84
  - test/big_machine_test.rb
85
+ - test/test_helper.rb