Module: Stateology

Defined in:
lib/stateology.rb

Defined Under Namespace

Modules: SM_Class_Methods

Constant Summary collapse

Default =

alternative to ‘nil’

nil

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(c) ⇒ Object

bring in class methods on include



15
16
17
# File 'lib/stateology.rb', line 15

def self.included(c)
    c.extend(SM_Class_Methods)
end

Instance Method Details

#state(*state_args, &block) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/stateology.rb', line 68

def state(*state_args, &block)
                                        
    # behave as getter
    if(state_args.empty?) then            
        return @__SM_cur_state ? "#{@__SM_cur_state}".split(/::/).last.intern : nil
    end
    
    # behave as setter (only care about first argument)
    state_name = state_args.shift
    
    # if we receive a Symbol convert it to a constant
    if(Symbol === state_name) then
        state_name = self.class.const_get(state_name)
    end
            
    # prevent unnecessary state transitions
    return if(@__SM_cur_state == state_name)
      
    # exit old state                                   
    __state_epilogue(@__SM_cur_state)                   
    
                
    # enter new state               
    __state_prologue(state_name, state_args)   
                    
    # update the current state variable    
    @__SM_cur_state = state_name
    
    # if we were given a block, run it now
    if(block) then yield end
    
    rescue NameError
        raise NameError, "#{state_name} not a valid state" 
                                    
end

#state?(state_name) ⇒ Boolean

is the current state equal to state_name?

Returns:

  • (Boolean)


105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/stateology.rb', line 105

def state?(state_name)

    # if we receive a Symbol convert it to a constant
    if(Symbol === state_name) then
        state_name = self.class.const_get(state_name)
    end
    
    raise NameError if(!(Module === state_name) && state_name != nil)
    
    state_name == @__SM_cur_state
    
    rescue NameError
        raise NameError, "#{state_name} not a valid state" 
                                                        
end

#state_modObject

return the current state as a module



122
123
124
# File 'lib/stateology.rb', line 122

def state_mod
    @__SM_cur_state
end