Sha256: 0099c862e6c8264e997477ed7cc9f760a362f5e6bf2dc408c7c94f168af8fa56

Contents?: true

Size: 1.09 KB

Versions: 1

Compression:

Stored size: 1.09 KB

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] ||= {}
      @controller.session[options[:key]][id].symbolize_keys!
    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
        (Object::const_defined?("SecureRandom") ? SecureRandom : ActiveSupport::SecureRandom).hex(options[:length] / 2)
      when :integer
        self.counter += 1
      when :random_integer
        (Object::const_defined?("SecureRandom") ? SecureRandom : ActiveSupport::SecureRandom).integer
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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