Skip to content
Browse files

oops, updated ages ago but forgot to commit

  • Loading branch information...
1 parent 6596f15 commit 5a28c5eb22a7467a799152959426b08b58c6974a @banister committed Sep 14, 2009
Showing with 89 additions and 18 deletions.
  1. +7 −0 CHANGELOG
  2. +25 −7 lib/stateology.rb
  3. +57 −11 test/test.rb
View
7 CHANGELOG
@@ -1,4 +1,11 @@
+13-6-09
+* version 0.2.1
+* ugh, fixed silly (and rare) bug
+* added alias 'state=' for 'state' instance method
+13-6-09
+* version 0.2.0
+* now supports ruby 1.9.1 and ruby 1.8.6
3-12-08
* nested states now supported
View
32 lib/stateology.rb
@@ -1,6 +1,7 @@
# stateology.rb
# (C) John Mair 2008
# This program is distributed under the terms of the MIT License
+# now supports BOTH ruby 1.8.6 and ruby 1.9.1
begin
require 'rubygems'
@@ -11,7 +12,7 @@
require 'mixology'
module Stateology
- VERSION = "0.1.8"
+ VERSION = "0.2.0"
# alternative to 'nil'
Default = nil
@@ -21,18 +22,26 @@ def self.included(c)
c.extend(SM_Class_Methods)
end
+
# class methods
module SM_Class_Methods
def state(name, &block)
+
+ if RUBY_VERSION =~ /1.9/
+ pram = [false]
+ else
+ pram = []
+ end
# if const is defined here then module_eval
- if const_defined?(name) then
- self.module_eval(&block)
+ if const_defined?(name, *pram) then
+ m = self.const_get(name)
+ m.module_eval(&block)
else
m = Module.new
# if the state is defined further up the chain then "inherit it"
- if constants.include?(name.to_s) then
+ if constants.include?(name) || constants.include?(name.to_s) then
# if constant not defined here then must be inherited
inherited_state = const_get(name)
@@ -50,16 +59,17 @@ def state(name, &block)
# strip the class path and return just the constant name, i.e Hello::Fren -> Fren
def __elided_class_path(sym)
- sym.to_s.split(/::/).last.intern
+ sym.to_s.split(/::/).last.to_sym
end
def __sym_to_mod(sym)
class << self; self; end.const_get(sym)
end
def __mod_to_sym(mod)
+
# weird case where module does not have name (i.e when a state created on the eigenclass)
- if mod.name == "" then
+ if mod.name == nil || mod.name == "" then
class << self; self; end.constants.each do |v|
return v.to_sym if __sym_to_mod(v.to_sym) == mod
end
@@ -72,13 +82,19 @@ class << self; self; end.constants.each do |v|
# is state_name a nested state?
def __nested_state?(new_state)
+ if RUBY_VERSION =~ /1.9/
+ pram = [false]
+ else
+ pram = []
+ end
+
# test is:
# (1) are we currently in a state? (non nil)
# (2) is the new state a state? (non nil)
# (3) is the new state defined under the current state? (i.e it's nested)
__current_state &&
new_state &&
- __current_state.const_defined?(__elided_class_path(__mod_to_sym(new_state)))
+ __current_state.const_defined?(__elided_class_path(__mod_to_sym(new_state)), *pram)
end
# instance methods
@@ -184,6 +200,8 @@ def state?(state_name)
def state_mod
__current_state
end
+
+ alias_method :state=, :state
private :__state_prologue, :__state_epilogue, :__elided_class_path, :__mod_to_sym, :__sym_to_mod,
:__nested_state?, :__current_state, :__validate_state_name, :__state_transition, :__state_getter
View
68 test/test.rb
@@ -40,7 +40,6 @@ def state_exit
state(:State1_nested) {
def state_entry(&block)
- puts "balls-deep in State1_nested!"
if block then yield end
end
def act
@@ -146,11 +145,11 @@ def test_cant_transition_to_nested_from_nil
def test_nested_state
s = ParentState.new
- s.state :State1
- s.state :State1_nested
+ s.state = :State1
+ s.state = :State1_nested
assert_equal(1.5, s.act)
assert_equal(1, s.state1_act)
- s.state nil
+ s.state = nil
assert_raises(NoMethodError) { s.state1_act }
assert_equal(0, s.act)
end
@@ -159,29 +158,76 @@ def test_state_getter
s = ParentState.new
assert_equal(nil, s.state)
- s.state :State1
+ s.state = :State1
assert_equal(:State1, s.state)
- s.state :State1_nested
+ s.state = :State1_nested
assert_equal(:State1_nested, s.state)
end
def test_state_compare
s = ParentState.new
assert_equal(true, s.state?(nil))
- s.state :State1
+ s.state = :State1
assert_equal(false, s.state?(nil))
assert_equal(true, s.state?(:State1))
- s.state :State1_nested
+ s.state = :State1_nested
assert_equal(false, s.state?(:State1))
assert_equal(true, s.state?(:State1_nested))
- s.state nil
+ s.state = nil
assert_equal(true, s.state?(nil))
end
+ def test_state_reopen
+ s = ParentState.new
+
+ class << s
+ state(:State1) {
+ def reopen_act
+ :reopen_act
+ end
+ }
+
+ end
+
+ assert_raises(NoMethodError) { s.reopen_act }
+ s.state = :State1
+ assert_equal(1, s.act)
+ assert_equal(:reopen_act, s.reopen_act)
+ s.state = nil
+ assert_raises(NoMethodError) { s.reopen_act }
+ assert_equal(0, s.act)
+ end
+
+ def test_state_nested_reopen
+ s = ParentState.new
+
+ class << s
+ state(:State1) {
+ state(:State1_nested) {
+ def reopen_act
+ :reopen_act
+ end
+ }
+ }
+
+ end
+
+ assert_raises(NoMethodError) { s.reopen_act }
+ s.state = :State1
+ assert_equal(1, s.act)
+ assert_raises(NoMethodError) { s.reopen_act }
+ s.state = :State1_nested
+ assert_equal(:reopen_act, s.reopen_act)
+ assert_equal(1.5, s.act)
+ s.state = nil
+ assert_raises(NoMethodError) { s.reopen_act }
+ assert_equal(0, s.act)
+ end
+
def test_state_defined_on_singleton
s = ParentState.new
@@ -203,7 +249,7 @@ def state_exit
assert_equal(0, s.act)
- s.state :Sing_state
+ s.state = :Sing_state
# test the getter
assert_equal(:Sing_state, s.state)
@@ -217,7 +263,7 @@ def state_exit
# test state compare
assert_equal(true, s.state?(:Sing_state))
- s.state nil
+ s.state = nil
# test state_exit
assert_equal("sing_exit", s.state_exit_check)

0 comments on commit 5a28c5e

Please sign in to comment.
Something went wrong with that request. Please try again.