moody 0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data/README +96 -0
  2. data/lib/moody.rb +39 -0
  3. data/moody.gemspec +22 -0
  4. data/test/test_moody.rb +68 -0
  5. metadata +66 -0
data/README ADDED
@@ -0,0 +1,96 @@
1
+ Moody: An implementation of the State Pattern in ruby
2
+ =====================================================
3
+
4
+ This is an object-oriented way to represent Finite State Machines, where an
5
+ object can change its behavior at runtime, depending on its internal state.
6
+
7
+ If you want more information about this pattern, read:
8
+
9
+ http://sourcemaking.com/design_patterns/state
10
+
11
+ For a stupid example of how you would use this, here's a traffic light:
12
+
13
+ class Stop < Moody::State
14
+ def color
15
+ "red"
16
+ end
17
+
18
+ def next
19
+ sleep 3
20
+ switch_to Go
21
+ end
22
+ end
23
+
24
+ class Go < Moody::State
25
+ def color
26
+ "green"
27
+ end
28
+
29
+ def next
30
+ sleep 2
31
+ switch_to Caution
32
+ end
33
+ end
34
+
35
+ class Caution < Moody::State
36
+ def color
37
+ "yellow"
38
+ end
39
+
40
+ def next
41
+ sleep 1
42
+ switch_to Stop
43
+ end
44
+ end
45
+
46
+ class TrafficLight
47
+ extend Moody::Context
48
+
49
+ initial_state Stop
50
+ delegate_to_state :color, :next
51
+ end
52
+
53
+ traffic_light = TrafficLight.new
54
+
55
+ loop do
56
+ puts traffic_light.color
57
+ traffic_light.next
58
+ end
59
+
60
+ Inspiration
61
+ ===========
62
+
63
+ This is a reimplementation of the State Pattern gem by Daniel Cadenas. Check out
64
+ his implementation at http://github.com/dcadenas/state_pattern
65
+
66
+ Contributing
67
+ ============
68
+
69
+ * Send a pull request.
70
+ * Small, atomic commits, please.
71
+ * Make sure to add tests.
72
+ * Bonus points if you add features on topic branches.
73
+
74
+ License
75
+ =======
76
+
77
+ Copyright Nicolás Sanguinetti <hi@nicolassanguinetti.info>
78
+
79
+ Permission is hereby granted, free of charge, to any person obtaining
80
+ a copy of this software and associated documentation files (the
81
+ 'Software'), to deal in the Software without restriction, including
82
+ without limitation the rights to use, copy, modify, merge, publish,
83
+ distribute, sublicense, and/or sell copies of the Software, and to
84
+ permit persons to whom the Software is furnished to do so, subject to
85
+ the following conditions:
86
+
87
+ The above copyright notice and this permission notice shall be
88
+ included in all copies or substantial portions of the Software.
89
+
90
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
91
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
92
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
93
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
94
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
95
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
96
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/lib/moody.rb ADDED
@@ -0,0 +1,39 @@
1
+ module Moody
2
+ module Context
3
+ def self.extended(base)
4
+ base.class_eval do
5
+ attr_accessor :state
6
+ end
7
+ end
8
+
9
+ def initial_state(state)
10
+ module_eval <<-RUBY
11
+ def state
12
+ @state ||= #{state}.new(self)
13
+ end
14
+ RUBY
15
+ end
16
+
17
+ def delegate_to_state(*methods)
18
+ method_definitions = methods.inject("") do |code, method|
19
+ code + <<-RUBY
20
+ def #{method}(*args, &block)
21
+ state.#{method}(*args, &block)
22
+ end
23
+ RUBY
24
+ end
25
+
26
+ module_eval(method_definitions)
27
+ end
28
+ end
29
+
30
+ class State
31
+ def initialize(context)
32
+ @_context = context
33
+ end
34
+
35
+ def switch_to(next_state)
36
+ @_context.state = next_state.new(@_context)
37
+ end
38
+ end
39
+ end
data/moody.gemspec ADDED
@@ -0,0 +1,22 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "moody"
3
+ s.version = "0.1"
4
+ s.date = "2010-10-23"
5
+
6
+ s.description = "Simple and straightforward implementation of the state pattern, inspired by the StatePattern gem"
7
+ s.summary = "State Pattern for your rubies"
8
+ s.homepage = "http://github.com/foca/moody"
9
+
10
+ s.authors = ["Nicolás Sanguinetti"]
11
+ s.email = "contacto@nicolassanguinetti.info"
12
+
13
+ s.require_paths = ["lib"]
14
+ s.has_rdoc = true
15
+
16
+ s.files = %w[
17
+ README
18
+ moody.gemspec
19
+ lib/moody.rb
20
+ test/test_moody.rb
21
+ ]
22
+ end
@@ -0,0 +1,68 @@
1
+ require "test/unit"
2
+ require "moody"
3
+ require "ostruct"
4
+
5
+ class Stop < Moody::State
6
+ def color
7
+ "red"
8
+ end
9
+
10
+ def next
11
+ switch_to Go
12
+ end
13
+ end
14
+
15
+ class Go < Moody::State
16
+ def color
17
+ "green"
18
+ end
19
+
20
+ def next
21
+ switch_to Caution
22
+ end
23
+ end
24
+
25
+ class Caution < Moody::State
26
+ def color
27
+ "yellow"
28
+ end
29
+
30
+ def next
31
+ switch_to Stop
32
+ end
33
+ end
34
+
35
+ class TrafficLight
36
+ extend Moody::Context
37
+
38
+ initial_state Stop
39
+ delegate_to_state :color, :next
40
+ end
41
+
42
+ class TestMoodyByExample < Test::Unit::TestCase
43
+ def setup
44
+ @context = TrafficLight.new
45
+ end
46
+
47
+ def test_initial_state
48
+ assert @context.state.is_a?(Stop)
49
+ end
50
+
51
+ def test_switch_state
52
+ @context.next
53
+ assert @context.state.is_a?(Go)
54
+ @context.next
55
+ assert @context.state.is_a?(Caution)
56
+ @context.next
57
+ assert @context.state.is_a?(Stop)
58
+ end
59
+
60
+ def test_delegate_properties
61
+ @context.next
62
+ assert_equal "green", @context.color
63
+ @context.next
64
+ assert_equal "yellow", @context.color
65
+ @context.next
66
+ assert_equal "red", @context.color
67
+ end
68
+ end
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: moody
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ version: "0.1"
9
+ platform: ruby
10
+ authors:
11
+ - "Nicol\xC3\xA1s Sanguinetti"
12
+ autorequire:
13
+ bindir: bin
14
+ cert_chain: []
15
+
16
+ date: 2010-10-23 00:00:00 -02:00
17
+ default_executable:
18
+ dependencies: []
19
+
20
+ description: Simple and straightforward implementation of the state pattern, inspired by the StatePattern gem
21
+ email: contacto@nicolassanguinetti.info
22
+ executables: []
23
+
24
+ extensions: []
25
+
26
+ extra_rdoc_files: []
27
+
28
+ files:
29
+ - README
30
+ - moody.gemspec
31
+ - lib/moody.rb
32
+ - test/test_moody.rb
33
+ has_rdoc: true
34
+ homepage: http://github.com/foca/moody
35
+ licenses: []
36
+
37
+ post_install_message:
38
+ rdoc_options: []
39
+
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ none: false
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ segments:
48
+ - 0
49
+ version: "0"
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ segments:
56
+ - 0
57
+ version: "0"
58
+ requirements: []
59
+
60
+ rubyforge_project:
61
+ rubygems_version: 1.3.7
62
+ signing_key:
63
+ specification_version: 3
64
+ summary: State Pattern for your rubies
65
+ test_files: []
66
+