Sha256: 7dc80f1de411c6846946f676a3c2f42978a0e1dbfe9b59cf24f5954eb3987925

Contents?: true

Size: 868 Bytes

Versions: 16

Compression:

Stored size: 868 Bytes

Contents

require "json"

module Split
  module Persistence
    class CookieAdapter

      EXPIRES = Time.now + 31536000 # One year from now

      def initialize(context)
        @cookies = context.send(:cookies)
      end

      def [](key)
        hash[key]
      end

      def []=(key, value)
        set_cookie(hash.merge(key => value))
      end

      def delete(key)
        set_cookie(hash.tap { |h| h.delete(key) })
      end

      def keys
        hash.keys
      end

      private

      def set_cookie(value)
        @cookies[:split] = {
          :value => JSON.generate(value),
          :expires => EXPIRES
        }
      end

      def hash
        if @cookies[:split]
          begin
            JSON.parse(@cookies[:split])
          rescue JSON::ParserError
            {}
          end
        else
          {}
        end
      end

    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
split-1.2.1 lib/split/persistence/cookie_adapter.rb
split-1.2.0 lib/split/persistence/cookie_adapter.rb
split-1.1.0 lib/split/persistence/cookie_adapter.rb
split-1.0.0 lib/split/persistence/cookie_adapter.rb
split-0.8.0 lib/split/persistence/cookie_adapter.rb
split-0.7.3 lib/split/persistence/cookie_adapter.rb
split-0.7.2 lib/split/persistence/cookie_adapter.rb
split-0.7.1 lib/split/persistence/cookie_adapter.rb
split-0.7.0 lib/split/persistence/cookie_adapter.rb
split-0.6.6 lib/split/persistence/cookie_adapter.rb
split-0.6.5 lib/split/persistence/cookie_adapter.rb
split-0.6.4 lib/split/persistence/cookie_adapter.rb
split-0.6.3 lib/split/persistence/cookie_adapter.rb
split-0.6.2 lib/split/persistence/cookie_adapter.rb
split-0.6.1 lib/split/persistence/cookie_adapter.rb
split-0.6.0 lib/split/persistence/cookie_adapter.rb