Class: Volt::Persistors::Cookies
- Defined in:
- lib/volt/models/persistors/cookies.rb
Overview
Backs a collection in the local store
Instance Method Summary collapse
-
#added(model, index) ⇒ Object
Called when a model is added to the collection.
-
#changed(attribute_name) ⇒ Object
Callled when an cookies value is changed.
-
#initialize(model) ⇒ Cookies
constructor
A new instance of Cookies.
- #loaded(initial_state = nil) ⇒ Object
- #read_cookies ⇒ Object
- #removed(attribute_name) ⇒ Object
- #write_cookie(key, value, options = {}) ⇒ Object
- #writing_cookies ⇒ Object
Methods inherited from Base
#clear, #event_added, #event_removed, #root_model
Constructor Details
#initialize(model) ⇒ Cookies
Returns a new instance of Cookies.
48 49 50 |
# File 'lib/volt/models/persistors/cookies.rb', line 48 def initialize(model) @model = model end |
Instance Method Details
#added(model, index) ⇒ Object
Called when a model is added to the collection
53 54 55 |
# File 'lib/volt/models/persistors/cookies.rb', line 53 def added(model, index) # Save an added cookie end |
#changed(attribute_name) ⇒ Object
Callled when an cookies value is changed
75 76 77 78 79 80 81 82 83 |
# File 'lib/volt/models/persistors/cookies.rb', line 75 def changed(attribute_name) # TODO: Make sure we're only assigning directly, not sub models unless $writing_cookies value = @model.get(attribute_name) # Temp, expire in 1 year, going to expand this api (attribute_name, value.to_s, expires: Time.now + (356 * 24 * 60 * 60), path: '/') end end |
#loaded(initial_state = nil) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/volt/models/persistors/cookies.rb', line 57 def loaded(initial_state = nil) # When the main model is first loaded, we pull in the data from the # store if it exists if !@cookies_loaded && @model.path == [] @cookies_loaded = true do # Assign directly so we don't trigger the callbacks on the initial load attrs = @model.attributes .each_pair do |key, value| attrs[key.to_sym] = value end end end end |
#read_cookies ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/volt/models/persistors/cookies.rb', line 10 def = `document.cookie` Hash[.split(';').map do |v| # Equals are valid as part of a cookie, so only parse the first equals. parts = v.split('=', 2).map { |p| p = p.strip; `decodeURIComponent(p)` } # Default to empty if no value parts << '' if parts.size == 1 # Equals are valid in parts end] end |
#removed(attribute_name) ⇒ Object
85 86 87 88 89 |
# File 'lib/volt/models/persistors/cookies.rb', line 85 def removed(attribute_name) do (attribute_name, '', max_age: 0, path: '/') end end |
#write_cookie(key, value, options = {}) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/volt/models/persistors/cookies.rb', line 24 def (key, value, = {}) [:path] ||= '/' parts = [] parts << `encodeURIComponent(key)` parts << '=' parts << `encodeURIComponent(value)` parts << '; ' parts << 'path=' << [:path] << '; ' if [:path] parts << 'max-age=' << [:max_age] << '; ' if [:max_age] if (expires = [:expires]) parts << 'expires=' << `expires.toGMTString()` << '; ' end parts << 'domain=' << [:domain] << '; ' if [:domain] parts << 'secure' if [:secure] = parts.join `document.cookie = cookie_val` end |
#writing_cookies ⇒ Object
91 92 93 94 95 |
# File 'lib/volt/models/persistors/cookies.rb', line 91 def $writing_cookies = true yield $writing_cookies = false end |