Sha256: 6673e32b6514074427dc47c0e2470822ecd5242e113f868743688e3bd3514adc

Contents?: true

Size: 977 Bytes

Versions: 1

Compression:

Stored size: 977 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
    
    def url_options
      if empty?
        {}
      else
        {options[:param] => id}
      end
    end
    
    delegate :empty?, :[], :[]=, :to_h, :to_hash, :inspect, :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.1 lib/micro_sessions/micro_session.rb