simple_states 0.1.0.pre2 → 0.1.0.pre3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -7,28 +7,30 @@ Travis CI.
7
7
 
8
8
  Define states and events like this:
9
9
 
10
- class Foo
11
- include SimpleStates
10
+ ``` ruby
11
+ class Foo
12
+ include SimpleStates
12
13
 
13
- states :created, :started, :finished
14
+ states :created, :started, :finished
14
15
 
15
- event :start, :from => :created, :to => :started, :if => :startable?
16
- event :finish, :to => :finished, :after => :cleanup
16
+ event :start, :from => :created, :to => :started, :if => :startable?
17
+ event :finish, :to => :finished, :after => :cleanup
17
18
 
18
- attr_accessor :state, :started_at, :finished_at
19
+ attr_accessor :state, :started_at, :finished_at
19
20
 
20
- def start
21
- # start foo
22
- end
21
+ def start
22
+ # start foo
23
+ end
23
24
 
24
- def startable?
25
- true
26
- end
25
+ def startable?
26
+ true
27
+ end
27
28
 
28
- def cleanup
29
- # cleanup foo
30
- end
31
- end
29
+ def cleanup
30
+ # cleanup foo
31
+ end
32
+ end
33
+ ```
32
34
 
33
35
  Including the SimpleStates module to your class is currently required. We'll add
34
36
  hooks for ActiveRecord etc later.
@@ -37,24 +39,26 @@ SimpleStates expects your model to support attribute accessors for `:state`.
37
39
 
38
40
  Event options have the following well-known meanings:
39
41
 
40
- :from # valid states to transition from
41
- :to # target state to transition to
42
- :if # only proceed if the given method returns true
43
- :unless # only proceed if the given method returns false
44
- :before # run the given method before running `super` and setting the new state
45
- :after # run the given method at the very end
42
+ ``` ruby
43
+ :from # valid states to transition from
44
+ :to # target state to transition to
45
+ :if # only proceed if the given method returns true
46
+ :unless # only proceed if the given method returns false
47
+ :before # run the given method before running `super` and setting the new state
48
+ :after # run the given method at the very end
49
+ ```
46
50
 
47
51
  All of these options except for `:to` can be given as a single symbol or string or
48
52
  as an Array of symbols or strings.
49
53
 
50
54
  Calling `event` will effectively add methods to a proxy module which is
51
55
  included to the singleton class of your class' instances. E.g. declaring `event
52
- :start` in the example above will add a method `start` to a module included to
53
- the singleton class of instances of `Foo`.
56
+ :start` in the example above will add methods `start` and `start!` to a module
57
+ included to the singleton class of instances of `Foo`.
54
58
 
55
59
  This method will
56
60
 
57
- 1. check if `:if`/`:except` conditions apply (if given) and just return from the method otherwise
61
+ 1. check if `:if`/`:unless` conditions apply (if given) and just return from the method otherwise
58
62
  2. check if the object currently is in a valid `:from` state (if given) and raise an exception otherwise
59
63
  3. run `:before` callbacks (if given)
60
64
  4. call `super` if Foo defines the current method (i.e. call `start` but not `finish` in the example above)
@@ -65,8 +69,10 @@ This method will
65
69
 
66
70
  You can define options for all events like so:
67
71
 
68
- event :finish, :to => :finished, :after => :cleanup
69
- event :all, :after => :notify
72
+ ``` ruby
73
+ event :finish, :to => :finished, :after => :cleanup
74
+ event :all, :after => :notify
75
+ ```
70
76
 
71
77
  This will call :cleanup first and then :notify on :finish.
72
78
 
@@ -78,31 +84,36 @@ way then it will raise an exception.
78
84
  By default SimpleStates will assum `:created` as an initial state. You can
79
85
  overwrite this using:
80
86
 
81
- self.initial_state :something
87
+ ``` ruby
88
+ # note that we have to use self here!
89
+ self.initial_state = :some_state
90
+ ```
82
91
 
