Sha256: 59e277723b5fed5d7edbfb972e54d53793f330eb0ec0072a94d5c4ff9a9f7148

Contents?: true

Size: 854 Bytes

Versions: 1

Compression:

Stored size: 854 Bytes

Contents

module MicroSessions
  class MicroSession
    def initialize(controller)
      @controller = controller
    end
    
    def options
      @controller.class.micro_session_options
    end
    
    def data
      @controller.session[options[:key]] ||= {}
      @controller.session[options[:key]][id] ||= {}
    end
    
    delegate :empty?, :[], :[]=, :to_h, :to_hash, :to => :data
    
    def id
      @id ||= id_from_params || generate_id
    end
    
    
    protected
    
    def id_from_params
      @controller.params[options[:param]] if @controller.params
    end
    
    def generate_id
      case options[:param_type]
      when :hash
        ActiveSupport::SecureRandom.hex(options[:length] / 2)
      when :integer
        self.counter += 1
      when :random_integer
        ActiveSupport::SecureRandom.integer
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
micro_sessions-0.1.0 lib/micro_sessions/micro_session.rb