Class: Sinatra::Cookies::Jar

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Jar

Returns a new instance of Jar.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/j1_app/sinatra/extras/cookies.rb', line 10

def initialize(app)
  @response_string = 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
  }

  if app.settings.respond_to? :cookie_options
    @options.merge! app.settings.cookie_options
  end
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



8
9
10
# File 'lib/j1_app/sinatra/extras/cookies.rb', line 8

def options
  @options
end

Instance Method Details

#==(other) ⇒ Object



29
30
31
# File 'lib/j1_app/sinatra/extras/cookies.rb', line 29

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

#[](key) ⇒ Object



33
34
35
# File 'lib/j1_app/sinatra/extras/cookies.rb', line 33

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

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



37
38
39
# File 'lib/j1_app/sinatra/extras/cookies.rb', line 37

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

#assoc(key) ⇒ Object



41
42
43
# File 'lib/j1_app/sinatra/extras/cookies.rb', line 41

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

#clearObject



45
46
47
# File 'lib/j1_app/sinatra/extras/cookies.rb', line 45

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

#compare_by_identity?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/j1_app/sinatra/extras/cookies.rb', line 49

def compare_by_identity?
  false
end

#defaultObject Also known as: default_proc



53
54
55
# File 'lib/j1_app/sinatra/extras/cookies.rb', line 53

def default
  nil
end

#delete(key) ⇒ Object



59
60
61
62
63
# File 'lib/j1_app/sinatra/extras/cookies.rb', line 59

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

#delete_ifObject Also known as: reject!



65
66
67
68
69
# File 'lib/j1_app/sinatra/extras/cookies.rb', line 65

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



71
72
73
74
# File 'lib/j1_app/sinatra/extras/cookies.rb', line 71

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

#each_key(&block) ⇒ Object



76
77
78
79
# File 'lib/j1_app/sinatra/extras/cookies.rb', line 76

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

#each_value(&block) ⇒ Object



83
84
85
86
# File 'lib/j1_app/sinatra/extras/cookies.rb', line 83

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

#empty?Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/j1_app/sinatra/extras/cookies.rb', line 88

def empty?
  to_hash.empty?
end

#fetch(key, &block) ⇒ Object



92
93
94
95
96
# File 'lib/j1_app/sinatra/extras/cookies.rb', line 92

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

#flattenObject



98
99
100
# File 'lib/j1_app/sinatra/extras/cookies.rb', line 98

def flatten
  to_hash.flatten
end

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

Returns:

  • (Boolean)


102
103
104
# File 'lib/j1_app/sinatra/extras/cookies.rb', line 102

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)


106
107
108
# File 'lib/j1_app/sinatra/extras/cookies.rb', line 106

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

#hashObject



110
111
112
# File 'lib/j1_app/sinatra/extras/cookies.rb', line 110

def hash
  to_hash.hash
end

#index(value) ⇒ Object



117
118
119
120
# File 'lib/j1_app/sinatra/extras/cookies.rb', line 117

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

#inspectObject



122
123
124
# File 'lib/j1_app/sinatra/extras/cookies.rb', line 122

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

#invertObject



126
127
128
# File 'lib/j1_app/sinatra/extras/cookies.rb', line 126

def invert
  to_hash.invert
end

#keep_ifObject Also known as: select!



130
131
132
133
# File 'lib/j1_app/sinatra/extras/cookies.rb', line 130

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

#key(value) ⇒ Object



135
136
137
# File 'lib/j1_app/sinatra/extras/cookies.rb', line 135

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

#keysObject



141
142
143
# File 'lib/j1_app/sinatra/extras/cookies.rb', line 141

def keys
  to_hash.keys
end

#lengthObject Also known as: size



145
146
147
# File 'lib/j1_app/sinatra/extras/cookies.rb', line 145

def length
  to_hash.length
end

#merge(other, &block) ⇒ Object



149
150
151
# File 'lib/j1_app/sinatra/extras/cookies.rb', line 149

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

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



153
154
155
156
157
158
159
160
161
# File 'lib/j1_app/sinatra/extras/cookies.rb', line 153

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



163
164
165
# File 'lib/j1_app/sinatra/extras/cookies.rb', line 163

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

#rehashObject



167
168
169
170
171
# File 'lib/j1_app/sinatra/extras/cookies.rb', line 167

def rehash
  response_cookies.rehash
  request_cookies.rehash
  self
end

#reject(&block) ⇒ Object



173
174
175
176
# File 'lib/j1_app/sinatra/extras/cookies.rb', line 173

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

#replace(other) ⇒ Object



180
181
182
183
# File 'lib/j1_app/sinatra/extras/cookies.rb', line 180

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

#select(&block) ⇒ Object



185
186
187
188
# File 'lib/j1_app/sinatra/extras/cookies.rb', line 185

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

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



192
193
194
# File 'lib/j1_app/sinatra/extras/cookies.rb', line 192

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

#shiftObject



196
197
198
199
200
# File 'lib/j1_app/sinatra/extras/cookies.rb', line 196

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

#sort(&block) ⇒ Object



204
205
206
# File 'lib/j1_app/sinatra/extras/cookies.rb', line 204

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

#to_aObject



214
215
216
# File 'lib/j1_app/sinatra/extras/cookies.rb', line 214

def to_a
  to_hash.to_a
end

#to_hashObject



210
211
212
# File 'lib/j1_app/sinatra/extras/cookies.rb', line 210

def to_hash
  request_cookies.merge(response_cookies)
end

#to_sObject



218
219
220
# File 'lib/j1_app/sinatra/extras/cookies.rb', line 218

def to_s
  to_hash.to_s
end

#valuesObject



225
226
227
# File 'lib/j1_app/sinatra/extras/cookies.rb', line 225

def values
  to_hash.values
end

#values_at(*list) ⇒ Object



229
230
231
# File 'lib/j1_app/sinatra/extras/cookies.rb', line 229

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