Class: Jets::Controller::Cookies::Jar

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/jets/controller/cookies/jar.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(controller) ⇒ Jar

Returns a new instance of Jar.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/jets/controller/cookies/jar.rb', line 8

def initialize(controller)
  @response_string = nil
  @response_hash   = {}
  @response        = controller.response
  @request         = controller.request
  @deleted         = []

  @options = {
    path: @request.script_name.to_s.empty? ? '/' : @request.script_name,
    domain: @request.host == 'localhost' ? nil : @request.host,
    secure: @request.ssl?,
    httponly: true
  }
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



6
7
8
# File 'lib/jets/controller/cookies/jar.rb', line 6

def options
  @options
end

Instance Method Details

#==(other) ⇒ Object



23
24
25
# File 'lib/jets/controller/cookies/jar.rb', line 23

def ==(other)
  other.respond_to? :to_hash and to_hash == other.to_hash
end

#[](key) ⇒ Object



27
28
29
# File 'lib/jets/controller/cookies/jar.rb', line 27

def [](key)
  response_cookies[key.to_s] || request_cookies[key.to_s]
end

#[]=(key, value) ⇒ Object Also known as: store



31
32
33
# File 'lib/jets/controller/cookies/jar.rb', line 31

def []=(key, value)
  set(key, value: value)
end

#assoc(key) ⇒ Object



35
36
37
# File 'lib/jets/controller/cookies/jar.rb', line 35

def assoc(key)
  to_hash.assoc(key.to_s)
end

#clearObject



39
40
41
# File 'lib/jets/controller/cookies/jar.rb', line 39

def clear
  each_key { |k| delete(k) }
end

#compare_by_identity?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/jets/controller/cookies/jar.rb', line 43

def compare_by_identity?
  false
end

#defaultObject Also known as: default_proc



47
48
49
# File 'lib/jets/controller/cookies/jar.rb', line 47

def default
  nil
end

#delete(key) ⇒ Object



53
54
55
56
57
# File 'lib/jets/controller/cookies/jar.rb', line 53

def delete(key)
  result = self[key]
  @response.delete_cookie(key.to_s, @options)
  result
end

#delete_ifObject Also known as: reject!



59
60
61
62
63
# File 'lib/jets/controller/cookies/jar.rb', line 59

def delete_if
  return enum_for(__method__) unless block_given?
  each { |k, v| delete(k) if yield(k, v) }
  self
end

#each(&block) ⇒ Object Also known as: each_pair



65
66
67
68
# File 'lib/jets/controller/cookies/jar.rb', line 65

def each(&block)
  return enum_for(__method__) unless block_given?
  to_hash.each(&block)
end

#each_key(&block) ⇒ Object



70
71
72
73
# File 'lib/jets/controller/cookies/jar.rb', line 70

def each_key(&block)
  return enum_for(__method__) unless block_given?
  to_hash.each_key(&block)
end

#each_value(&block) ⇒ Object



77
78
79
80
# File 'lib/jets/controller/cookies/jar.rb', line 77

def each_value(&block)
  return enum_for(__method__) unless block_given?
  to_hash.each_value(&block)
end

#empty?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/jets/controller/cookies/jar.rb', line 82

def empty?
  to_hash.empty?
end

#fetch(key, &block) ⇒ Object



86
87
88
89
90
# File 'lib/jets/controller/cookies/jar.rb', line 86

def fetch(key, &block)
  response_cookies.fetch(key.to_s) do
    request_cookies.fetch(key.to_s, &block)
  end
end

#flattenObject



92
93
94
# File 'lib/jets/controller/cookies/jar.rb', line 92

def flatten
  to_hash.flatten
end

#has_key?(key) ⇒ Boolean Also known as: include?, member?, key?

Returns:

  • (Boolean)


96
97
98
# File 'lib/jets/controller/cookies/jar.rb', line 96

def has_key?(key)
  response_cookies.has_key? key.to_s or request_cookies.has_key? key.to_s
end

#has_value?(value) ⇒ Boolean Also known as: value?

Returns:

  • (Boolean)


100
101
102
# File 'lib/jets/controller/cookies/jar.rb', line 100

def has_value?(value)
  response_cookies.has_value? value or request_cookies.has_value? value
end

#hashObject



