Sha256: b0cf45951690b0771f4231a3bb920b5933e121e388f106d76d66e61733109c74

Contents?: true

Size: 1.25 KB

Versions: 6

Compression:

Stored size: 1.25 KB

Contents

module Plezi

	# set magic cookies
	#
	# magic cookies keep track of both incoming and outgoing cookies, setting the response's cookies as well as the combined cookie respetory (held by the request object).
	#
	# use only the []= for magic cookies. merge and update might not set the response cookies.
	class Cookies < ::Hash
		# sets the Magic Cookie's controller object (which holds the response object and it's `set_cookie` method).
		def set_controller controller
			@controller = controller
		end
		# overrides the []= method to set the cookie for the response (by encoding it and preparing it to be sent), as well as to save the cookie in the combined cookie jar (unencoded and available).
		def []= key, val
			if key.is_a?(Symbol) && self.has_key?( key.to_s)
				key = key.to_s
			elsif self.has_key?( key.to_s.to_sym)
				key = key.to_s.to_sym
			end
			@controller.response.set_cookie key, (val ? val.to_s.dup : nil) if @controller
			super
		end
	end

	module Helpers
		# a proc that allows Hashes to search for key-value pairs while also converting keys from objects to symbols and from symbols to strings.
		#
		# (key type agnostic search Hash proc)
		 HASH_SYM_PROC = Proc.new {|h,k| k = (Symbol === k ? k.to_s : k.to_s.to_sym); h[k] if h.has_key?(k) }
	end

end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
plezi-0.9.2 lib/plezi/handlers/magic_helpers.rb
plezi-0.9.1 lib/plezi/handlers/magic_helpers.rb
plezi-0.9.0 lib/plezi/handlers/magic_helpers.rb
plezi-0.8.7 lib/plezi/handlers/magic_helpers.rb
plezi-0.8.6 lib/plezi/handlers/magic_helpers.rb
plezi-0.8.5 lib/plezi/handlers/magic_helpers.rb