Class: Sinatra::Cookies::Jar

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/sinatra/cookies.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Jar

Returns a new instance of Jar.



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/sinatra/cookies.rb', line 62

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

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

  return unless app.settings.respond_to? :cookie_options

  @options.merge! app.settings.cookie_options
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



60
61
62
# File 'lib/sinatra/cookies.rb', line 60

def options
  @options
end

Instance Method Details

#==(other) ⇒ Object



81
82
83
# File 'lib/sinatra/cookies.rb', line 81

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

#[](key) ⇒ Object



85
86
87
# File 'lib/sinatra/cookies.rb', line 85

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

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



89
90
91
# File 'lib/sinatra/cookies.rb', line 89

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

#assoc(key) ⇒ Object



94
95
96
# File 'lib/sinatra/cookies.rb', line 94

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

#clearObject



99
100
101
# File 'lib/sinatra/cookies.rb', line 99

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

#compare_by_identity?Boolean

Returns:

  • (Boolean)


103
104
105
# File 'lib/sinatra/cookies.rb', line 103

def compare_by_identity?
  false
end

#defaultObject Also known as: default_proc



107
108
109
# File 'lib/sinatra/cookies.rb', line 107

def default
  nil
end

#delete(key) ⇒ Object



113
114
115
116
117
# File 'lib/sinatra/cookies.rb', line 113

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

#delete_ifObject Also known as: reject!



119
120
121
122
123
124
# File 'lib/sinatra/cookies.rb', line 119

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



126
127
128
129
130
# File 'lib/sinatra/cookies.rb', line 126

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

  to_hash.each(&block)
end

#each_key(&block) ⇒ Object



132
133
134
135
136
# File 'lib/sinatra/cookies.rb', line 132

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

  to_hash.each_key(&block)
end

#each_value(&block) ⇒ Object



140
141
142
143
144
# File 'lib/sinatra/cookies.rb', line 140

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

  to_hash.each_value(&block)
end

#empty?Boolean

Returns:

  • (Boolean)


146
147
148
# File 'lib/sinatra/cookies.rb', line 146

def empty?
  to_hash.empty?
end

#fetch(key, &block) ⇒ Object



150
151
152
153
154
# File 'lib/sinatra/cookies.rb', line 150

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

#flattenObject



157
158
159
# File 'lib/sinatra/cookies.rb', line 157

def flatten
  to_hash.flatten
end

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

Returns:

  • (Boolean)


162
163
164
# File 'lib/sinatra/cookies.rb', line 162

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

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

Returns:

  • (Boolean)


166
167
168
# File 'lib/sinatra/cookies.rb', line 166

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

#hashObject



170
171
172
# File 'lib/sinatra/cookies.rb', line 170

def hash
  to_hash.hash
end

#inspectObject



177
178
179
# File 'lib/sinatra/cookies.rb', line 177

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

#invertObject



182
183
184
# File 'lib/sinatra/cookies.rb', line 182

def invert
  to_hash.invert
end

#keep_ifObject Also known as: select!



187
188
189
190
191
# File 'lib/sinatra/cookies.rb', line 187

def keep_if
  return enum_for(__method__) unless block_given?

  delete_if { |*a| !yield(*a) }
end

#key(value) ⇒ Object



193
194
195
# File 'lib/sinatra/cookies.rb', line 193

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

#keysObject



199
200
201
# File 'lib/sinatra/cookies.rb', line 199

def keys
  to_hash.keys
end

#lengthObject Also known as: size



203
204
205
# File 'lib/sinatra/cookies.rb', line 203

def length
  to_hash.length
end

#merge(other, &block) ⇒ Object



207
208
209
# File 'lib/sinatra/cookies.rb', line 207

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

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



211
212
213
214
215
216
217
218
219
# File 'lib/sinatra/cookies.rb', line 211

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

#rassoc(value) ⇒ Object



221
222
223
# File 'lib/sinatra/cookies.rb', line 221

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

#rehashObject



225
226
227
228
229
# File 'lib/sinatra/cookies.rb', line 225

def rehash
  response_cookies.rehash
  request_cookies.rehash
  self
end

#reject(&block) ⇒ Object



231
232
233
234
235
# File 'lib/sinatra/cookies.rb', line 231

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

  to_hash.reject(&block)
end

#replace(other) ⇒ Object



239
240
241
242
# File 'lib/sinatra/cookies.rb', line 239

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

#select(&block) ⇒ Object



244
245
246
247
248
# File 'lib/sinatra/cookies.rb', line 244

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

  to_hash.select(&block)
end

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



252
253
254
# File 'lib/sinatra/cookies.rb', line 252

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

#shiftObject



256
257
258
259
260
# File 'lib/sinatra/cookies.rb', line 256

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

#sort(&block) ⇒ Object



265
266
267
# File 'lib/sinatra/cookies.rb', line 265

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

#to_aObject



276
277
278
# File 'lib/sinatra/cookies.rb', line 276

def to_a
  to_hash.to_a
end

#to_hashObject



272
273
274
# File 'lib/sinatra/cookies.rb', line 272

def to_hash
  request_cookies.merge(response_cookies)
end

#to_sObject



280
281
282
# File 'lib/sinatra/cookies.rb', line 280

def to_s
  to_hash.to_s
end

#valuesObject



287
288
289
# File 'lib/sinatra/cookies.rb', line 287

def values
  to_hash.values
end

#values_at(*list) ⇒ Object



291
292
293
# File 'lib/sinatra/cookies.rb', line 291

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