stateful 0.0.1 → 0.0.2

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 85ae4d99568569f667a41d4b1959cc511f300dea
4
- data.tar.gz: af2c257fdfcedf21ea5b760537c9d32c004ac74e
3
+ metadata.gz: 9261a6a19bbd004baeacff1e9d6b3d33e3096a8c
4
+ data.tar.gz: 7c4edb733fb8d48880f3cc89538b200c84a69d8c
5
5
  SHA512:
6
- metadata.gz: 8b714fc19974ee231d8baf208c655f835a6c002fa12c984740f0a14a29449a8b3595b7e7297ecf203dc318f077ec49f7ad8c0be396b818d3b915502f5db59255
7
- data.tar.gz: 1b6d4c06b40dad7123e6a6f6c946596ca0f1a1665cff1b0e4fd9f7e68012703def03a97c36417584ccfb8e1ac221f48a84233809907e146a6b49dd966bb1d801
6
+ metadata.gz: 1a2144835b0991988cc9935bdf70891662552774cda5f9f3d124e4ef6887aabee2d542dbee377f871fb76a500241b546e773b62b1a112b74f47da46623f03130
7
+ data.tar.gz: 13aaa3bcbe79a07d9d8ceae4eaf58c4e789351ce981e9c9fe639d119489deffa20fd906fdaa137334545930014351d2cd0d97d2e365090342976e10a010f02a2
data/README.md CHANGED
@@ -40,7 +40,7 @@ class Project
40
40
  active: {
41
41
  new: :published,
42
42
  published: {
43
- needs_approval: [:approved, :duplicate],
43
+ needs_approval: [:approved, :duplicate, :new],
44
44
  approved: :closed
45
45
  }
46
46
  },
@@ -1,3 +1,3 @@
1
1
  module Stateful
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/lib/stateful.rb CHANGED
@@ -8,6 +8,7 @@ module Stateful
8
8
  included do
9
9
  if defined?(Mongoid)
10
10
  require 'mongoid/document'
11
+ require 'stateful/mongoid'
11
12
  include Stateful::Mongoid if included_modules.include?(::Mongoid::Document)
12
13
  end
13
14
  end
@@ -35,25 +36,18 @@ module Stateful
35
36
  define_method 'change_state' do |new_state, options = {}, &block|
36
37
  return false if new_state == state
37
38
  return false unless state_info.can_transition_to?(new_state)
38
-
39
- # convert shortcut event name to options hash
40
- options = {event: options} if options.is_a? Symbol
41
- options[:persist_methods] = [:persist_state, :save]
42
-
43
- _change_state(new_state, options, &block)
39
+ _change_state(new_state, options, [:persist_state, :save], &block)
44
40
  end
45
41
 
46
42
  define_method 'change_state!' do |new_state, options = {}, &block|
47
- return false if new_state == state
48
43
  raise "transition from #{state} to #{new_state} not allowed" unless state_info.can_transition_to?(new_state)
44
+ _change_state(new_state, options, [:persist_state!, :save!], &block)
45
+ end
49
46
 
47
+ define_method '_change_state' do |new_state, options, persist_methods, &block|
50
48
  # convert shortcut event name to options hash
51
49
  options = {event: options} if options.is_a? Symbol
52
- options[:persist_methods] = [:persist_state!, :save!]
53
- _change_state(new_state, options, &block)
54
- end
55
50
 
56
- define_method '_change_state' do |new_state, options, &block|
57
51
  # do a little magic and infer the event name from the method name used to call change_state
58
52
  # TODO: decide if this is too magical, for now it has been commented out.
59
53
  #unless options[:event]
@@ -68,20 +62,23 @@ module Stateful
68
62
  callbacks << options[:event] if options[:event]
69
63
  run_callbacks *callbacks do
70
64
  self.state = new_state
71
- if options[:persist_methods]
72
- method = options[:persist_methods].find {|m| respond_to?(m)}
65
+
66
+ ## if a specific persist method value was provided
67
+ #if options.has_key?(:persist_method)
68
+ # # call the method if one was provided
69
+ # __send__(options[:persist_method]) if options[:persist_method]
70
+ ## if no persist method option was provided than use the defaults
71
+ #else
72
+ method = persist_methods.find {|m| respond_to?(m)}
73
73
  __send__(method) if method
74
- end
75
- if respond_to?(:persist_state)
76
- persist_state
77
- elsif respond_to?(:save!)
78
- save!
79
- end
74
+ #end
80
75
  end
81
76
  true
82
77
  end
83
78
  end
84
79
 
80
+ protected :change_state
81
+ protected :change_state!
85
82
  private :_change_state
86
83
 
87
84
  define_method 'can_transition_to?' do |new_state|
data/spec/mongoid_spec.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require 'spec_helper'
2
2
  require './lib/stateful'
3
3
  require 'mongoid'
4
+ require 'mongoid/document'
4
5
 
5
6
 
6
7
  class Kata
@@ -33,7 +34,7 @@ describe Stateful::Mongoid do
33
34
  end
34
35
 
35
36
  it 'should support can_transition_to?' do
36
- kata.can_transition_to?(:beta).should be_true
37
+ kata.can_transition_to?(:needs_testing).should be_true
37
38
  kata.can_transition_to?(:retired).should be_false
38
39
  end
39
40
 
@@ -91,15 +91,15 @@ describe Kata do
91
91
 
92
92
  context 'change_state' do
93
93
  it 'should raise error when an invalid transition state is provided' do
94
- expect{kata.change_state(:retired)}.to raise_error
94
+ expect{kata.send(:change_state!, :retired)}.to raise_error
95
95
  end
96
96
 
97
97
  it 'should raise error when a group state is provided' do
98
- expect{kata.change_state(:beta)}.to raise_error
98
+ expect{kata.send(:change_state!, :beta)}.to raise_error
99
99
  end
100
100
 
101
101
  it 'should return false when state is the same' do
102
- kata.change_state(:draft).should be_false
102
+ kata.send(:change_state, :draft).should be_false
103
103
  end
104
104
 
105
105
  it 'should support state_valid?' do
@@ -107,14 +107,14 @@ describe Kata do
107
107
  end
108
108
 
109
109
  it 'should change the state when a proper state is provided' do
110
- kata.change_state(:needs_feedback).should be_true
110
+ kata.send(:change_state, :needs_feedback).should be_true
111
111
  kata.state.should == :needs_feedback
112
- kata.change_state(:needs_approval).should be_true
112
+ kata.send(:change_state, :needs_approval).should be_true
113
113
  kata.state.should == :needs_approval
114
- kata.change_state(:draft).should be_true
114
+ kata.send(:change_state, :draft).should be_true
115
115
  kata.state.should == :draft
116
- kata.change_state(:needs_approval).should be_true
117
- kata.change_state(:approved).should be_true
116
+ kata.send(:change_state, :needs_approval).should be_true
117
+ kata.send(:change_state, :approved).should be_true
118
118
  kata.state.should == :approved
119
119
  end
120
120
 
@@ -151,8 +151,6 @@ describe Kata do
151
151
  Kata.state_infos[:needs_approval].to_transitions.should == [:draft, :approved]
152
152
 
153
153
  Kata.state_infos[:retired].to_transitions.should be_empty
154
-
155
- p Kata.instance_methods
156
154
  end
157
155
  end
158
156
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stateful
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - jake hoffner
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-12-17 00:00:00.000000000 Z
11
+ date: 2013-12-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport