spoonsix-ssm 0.1.4 → 0.1.5
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.
- data/lib/ssm.rb +21 -8
- metadata +1 -1
    
        data/lib/ssm.rb
    CHANGED
    
    | @@ -40,7 +40,7 @@ require File.join(File.dirname(__FILE__), 'state_machine') | |
| 40 40 | 
             
            #--
         | 
| 41 41 | 
             
            module SSM
         | 
| 42 42 |  | 
| 43 | 
            -
              VERSION = '0.1. | 
| 43 | 
            +
              VERSION = '0.1.5';
         | 
| 44 44 |  | 
| 45 45 | 
             
              class InvalidTransition     < RuntimeError; end
         | 
| 46 46 | 
             
              class UndefinedState        < RuntimeError; end
         | 
| @@ -91,9 +91,7 @@ module SSM | |
| 91 91 | 
             
                    instance.instance_eval("def #{sm.property_name}; @#{sm.property_name}; end") unless instance.respond_to?(sm.property_name)
         | 
| 92 92 | 
             
                    instance.instance_eval("def #{sm.property_name}=(v); @#{sm.property_name} = v; end") unless instance.respond_to?("#{sm.property_name}=".to_sym)
         | 
| 93 93 |  | 
| 94 | 
            -
                     | 
| 95 | 
            -
                    initial_state_value = sm.use_property_index == true ? sm.get_state_index_by_name(sm.initial_state.name) : sm.initial_state.name
         | 
| 96 | 
            -
                    instance.instance_variable_set("@#{sm.property_name}".to_sym, initial_state_value)
         | 
| 94 | 
            +
                    instance.send(:_synchronize_state)
         | 
| 97 95 | 
             
                  end
         | 
| 98 96 | 
             
                end
         | 
| 99 97 |  | 
| @@ -215,7 +213,7 @@ module SSM | |
| 215 213 |  | 
| 216 214 | 
             
                  # Create StateMachine and create method associated with this StateTransition
         | 
| 217 215 | 
             
                  SSM::TemplateStateMachines[self] << SSM::Event.new(name, SSM::StateTransition.new(from, to), &block)
         | 
| 218 | 
            -
                  define_method("#{name.to_s}") { |*args|  | 
| 216 | 
            +
                  define_method("#{name.to_s}") { |*args| _synchronize_state; _ssm_trigger_event(name, args) }
         | 
| 219 217 | 
             
                end
         | 
| 220 218 |  | 
| 221 219 | 
             
                def template_state_machine #:nodoc:
         | 
| @@ -261,7 +259,7 @@ module SSM | |
| 261 259 | 
             
              #   door.open
         | 
| 262 260 | 
             
              #   door.is?(:opened) #=> true
         | 
| 263 261 | 
             
              def is?(state_name_or_symbol)
         | 
| 264 | 
            -
                 | 
| 262 | 
            +
                _synchronize_state
         | 
| 265 263 | 
             
                @ssm_state_machine.current_state.name.to_sym == state_name_or_symbol.to_sym
         | 
| 266 264 | 
             
              end
         | 
| 267 265 |  | 
| @@ -287,13 +285,14 @@ module SSM | |
| 287 285 | 
             
              #   door.open
         | 
| 288 286 | 
             
              #   door.is?(:closed) #=> false  
         | 
| 289 287 | 
             
              def is_not?(state_name_or_symbol)
         | 
| 290 | 
            -
                 | 
| 288 | 
            +
                _synchronize_state
         | 
| 291 289 | 
             
                @ssm_state_machine.current_state.name.to_sym != state_name_or_symbol.to_sym
         | 
| 292 290 | 
             
              end
         | 
| 293 291 |  | 
| 294 292 | 
             
              private
         | 
| 295 293 |  | 
| 296 294 | 
             
              def _ssm_trigger_event(event_name_or_symbol, args)
         | 
| 295 | 
            +
                _synchronize_state
         | 
| 297 296 | 
             
                event = @ssm_state_machine.get_event_by_name(event_name_or_symbol)
         | 
| 298 297 |  | 
| 299 298 | 
             
                @ssm_state_machine.transition(event.transition)
         | 
| @@ -329,6 +328,20 @@ module SSM | |
| 329 328 | 
             
                end
         | 
| 330 329 | 
             
              end
         | 
| 331 330 |  | 
| 331 | 
            +
              
         | 
| 332 | 
            +
              def _synchronize_state
         | 
| 333 | 
            +
                
         | 
| 334 | 
            +
                if instance_variable_get("@#{@ssm_state_machine.property_name}".to_sym).nil?
         | 
| 335 | 
            +
                  state_value = @ssm_state_machine.use_property_index == true ?
         | 
| 336 | 
            +
                    @ssm_state_machine.get_state_index_by_name(@ssm_state_machine.current_state.name) :
         | 
| 337 | 
            +
                    @ssm_state_machine.current_state.name
         | 
| 338 | 
            +
                  send("#{@ssm_state_machine.property_name}=".to_sym, state_value)
         | 
| 339 | 
            +
                else
         | 
| 340 | 
            +
                  _update_ssm_state unless _state_up_to_date?
         | 
| 341 | 
            +
                end
         | 
| 342 | 
            +
                true
         | 
| 343 | 
            +
              end
         | 
| 344 | 
            +
              
         | 
| 332 345 | 
             
              # Checks whether the StateMachine and the property in the instance are in sync
         | 
| 333 346 | 
             
              def _state_up_to_date?
         | 
| 334 347 | 
             
                unless @ssm_state_machine.property_name.nil?
         | 
| @@ -346,7 +359,7 @@ module SSM | |
| 346 359 | 
             
              # Updates the StateMachine based on the value of the state property of the instance
         | 
| 347 360 | 
             
              def _update_ssm_state
         | 
| 348 361 | 
             
                unless @ssm_state_machine.property_name.nil?
         | 
| 349 | 
            -
                  state_value =  | 
| 362 | 
            +
                  state_value = send("#{@ssm_state_machine.property_name}".to_sym)
         | 
| 350 363 | 
             
                  @ssm_state_machine.current_state = @ssm_state_machine.use_property_index == true ? @ssm_state_machine.get_state_by_index(state_value) : @ssm_state_machine.get_state_by_name(state_value)
         | 
| 351 364 | 
             
                end
         | 
| 352 365 | 
             
              end
         |