104
105
106
# File 'lib/jets/controller/cookies/jar.rb', line 104

def hash
  to_hash.hash
end

#index(value) ⇒ Object



111
112
113
114
# File 'lib/jets/controller/cookies/jar.rb', line 111

def index(value)
  warn "Hash#index is deprecated; use Hash#key"
  key(value)
end

#inspectObject



116
117
118
# File 'lib/jets/controller/cookies/jar.rb', line 116

def inspect
  "<##{self.class}: #{to_hash.inspect[1..-2]}>"
end

#invertObject



120
121
122
# File 'lib/jets/controller/cookies/jar.rb', line 120

def invert
  to_hash.invert
end

#keep_ifObject Also known as: select!



124
125
126
127
# File 'lib/jets/controller/cookies/jar.rb', line 124

def keep_if
  return enum_for(__method__) unless block_given?
  delete_if { |*a| not yield(*a) }
end

#key(value) ⇒ Object



129
130
131
# File 'lib/jets/controller/cookies/jar.rb', line 129

def key(value)
  to_hash.key(value)
end

#keysObject



135
136
137
# File 'lib/jets/controller/cookies/jar.rb', line 135

def keys
  to_hash.keys
end

#lengthObject Also known as: size



139
140
141
# File 'lib/jets/controller/cookies/jar.rb', line 139

def length
  to_hash.length
end

#merge(other, &block) ⇒ Object



143
144
145
# File 'lib/jets/controller/cookies/jar.rb', line 143

def merge(other, &block)
  to_hash.merge(other, &block)
end

#merge!(other) ⇒ Object Also known as: update



147
148
149
150
151
152
153
154
155
# File 'lib/jets/controller/cookies/jar.rb', line 147

def merge!(other)
  other.each_pair do |key, value|
    if block_given? and include? key
      self[key] = yield(key.to_s, self[key], value)
    else
      self[key] = value
    end
  end
end

#rassoc(value) ⇒ Object



157
158
159
# File 'lib/jets/controller/cookies/jar.rb', line 157

def rassoc(value)
  to_hash.rassoc(value)
end

#rehashObject



161
162
163
164
165
# File 'lib/jets/controller/cookies/jar.rb', line 161

def rehash
  response_cookies.rehash
  request_cookies.rehash
  self
end

#reject(&block) ⇒ Object



167
168
169
170
# File 'lib/jets/controller/cookies/jar.rb', line 167

def reject(&block)
  return enum_for(__method__) unless block_given?
  to_hash.reject(&block)
end

#replace(other) ⇒ Object



174
175
176
177
# File 'lib/jets/controller/cookies/jar.rb', line 174

def replace(other)
  select! { |k, v| other.include?(k) or other.include?(k.to_s)  }
  merge! other
end

#select(&block) ⇒ Object



179
180
181
182
# File 'lib/jets/controller/cookies/jar.rb', line 179

def select(&block)
  return enum_for(__method__) unless block_given?
  to_hash.select(&block)
end

#set(key, options = {}) ⇒ Object



186
187
188
# File 'lib/jets/controller/cookies/jar.rb', line 186

def set(key, options = {})
  @response.set_cookie key.to_s, @options.merge(options)
end

#shiftObject



190
191
192
193
194
# File 'lib/jets/controller/cookies/jar.rb', line 190

def shift
  key, value = to_hash.shift
  delete(key)
  [key, value]
end

#sort(&block) ⇒ Object



198
199
200
# File 'lib/jets/controller/cookies/jar.rb', line 198

def sort(&block)
  to_hash.sort(&block)
end

#to_aObject



208
209
210
# File 'lib/jets/controller/cookies/jar.rb', line 208

def to_a
  to_hash.to_a
end

#to_hashObject



204
205
206
# File 'lib/jets/controller/cookies/jar.rb', line 204

def to_hash
  request_cookies.merge(response_cookies)
end

#to_sObject



212
213
214
# File 'lib/jets/controller/cookies/jar.rb', line 212

def to_s
  to_hash.to_s
end

#valuesObject



219
220
221
# File 'lib/jets/controller/cookies/jar.rb', line 219

def values
  to_hash.values
end

#values_at(*list) ⇒ Object



223
224
225
# File 'lib/jets/controller/cookies/jar.rb', line 223

def values_at(*list)
  list.map { |k| self[k] }
end