pipa-statelogic 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +40 -6
- data/Rakefile +7 -6
- data/lib/statelogic/activerecord.rb +5 -5
- data/lib/statelogic/callbacks_ext.rb +1 -0
- metadata +1 -1
data/README.rdoc
CHANGED
@@ -1,13 +1,47 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
=== Introduction
|
2
|
+
Hi there!
|
3
3
|
|
4
|
-
|
4
|
+
=== Features
|
5
|
+
- State transitions validation.
|
6
|
+
- State-scoped validations.
|
7
|
+
- State-scoped lifecycle callbacks (before|after_save, etc)
|
8
|
+
- ???????
|
9
|
+
- PROFIT!!!11
|
10
|
+
As a bonus you get multiple conditions in <tt>:if|:unless => [:one, :two, ...]</tt>
|
11
|
+
callback and validation options (as in edge Rails).
|
5
12
|
|
13
|
+
=== Installation
|
14
|
+
gem install pipa-statelogic --source http://gems.github.com
|
6
15
|
|
7
|
-
|
8
|
-
=======
|
16
|
+
Installable as a plugin too.
|
9
17
|
|
10
|
-
|
18
|
+
=== Bugs & such
|
19
|
+
http://pipa.lighthouseapp.com/projects/22857-statelogic
|
20
|
+
|
21
|
+
=== Example
|
22
|
+
class Order < ActiveRecord::Base
|
23
|
+
...
|
24
|
+
statelogic do
|
25
|
+
# you get methods +unpaid?+ and +was_unpaid?+.
|
26
|
+
# may be more than one initial state.
|
27
|
+
initial_state 'unpaid' do
|
28
|
+
transitions_to 'ready', 'suspended' # won't let you change to wrong states
|
29
|
+
end
|
30
|
+
state 'ready' do # you get +ready?+, +was_ready?+
|
31
|
+
transitions_to 'redeemed', 'suspended'
|
32
|
+
validates_presence_of :txref # scoped validations
|
33
|
+
before_save :prepare_for_plucking # scoped callbacks
|
34
|
+
end
|
35
|
+
state 'redeemed' do # likewise
|
36
|
+
transitions_to 'suspended'
|
37
|
+
validates_presence_of :txref, :redeemed_at, :facility_id # scoped validations
|
38
|
+
end
|
39
|
+
state 'suspended' do # you guess
|
40
|
+
transitions_to 'unpaid', 'ready', 'redeemed'
|
41
|
+
end
|
42
|
+
end
|
43
|
+
...
|
44
|
+
end
|
11
45
|
|
12
46
|
|
13
47
|
Copyright (c) 2009 Igor Gunko, released under the MIT license
|
data/Rakefile
CHANGED
@@ -5,7 +5,7 @@ require 'rake/rdoctask'
|
|
5
5
|
desc 'Default: run unit tests.'
|
6
6
|
task :default => :test
|
7
7
|
|
8
|
-
desc 'Test
|
8
|
+
desc 'Test Statelogic'
|
9
9
|
Rake::TestTask.new(:test) do |t|
|
10
10
|
t.libs << 'lib'
|
11
11
|
t.libs << 'test'
|
@@ -13,11 +13,12 @@ Rake::TestTask.new(:test) do |t|
|
|
13
13
|
t.verbose = true
|
14
14
|
end
|
15
15
|
|
16
|
-
desc 'Generate documentation for
|
16
|
+
desc 'Generate documentation for Statelogic.'
|
17
17
|
Rake::RDocTask.new(:rdoc) do |rdoc|
|
18
|
-
|
19
|
-
rdoc.
|
18
|
+
files =['README.rdoc', 'MIT-LICENSE', 'lib/**/*.rb']
|
19
|
+
rdoc.rdoc_files.add(files)
|
20
|
+
rdoc.main = "README.rdoc" # page to start on
|
21
|
+
rdoc.title = "Statelogic Documentation"
|
22
|
+
rdoc.rdoc_dir = 'doc/rdoc' # rdoc output folder
|
20
23
|
rdoc.options << '--line-numbers' << '--inline-source'
|
21
|
-
rdoc.rdoc_files.include('README')
|
22
|
-
rdoc.rdoc_files.include('lib/**/*.rb')
|
23
24
|
end
|
@@ -10,8 +10,8 @@ module Statelogic
|
|
10
10
|
DEFAULT_OPTIONS = {:attribute => :state}.freeze
|
11
11
|
|
12
12
|
class StateScopeHelper
|
13
|
-
CALLBACKS = ::ActiveRecord::Callbacks::CALLBACKS.map(&:to_sym).to_set
|
14
|
-
MACROS_PATTERN = /\Avalidates_
|
13
|
+
CALLBACKS = ::ActiveRecord::Callbacks::CALLBACKS.map(&:to_sym).to_set.freeze
|
14
|
+
MACROS_PATTERN = /\Avalidates_/.freeze
|
15
15
|
|
16
16
|
def initialize(cl, state, config)
|
17
17
|
@class, @state, @config, @conditions = cl, state, config, state.map {|x| :"#{x}?"}
|
@@ -72,9 +72,8 @@ module Statelogic
|
|
72
72
|
|
73
73
|
ConfigHelper.new(self, options).instance_eval(&block)
|
74
74
|
|
75
|
-
initial = options[:initial]
|
76
|
-
validates_inclusion_of attr, :in => initial
|
77
|
-
#validates_inclusion_of attr, :in => options[:states].to_set
|
75
|
+
initial = options[:initial] || options[:states]
|
76
|
+
validates_inclusion_of attr, :in => initial, :on => :create unless initial.blank?
|
78
77
|
|
79
78
|
const = attr.to_s.pluralize.upcase
|
80
79
|
const_set(const, options[:states].freeze.each(&:freeze)) unless const_defined?(const)
|
@@ -83,6 +82,7 @@ module Statelogic
|
|
83
82
|
end
|
84
83
|
end
|
85
84
|
|
85
|
+
# :stopdoc:
|
86
86
|
class ActiveRecord::Base
|
87
87
|
include Statelogic::ActiveRecord
|
88
88
|
end
|