session_handler.rb

Path: lib/action_flow/session_handler.rb
Last Update: Tue May 01 14:39:28 -0400 2007

Methods

Constants

Session_data_prefix = 'flow_data-'   Prefix of the variables which will be created in the user session to store the flow data.
Session_data_prefix_regex = /^#{Session_data_prefix}[0-9]{8}$/   Used to detect flow data keys in the session hash
Session_timestamp_placeholder = 'updated_at'   Name of the key in the flow hash stored in the session which tells when was this flow last executed
Session_ttl = 1.hour   Defines for how long a flow execution data is maintained in the session placeholder.
Last_step_placeholder = 'last_step_name'   Defines where to store the name of the last executed step

Public Instance methods

Cleans the flows data for flows which have not been executed since the value defined by Session_ttl and Session_timestamp_placeholder.

[Source]

# File lib/action_flow/session_handler.rb, line 60
        def cleanup
        
          # Iterate over the real session keys to find flow placeholders
          request.session.instance_variable_get("@data").each do |key,value|
        
            # Check if the key is a flow placeholder
            if key =~ Session_data_prefix_regex
          
              # Delete it if the flow has expired
              request.session.instance_variable_get("@data").delete(key) if ( session[key].fetch( Session_timestamp_placeholder ) < ( Time.now - Session_ttl ) )
          
            end
          
          end
        
        end

Performs a deep copy of an object and returns a copy of the source and all it‘s sub objects as copies. Useful for example when a hash contains objects and you want a copy of the Hash and copies of the objects it contains and not the objects themselves

[Source]

# File lib/action_flow/session_handler.rb, line 254
  def deep_copy
      Marshal.load(Marshal.dump(self))
  end

Adds the last step name to the current flow data so we can interpret the returned event correctly once the user submits data back.

[Source]

# File lib/action_flow/session_handler.rb, line 172
        def fetch_last_step_name
  
          # Fetch the last step name from the correct placeholder
          return session[ flow_session_name ].fetch( state_session_name ).fetch( Last_step_placeholder )
        
        end

Returns the name of the variable used in the real session data hash to store this flow data

[Source]

# File lib/action_flow/session_handler.rb, line 231
        def flow_session_name
          ( Session_data_prefix + @flow_id[0,8] ).to_s
        end

Initializes the session variables needed to save the flow data.

[Source]

# File lib/action_flow/session_handler.rb, line 198
        def init_flow_session_space
        
          # The placeholder for the flow will be named...
          flow_placeholder = flow_session_name
          
          # Create a new placeholder for this flow data in the real session space
          session[flow_placeholder] = HashWithIndifferentAccess.new
        
          # Timestamp the creation time
          update_timestamp
        
          # Return shit
          return nil
      
        end

Initializes the session variables needed to save the state data.

[Source]

# File lib/action_flow/session_handler.rb, line 217
        def init_state_session_space
          
          # Create a new placeholder for this state data in the real session space
          # reserved for this flow data
          session[ flow_session_name ].store state_session_name, HashWithIndifferentAccess.new
      
          # Return shit
          return nil
        
        end

Adds the last step name to the current flow data so we can interpret the returned event correctly once the user submits data back.

[Source]

# File lib/action_flow/session_handler.rb, line 155
        def persist_last_step_name(state_name)
  
          # Store the last step name in the correct placeholder
          session[ flow_session_name ].fetch( state_session_name ).store( Last_step_placeholder, state_name.to_s )
        
          # Return shit
          return nil
        
        end

Saves the current flow state data and makes sure that everything will be available to restore the flow state later on.

[Source]

# File lib/action_flow/session_handler.rb, line 82
        def serialize
      
          # Update the flow timestamp
          update_timestamp
      
          # Copy the current state data
          old_state = (state).deep_copy
        
          # Generate a new flow id.
          # With old flows, we use the first 8 characters of the last execution id
          # but we generate 56 new ones which we append
          @flow_id = @flow_id[0,8] + ActionFlow::RandomGenerator.random_string(56)
        
          # Initialize the flow session storage for this execution (state)
          init_state_session_space
        
          # Copy the shadow session state into the new state to freeze it
          state = old_state.deep_copy
        
          # Return shit
          return nil
      
        end

Does everything needed to store data concerning a new flow execution

[Source]

# File lib/action_flow/session_handler.rb, line 123
        def start_new_flow_session_storage
      
          # With new flows, we define a completely new 64 characters id
          @flow_id = ActionFlow::RandomGenerator.random_string(64)
          
          # Initialize the flow session storage for the flow itself, since it will
          # then contain all the states
          init_flow_session_space
        
          # Initialize the flow session storage for this execution
          init_state_session_space
      
        end

Returns the name of the variable used in the flow session data hash to store this state data

[Source]

# File lib/action_flow/session_handler.rb, line 238
        def state_session_name
          ( @flow_id[8,56] ).to_s
        end

Cleans all data concerning the flow from the session.

[Source]

# File lib/action_flow/session_handler.rb, line 110
        def terminate
  
          # Destroy the flow placeholder in the session
          request.session.instance_variable_get("@data").delete flow_session_name
        
          # Return shit
          return nil
        
        end

Updates the timestamp for the current flow. Is to be called on each session modification.

[Source]

# File lib/action_flow/session_handler.rb, line 140
        def update_timestamp
      
          # Update the timestamp for the current flow
          session[flow_session_name].store Session_timestamp_placeholder, Time.now
      
        end

Validates that the given flow key corresponds to a stored flow data

[Source]

# File lib/action_flow/session_handler.rb, line 183
        def validate_flow_existence
          
          !session[ flow_session_name ].nil?
          
        end

[Validate]