Class: Nephos::Cookies

Inherits:
Params
  • Object
show all
Defined in:
lib/nephos-server/cookies.rb

Overview

Params to a hash, where every elements are accessibles via a stringified key so, even if the entry was added with the key :KEY, it will be accessible via a string equivalent to :key.to_s

param["key"] == param[:key]

Every methods present in Hash are usable in Param.

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}) ⇒ Cookies

Returns a new instance of Cookies.

Parameters:

  • hash (Hash) (defaults to: {})

    hash containing the parameters

Raises:

  • (ArgumentError)


14
15
16
17
18
# File 'lib/nephos-server/cookies.rb', line 14

def initialize(hash={})
  raise ArgumentError, "the first argument must be a Hash" unless hash.is_a? Hash
  # TODO: take care of the path
  @hash = Hash[hash.map{|k,v| [k.to_s, {content: v, path: "/"}]}]
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *a) ⇒ Object



20
21
22
23
# File 'lib/nephos-server/cookies.rb', line 20

def method_missing m, *a
  @hash.send(m, *(a.map(&:to_s)))
  @hash.send(m, *a)
end

Instance Method Details

#[](i) ⇒ Object



25
26
27
28
# File 'lib/nephos-server/cookies.rb', line 25

def [] i
  c = @hash[i.to_s]
  c && c[:content]
end

#[]=(i, v, path = "/") ⇒ Object



30
31
32
# File 'lib/nephos-server/cookies.rb', line 30

def []= i, v, path="/"
  @hash[i.to_s] = {content: v.to_s, path: path.to_s}
end

#path(i) ⇒ Object



34
35
36
# File 'lib/nephos-server/cookies.rb', line 34

def path i
  @hash[i.to_s][:path]
end

#set_path(i, v) ⇒ Object

Raises:

  • (InvalidPath)


38
39
40
41
# File 'lib/nephos-server/cookies.rb', line 38

def set_path i, v
  raise InvalidPath, v unless v.is_a? String
  @hash[i.to_s][:path] = URI.encode(v)
end

#to_hObject Also known as: to_hash



43
44
45
# File 'lib/nephos-server/cookies.rb', line 43

def to_h
  return @hash
end

#to_sObject



48
49
50
# File 'lib/nephos-server/cookies.rb', line 48

def to_s
  return to_h.to_s
end