Sha256: 86addbb7ec9f95100f6f260b75adfdf5157127c3c2f747252de685a934f731bf

Contents?: true

Size: 1.07 KB

Versions: 1

Compression:

Stored size: 1.07 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] ||= {}
    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.2.0 lib/micro_sessions/micro_session.rb