alter-ego-mongoid 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/Manifest ADDED
@@ -0,0 +1,9 @@
1
+ Manifest
2
+ README.rdoc
3
+ Rakefile
4
+ alter-ego-mongoid.gemspec
5
+ init.rb
6
+ lib/alter_ego/mongoid_adapter.rb
7
+ test/adapter_test.rb
8
+ test/helper.rb
9
+ test/traffic_signal.rb
data/README.rdoc ADDED
@@ -0,0 +1,37 @@
1
+ = AlterEgo-Mongoid
2
+
3
+ == Motivation
4
+ AlterEgo is my favorite Ruby state machine for the following reasons:
5
+ 1. It's not dependent on Mongoid - it can be used on plain Ruby objects.
6
+ 2. It most closely follows the GOF State Pattern because it allows for
7
+ polymorphic behavior based on state.
8
+
9
+ Out of the box, AlterEgo doesn't play nicely with Mongoid because it
10
+ stores state in <tt>@state</tt>, whereas Mongoid::Document
11
+ persists with setter methods.
12
+
13
+ This mixin overrides the AlterEgo's accessor methods for state to allow it
14
+ to be properly persisted to a database, as well as serialized/unserialized as
15
+ json, yml, and xml.
16
+
17
+ == Installation
18
+ === As a Ruby Gem
19
+ gem install alter-ego-mongoid
20
+ === OR as a Rails plugin
21
+ script/plugin install git://github.com/wink/alter-ego-mongoid.git
22
+
23
+ == Usage
24
+ This plugin automatically adds a string field to your model called "state".
25
+
26
+ # you'll only need the following two lines if you're NOT using
27
+ # alter-ego-mongoid as a plugin
28
+ gem 'alter-ego-mongoid'
29
+ require 'alter_ego/mongoid_adapter'
30
+
31
+ class Example
32
+ include Mongoid::Document
33
+ include AlterEgo # include this first
34
+ include AlterEgo::MongoidAdapter
35
+
36
+ # Your code here
37
+ end
data/Rakefile ADDED
@@ -0,0 +1,18 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'echoe'
4
+
5
+ Echoe.new('alter-ego-mongoid', '0.1.2') do |p|
6
+ p.description = "Adapter to allow Mongoid to persist and restore state of objects using the AlterEgo state machine, ported from the alter-ego-activerecord gem by Paul Hieromnimon"
7
+ p.author = "Micah Winkelspecht"
8
+ p.email = "winkelspecht@gmail.com"
9
+ p.url = "http://github.com/wink/alter-ego-mongoid"
10
+ p.runtime_dependencies = ["mongoid >=2.0.0.rc.6", "bson_ext ~>1.2", "alter-ego >=1.0.1" ]
11
+ end
12
+
13
+ require 'rake/testtask'
14
+ Rake::TestTask.new(:test) do |test|
15
+ test.libs << 'lib' << 'test'
16
+ test.pattern = 'test/**/test_*.rb'
17
+ test.verbose = true
18
+ end
@@ -0,0 +1,40 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{alter-ego-mongoid}
5
+ s.version = "0.1.2"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Micah Winkelspecht"]
9
+ s.date = %q{2011-01-30}
10
+ s.description = %q{Adapter to allow Mongoid to persist and restore state of objects using the AlterEgo state machine, ported from the alter-ego-activerecord gem by Paul Hieromnimon}
11
+ s.email = %q{winkelspecht@gmail.com}
12
+ s.extra_rdoc_files = ["README.rdoc", "lib/alter_ego/mongoid_adapter.rb"]
13
+ s.files = ["Manifest", "README.rdoc", "Rakefile", "alter-ego-mongoid.gemspec", "init.rb", "lib/alter_ego/mongoid_adapter.rb", "test/adapter_test.rb", "test/helper.rb", "test/traffic_signal.rb"]
14
+ s.homepage = %q{http://github.com/wink/alter-ego-mongoid}
15
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Alter-ego-mongoid", "--main", "README.rdoc"]
16
+ s.require_paths = ["lib"]
17
+ s.rubyforge_project = %q{alter-ego-mongoid}
18
+ s.rubygems_version = %q{1.3.7}
19
+ s.summary = %q{Adapter to allow Mongoid to persist and restore state of objects using the AlterEgo state machine, ported from the alter-ego-activerecord gem by Paul Hieromnimon}
20
+ s.test_files = ["test/adapter_test.rb"]
21
+
22
+ if s.respond_to? :specification_version then
23
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
24
+ s.specification_version = 3
25
+
26
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
27
+ s.add_runtime_dependency(%q<mongoid>, [">= 2.0.0.rc.6"])
28
+ s.add_runtime_dependency(%q<bson_ext>, ["~> 1.2"])
29
+ s.add_runtime_dependency(%q<alter-ego>, [">= 1.0.1"])
30
+ else
31
+ s.add_dependency(%q<mongoid>, [">= 2.0.0.rc.6"])
32
+ s.add_dependency(%q<bson_ext>, ["~> 1.2"])
33
+ s.add_dependency(%q<alter-ego>, [">= 1.0.1"])
34
+ end
35
+ else
36
+ s.add_dependency(%q<mongoid>, [">= 2.0.0.rc.6"])
37
+ s.add_dependency(%q<bson_ext>, ["~> 1.2"])
38
+ s.add_dependency(%q<alter-ego>, [">= 1.0.1"])
39
+ end
40
+ end
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'alter_ego/mongoid_adapter'
@@ -0,0 +1,31 @@
1
+ module AlterEgo
2
+ module MongoidAdapter
3
+
4
+ def self.included klass
5
+ klass.field :state
6
+ klass.after_initialize do |r|
7
+ r.state = r.state
8
+ end
9
+ end
10
+
11
+ # # rails needs this method to be defined for the
12
+ # # class method after_initialize to be able to take a block
13
+ # # this is because of some performance optimization
14
+ # def after_initialize; end
15
+
16
+ # Override methods from AlterEgo to store state
17
+ # in @attributes["state"] instead of @state
18
+ def state=(identifier)
19
+ # state needs to always be stored as a string - symbol serialization is strange
20
+ self[:state] = identifier.to_s unless identifier.nil?
21
+ end
22
+
23
+ def state
24
+ result = self[:state] || self.class.default_state
25
+ result = result.to_sym unless result.nil?
26
+ assert(result.nil? || self.class.states.keys.include?(result))
27
+ result
28
+ end
29
+
30
+ end
31
+ end
@@ -0,0 +1,70 @@
1
+ require 'helper'
2
+
3
+ class AdapterTest < Test::Unit::TestCase
4
+
5
+ def test_state_stored_in_attributes
6
+ t = TrafficSignal.new
7
+ assert_not_nil t.state
8
+ t.state = :caution
9
+ assert_equal :caution, t.state
10
+ end
11
+
12
+ def test_state_stored_as_string
13
+ t = TrafficSignal.new
14
+ assert_equal String, t[:state].class
15
+ end
16
+
17
+ def test_state_returned_as_symbol
18
+ t = TrafficSignal.new
19
+ assert_equal Symbol, t.state.class
20
+ end
21
+
22
+ def test_state_is_persisted_and_restored
23
+ t = TrafficSignal.new
24
+ old_state = t.state
25
+ t.cycle!
26
+ new_state = t.state
27
+
28
+ #sanity check
29
+ assert_not_equal old_state, new_state
30
+
31
+ t.save!
32
+ t = t.reload
33
+
34
+ assert_equal t.state, new_state
35
+ end
36
+
37
+ def test_new_object_has_default_state
38
+ t=TrafficSignal.new
39
+ assert_equal t.state, TrafficSignal.default_state
40
+ assert_equal t[:state].to_sym, TrafficSignal.default_state
41
+ end
42
+
43
+ def test_yaml
44
+ require 'yaml'
45
+ t= TrafficSignal.new
46
+ t.cycle!
47
+ yaml = YAML::dump t
48
+ t2 = YAML::load yaml
49
+ assert_equal t.state, t2.state
50
+ end
51
+
52
+ def test_json
53
+ t = TrafficSignal.new
54
+ t.cycle!
55
+ json = t.to_json
56
+ t2 = TrafficSignal.new
57
+ t2.from_json json
58
+ assert_equal t.state, t2.state
59
+ end
60
+
61
+ def test_xml
62
+ t = TrafficSignal.new
63
+ t.cycle!
64
+ xml = t.to_xml
65
+ t2 = TrafficSignal.new
66
+ t2.from_xml xml
67
+ assert_equal t.state, t2.state
68
+ end
69
+
70
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,16 @@
1
+ require 'test/unit'
2
+ require 'rubygems'
3
+ gem 'mongoid'
4
+ gem 'bson_ext'
5
+ require 'mongoid'
6
+ gem 'alter-ego'
7
+ require 'alter_ego'
8
+ require 'alter_ego/mongoid_adapter'
9
+ require 'traffic_signal'
10
+
11
+ Mongoid.configure do |config|
12
+ name = "alter-ego-mongoid-test"
13
+ host = "localhost"
14
+ config.master = Mongo::Connection.new.db(name)
15
+ config.persist_in_safe_mode = false
16
+ end
@@ -0,0 +1,30 @@
1
+ class TrafficSignal
2
+ include Mongoid::Document
3
+
4
+ field :state
5
+
6
+ include AlterEgo
7
+ include AlterEgo::MongoidAdapter
8
+
9
+ state :proceed, :default=>true do
10
+ handle :color do
11
+ "green"
12
+ end
13
+ transition :to => :caution, :on => :cycle!
14
+ end
15
+
16
+ state :caution do
17
+ handle :color do
18
+ "yellow"
19
+ end
20
+ transition :to => :stop, :on => :cycle!
21
+ end
22
+
23
+ state :stop do
24
+ handle :color do
25
+ "red"
26
+ end
27
+ transition :to => :proceed, :on => :cycle!
28
+ end
29
+
30
+ end
metadata ADDED
@@ -0,0 +1,130 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: alter-ego-mongoid
3
+ version: !ruby/object:Gem::Version
4
+ hash: 31
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 2
10
+ version: 0.1.2
11
+ platform: ruby
12
+ authors:
13
+ - Micah Winkelspecht
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-01-30 00:00:00 -08:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: mongoid
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 15424089
30
+ segments:
31
+ - 2
32
+ - 0
33
+ - 0
34
+ - rc
35
+ - 6
36
+ version: 2.0.0.rc.6
37
+ type: :runtime
38
+ version_requirements: *id001
39
+ - !ruby/object:Gem::Dependency
40
+ name: bson_ext
41
+ prerelease: false
42
+ requirement: &id002 !ruby/object:Gem::Requirement
43
+ none: false
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ hash: 11
48
+ segments:
49
+ - 1
50
+ - 2
51
+ version: "1.2"
52
+ type: :runtime
53
+ version_requirements: *id002
54
+ - !ruby/object:Gem::Dependency
55
+ name: alter-ego
56
+ prerelease: false
57
+ requirement: &id003 !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ hash: 21
63
+ segments:
64
+ - 1
65
+ - 0
66
+ - 1
67
+ version: 1.0.1
68
+ type: :runtime
69
+ version_requirements: *id003
70
+ description: Adapter to allow Mongoid to persist and restore state of objects using the AlterEgo state machine, ported from the alter-ego-activerecord gem by Paul Hieromnimon
71
+ email: winkelspecht@gmail.com
72
+ executables: []
73
+
74
+ extensions: []
75
+
76
+ extra_rdoc_files:
77
+ - README.rdoc
78
+ - lib/alter_ego/mongoid_adapter.rb
79
+ files:
80
+ - Manifest
81
+ - README.rdoc
82
+ - Rakefile
83
+ - alter-ego-mongoid.gemspec
84
+ - init.rb
85
+ - lib/alter_ego/mongoid_adapter.rb
86
+ - test/adapter_test.rb
87
+ - test/helper.rb
88
+ - test/traffic_signal.rb
89
+ has_rdoc: true
90
+ homepage: http://github.com/wink/alter-ego-mongoid
91
+ licenses: []
92
+
93
+ post_install_message:
94
+ rdoc_options:
95
+ - --line-numbers
96
+ - --inline-source
97
+ - --title
98
+ - Alter-ego-mongoid
99
+ - --main
100
+ - README.rdoc
101
+ require_paths:
102
+ - lib
103
+ required_ruby_version: !ruby/object:Gem::Requirement
104
+ none: false
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ hash: 3
109
+ segments:
110
+ - 0
111
+ version: "0"
112
+ required_rubygems_version: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ hash: 11
118
+ segments:
119
+ - 1
120
+ - 2
121
+ version: "1.2"
122
+ requirements: []
123
+
124
+ rubyforge_project: alter-ego-mongoid
125
+ rubygems_version: 1.3.7
126
+ signing_key:
127
+ specification_version: 3
128
+ summary: Adapter to allow Mongoid to persist and restore state of objects using the AlterEgo state machine, ported from the alter-ego-activerecord gem by Paul Hieromnimon
129
+ test_files:
130
+ - test/adapter_test.rb