83
92
  So with the example above something the following would work:
84
93
 
85
- foo = Foo.new
94
+ ``` ruby
95
+ foo = Foo.new
86
96
 
87
- foo.state # :created
88
- foo.created? # true
89
- foo.was_created? # true
90
- foo.state?(:created) # true
97
+ foo.state # :created
98
+ foo.created? # true
99
+ foo.was_created? # true
100
+ foo.state?(:created) # true
91
101
 
92
- foo.start # checks Foo#startable? and then calls Foo#start
102
+ foo.start # checks Foo#startable? and then calls Foo#start
103
+ # calling foo.start! (with exclamation mark) would perform same actions as foo.start, but
104
+ # also call foo.save! afterwards.
93
105
 
94
- foo.state # :started
95
- foo.started? # true
96
- foo.started_at # Time.now
97
- foo.created? # false
98
- foo.was_created? # true
99
-
100
- foo.finish # just performs state logic as there's no Foo#finish
101
-
102
- foo.state # :finished
103
- foo.finished? # true
104
- foo.finished_at # Time.now
105
- foo.was_created? # true
106
- foo.was_started? # true
106
+ foo.state # :started
107
+ foo.started? # true
108
+ foo.started_at # Time.now
109
+ foo.created? # false
110
+ foo.was_created? # true
107
111
 
112
+ foo.finish # just performs state logic as there's no Foo#finish
108
113
 
114
+ foo.state # :finished
115
+ foo.finished? # true
116
+ foo.finished_at # Time.now
117
+ foo.was_created? # true
118
+ foo.was_started? # true
119
+ ```
data/lib/simple_states.rb CHANGED
@@ -42,7 +42,7 @@ module SimpleStates
42
42
  end
43
43
 
44
44
  def event(name, options = {})
45
- add_states(options[:from], options[:to])
45
+ add_states(*options[:from], options[:to])
46
46
  self.events += [Event.new(name, options)]
47
47
  end
48
48
  end
@@ -1,3 +1,3 @@
1
1
  module SimpleStates
2
- VERSION = '0.1.0.pre2'
2
+ VERSION = '0.1.0.pre3'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_states
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0.pre2
4
+ version: 0.1.0.pre3
5
5
  prerelease: 6
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-12-01 00:00:00.000000000Z
12
+ date: 2012-04-22 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
16
- requirement: &70191004044960 !ruby/object:Gem::Requirement
16
+ requirement: &70262350435840 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70191004044960
24
+ version_requirements: *70262350435840
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: hashr
27
- requirement: &70191004043800 !ruby/object:Gem::Requirement
27
+ requirement: &70262350434940 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: 0.0.10
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *70191004043800
35
+ version_requirements: *70262350434940
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rake
38
- requirement: &70191004042340 !ruby/object:Gem::Requirement
38
+ requirement: &70262350434360 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: 0.9.2
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70191004042340
46
+ version_requirements: *70262350434360
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: test_declarative
49
- requirement: &70191004041700 !ruby/object:Gem::Requirement
49
+ requirement: &70262350433840 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *70191004041700
57
+ version_requirements: *70262350433840
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: mocha
60
- requirement: &70191004041140 !ruby/object:Gem::Requirement
60
+ requirement: &70262350433120 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,7 +65,7 @@ dependencies:
65
65
  version: '0'
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *70191004041140
68
+ version_requirements: *70262350433120
69
69
  description: ! '[description]'
70
70
  email: svenfuchs@artweb-design.de
71
71
  executables: []
@@ -102,9 +102,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
102
102
  version: 1.3.1
103
103
  requirements: []
104
104
  rubyforge_project: ! '[none]'
105
- rubygems_version: 1.8.10
105
+ rubygems_version: 1.8.11
106
106
  signing_key:
107
107
  specification_version: 3
108
108
  summary: ! '[summary]'
109
109
  test_files: []
110
- has_rdoc: