From 82145d101815fec394a4461ac10285de59412d51 Mon Sep 17 00:00:00 2001 From: balmoral Date: Wed, 4 Nov 2015 16:06:45 +1100 Subject: [PATCH 1/8] add session_store to supplement local_store --- .gitignore | 1 + lib/volt/utils/html_storage.rb | 58 +++++++++++++++++++++++++++++++ lib/volt/utils/local_storage.rb | 54 +++------------------------- lib/volt/utils/session_storage.rb | 9 +++++ 4 files changed, 73 insertions(+), 49 deletions(-) create mode 100644 lib/volt/utils/html_storage.rb create mode 100644 lib/volt/utils/session_storage.rb diff --git a/.gitignore b/.gitignore index 0a9a6497..46faf732 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +.idea *.gem *.rbc .bundle diff --git a/lib/volt/utils/html_storage.rb b/lib/volt/utils/html_storage.rb new file mode 100644 index 00000000..387bd83f --- /dev/null +++ b/lib/volt/utils/html_storage.rb @@ -0,0 +1,58 @@ +if RUBY_PLATFORM == 'opal' + module Volt + module HtmlStorage + + def self.area + nil # implement in SessionStorage and LocalStorage + end + + def self.[](key) + ` + var val = {{store}}.getItem(key); + return val === null ? nil : val; + ` + end + + def self.[]=(key, value) + `{{store}}.setItem(key, value)` + end + + def self.clear + `{{store}}.clear()` + self + end + + def self.delete(key) + ` + var val = {{store}}.getItem(key); + {{store}}.removeItem(key); + return val === null ? nil : val; + ` + end + end + end +else + module Volt + module HtmlStorage + @@store = {} + + def self.[](key) + @@store[key] + end + + def self.[]=(key, value) + @@store[key] = value + end + + def self.clear + @@store = {} + + self + end + + def self.delete(key) + @@store.delete(key) + end + end + end +end diff --git a/lib/volt/utils/local_storage.rb b/lib/volt/utils/local_storage.rb index 76ef7aff..56adb973 100644 --- a/lib/volt/utils/local_storage.rb +++ b/lib/volt/utils/local_storage.rb @@ -1,53 +1,9 @@ -if RUBY_PLATFORM == 'opal' - module Volt - module LocalStorage - def self.[](key) - ` - var val = localStorage.getItem(key); - return val === null ? nil : val; - ` - end +module Volt + module SessionStorage + include HtmlStorage - def self.[]=(key, value) - `localStorage.setItem(key, value)` - end - - def self.clear - `localStorage.clear()` - self - end - - def self.delete(key) - ` - var val = localStorage.getItem(key); - localStorage.removeItem(key); - return val === null ? nil : val; - ` - end - end - end -else - module Volt - module LocalStorage - @@store = {} - - def self.[](key) - @@store[key] - end - - def self.[]=(key, value) - @@store[key] = value - end - - def self.clear - @@store = {} - - self - end - - def self.delete(key) - @@store.delete(key) - end + def self.area + `localStorage` end end end diff --git a/lib/volt/utils/session_storage.rb b/lib/volt/utils/session_storage.rb new file mode 100644 index 00000000..a1011a01 --- /dev/null +++ b/lib/volt/utils/session_storage.rb @@ -0,0 +1,9 @@ +module Volt + module SessionStorage + include HtmlStorage + + def self.area + `sessionStorage` + end + end +end From 635f704c1d04137b3737489d538a848954cf6534 Mon Sep 17 00:00:00 2001 From: balmoral Date: Wed, 4 Nov 2015 16:08:13 +1100 Subject: [PATCH 2/8] add session_store to supplement local_store --- lib/volt/utils/local_storage.rb | 6 ++++-- lib/volt/utils/session_storage.rb | 7 +++++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/lib/volt/utils/local_storage.rb b/lib/volt/utils/local_storage.rb index 56adb973..6c925782 100644 --- a/lib/volt/utils/local_storage.rb +++ b/lib/volt/utils/local_storage.rb @@ -2,8 +2,10 @@ module Volt module SessionStorage include HtmlStorage - def self.area - `localStorage` + if RUBY_PLATFORM == 'opal' + def self.area + `localStorage` + end end end end diff --git a/lib/volt/utils/session_storage.rb b/lib/volt/utils/session_storage.rb index a1011a01..0ee4baa7 100644 --- a/lib/volt/utils/session_storage.rb +++ b/lib/volt/utils/session_storage.rb @@ -2,8 +2,11 @@ module Volt module SessionStorage include HtmlStorage - def self.area - `sessionStorage` + if RUBY_PLATFORM == 'opal' + def self.area + `sessionStorage` + end end + end end From 911ac84f4ea2dc38149b43f708481b93ed02fd68 Mon Sep 17 00:00:00 2001 From: balmoral Date: Wed, 4 Nov 2015 16:24:06 +1100 Subject: [PATCH 3/8] add session_store to supplement local_store --- lib/volt/controllers/collection_helpers.rb | 4 ++ lib/volt/models/persistors/html_store.rb | 55 +++++++++++++++++++++ lib/volt/models/persistors/local_store.rb | 42 ++-------------- lib/volt/models/persistors/session_store.rb | 15 ++++++ lib/volt/models/root_models/root_models.rb | 2 +- lib/volt/utils/html_storage.rb | 3 +- lib/volt/utils/local_storage.rb | 2 +- lib/volt/volt/repos.rb | 7 +++ 8 files changed, 89 insertions(+), 41 deletions(-) create mode 100644 lib/volt/models/persistors/html_store.rb create mode 100644 lib/volt/models/persistors/session_store.rb diff --git a/lib/volt/controllers/collection_helpers.rb b/lib/volt/controllers/collection_helpers.rb index 9e142d86..c3a59b81 100644 --- a/lib/volt/controllers/collection_helpers.rb +++ b/lib/volt/controllers/collection_helpers.rb @@ -34,6 +34,10 @@ def local_store Volt.current_app.local_store end + def session_store + Volt.current_app.session_store + end + def cookies Volt.current_app.cookies end diff --git a/lib/volt/models/persistors/html_store.rb b/lib/volt/models/persistors/html_store.rb new file mode 100644 index 00000000..5c5d6d07 --- /dev/null +++ b/lib/volt/models/persistors/html_store.rb @@ -0,0 +1,55 @@ +require 'volt/models/persistors/base' +require 'volt/utils/html_storage' +require 'volt/utils/ejson' + +module Volt + module Persistors + # Backs a collection in the local store + class HtmlStore < Base + + # Implement in LocalStore and SessionStore + def self.storage + raise 'should be implemented in SessionStore or LocalStore' + end + + # Called when a model is added to the collection + def added(model, index) + root_model.persistor.save_all + end + + def loaded(initial_state = nil) + super + # When the main model is first loaded, we pull in the data from the + # store if it exists + if @model.path == [] + json_data = self.class.storage['volt-store'] + if json_data + root_attributes = EJSON.parse(json_data) + + @loading_data = true + root_attributes.each_pair do |key, value| + @model.send(:"_#{key}=", value) + end + @loading_data = nil + end + end + end + + # Called when an item is changed (or removed) + def changed(attribute_name) + root_model.persistor.save_all + + true + end + + # Called on the root + def save_all + return if @loading_data + + json_data = EJSON.stringify(@model.to_h) + + self.class.storage['volt-store'] = json_data + end + end + end +end diff --git a/lib/volt/models/persistors/local_store.rb b/lib/volt/models/persistors/local_store.rb index 460b2ef6..fd67dd0c 100644 --- a/lib/volt/models/persistors/local_store.rb +++ b/lib/volt/models/persistors/local_store.rb @@ -1,49 +1,15 @@ -require 'volt/models/persistors/base' +require 'volt/models/persistors/html_store' require 'volt/utils/local_storage' -require 'volt/utils/ejson' module Volt module Persistors # Backs a collection in the local store - class LocalStore < Base - # Called when a model is added to the collection - def added(model, index) - root_model.persistor.save_all - end - - def loaded(initial_state = nil) - super - # When the main model is first loaded, we pull in the data from the - # store if it exists - if @model.path == [] - json_data = LocalStorage['volt-store'] - if json_data - root_attributes = EJSON.parse(json_data) - - @loading_data = true - root_attributes.each_pair do |key, value| - @model.send(:"_#{key}=", value) - end - @loading_data = nil - end - end - end + class LocalStore < HtmlStore - # Callled when an item is changed (or removed) - def changed(attribute_name) - root_model.persistor.save_all - - true + def self.storage + LocalStorage end - # Called on the root - def save_all - return if @loading_data - - json_data = EJSON.stringify(@model.to_h) - - LocalStorage['volt-store'] = json_data - end end end end diff --git a/lib/volt/models/persistors/session_store.rb b/lib/volt/models/persistors/session_store.rb new file mode 100644 index 00000000..131721ac --- /dev/null +++ b/lib/volt/models/persistors/session_store.rb @@ -0,0 +1,15 @@ +require 'volt/models/persistors/html_store' +require 'volt/utils/session_storage' + +module Volt + module Persistors + # Backs a collection in the local store + class SessionStore < HtmlStore + + def self.storage + SessionStorage + end + + end + end +end diff --git a/lib/volt/models/root_models/root_models.rb b/lib/volt/models/root_models/root_models.rb index ab0a6d3c..55df476c 100644 --- a/lib/volt/models/root_models/root_models.rb +++ b/lib/volt/models/root_models/root_models.rb @@ -11,7 +11,7 @@ class BaseRootModel < Volt::Model end -ROOT_MODEL_NAMES = [:Store, :Page, :Params, :Cookies, :LocalStore, :Flash] +ROOT_MODEL_NAMES = [:Store, :Page, :Params, :Cookies, :LocalStore, :SessionStore, :Flash] ROOT_MODEL_NAMES.each do |base_name| Object.const_set("#{base_name}Root", Class.new(BaseRootModel)) diff --git a/lib/volt/utils/html_storage.rb b/lib/volt/utils/html_storage.rb index 387bd83f..656435f3 100644 --- a/lib/volt/utils/html_storage.rb +++ b/lib/volt/utils/html_storage.rb @@ -2,8 +2,9 @@ module Volt module HtmlStorage + # Implement in SessionStorage and LocalStorage def self.area - nil # implement in SessionStorage and LocalStorage + raise 'should be implemented in SessionStorage or LocalStorage' end def self.[](key) diff --git a/lib/volt/utils/local_storage.rb b/lib/volt/utils/local_storage.rb index 6c925782..5f653f31 100644 --- a/lib/volt/utils/local_storage.rb +++ b/lib/volt/utils/local_storage.rb @@ -1,5 +1,5 @@ module Volt - module SessionStorage + module LocalStorage include HtmlStorage if RUBY_PLATFORM == 'opal' diff --git a/lib/volt/volt/repos.rb b/lib/volt/volt/repos.rb index 6b640444..3187fe72 100644 --- a/lib/volt/volt/repos.rb +++ b/lib/volt/volt/repos.rb @@ -32,6 +32,13 @@ def local_store end end + def session_store + @session_store ||= begin + check_for_client?('session_store') + SessionStoreRoot.new({}, persistor: Persistors::SessionStore) + end + end + def cookies @cookies ||= begin check_for_client?('cookies') From df9f43ad8685a553cb96fd3a3d2e3b705e93568b Mon Sep 17 00:00:00 2001 From: balmoral Date: Wed, 4 Nov 2015 16:42:28 +1100 Subject: [PATCH 4/8] add session_store to supplement local_store --- lib/volt/utils/html_storage.rb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/volt/utils/html_storage.rb b/lib/volt/utils/html_storage.rb index 656435f3..c4118935 100644 --- a/lib/volt/utils/html_storage.rb +++ b/lib/volt/utils/html_storage.rb @@ -9,24 +9,24 @@ def self.area def self.[](key) ` - var val = {{store}}.getItem(key); + var val = #{area}.getItem(key); return val === null ? nil : val; ` end def self.[]=(key, value) - `{{store}}.setItem(key, value)` + `#{area}.setItem(key, value)` end def self.clear - `{{store}}.clear()` + `#{area}.clear()` self end def self.delete(key) ` - var val = {{store}}.getItem(key); - {{store}}.removeItem(key); + var val = #{area}.getItem(key); + #{area}.removeItem(key); return val === null ? nil : val; ` end From 6721463a21571db9edc3d72b6bee13cba7569d28 Mon Sep 17 00:00:00 2001 From: balmoral Date: Wed, 4 Nov 2015 16:45:03 +1100 Subject: [PATCH 5/8] add session_store to supplement local_store --- lib/volt/utils/local_storage.rb | 2 +- lib/volt/utils/session_storage.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/volt/utils/local_storage.rb b/lib/volt/utils/local_storage.rb index 5f653f31..f7253151 100644 --- a/lib/volt/utils/local_storage.rb +++ b/lib/volt/utils/local_storage.rb @@ -1,6 +1,6 @@ module Volt module LocalStorage - include HtmlStorage + extend HtmlStorage if RUBY_PLATFORM == 'opal' def self.area diff --git a/lib/volt/utils/session_storage.rb b/lib/volt/utils/session_storage.rb index 0ee4baa7..55264718 100644 --- a/lib/volt/utils/session_storage.rb +++ b/lib/volt/utils/session_storage.rb @@ -1,6 +1,6 @@ module Volt module SessionStorage - include HtmlStorage + extend HtmlStorage if RUBY_PLATFORM == 'opal' def self.area From 4e7aadcb79ee31e77a8a7a0b954342261932ac52 Mon Sep 17 00:00:00 2001 From: balmoral Date: Wed, 4 Nov 2015 16:51:01 +1100 Subject: [PATCH 6/8] add session_store to supplement local_store --- lib/volt/models.rb | 1 + lib/volt/utils/local_storage.rb | 2 ++ lib/volt/utils/session_storage.rb | 2 ++ 3 files changed, 5 insertions(+) diff --git a/lib/volt/models.rb b/lib/volt/models.rb index 13cd02e0..9c8ccdfd 100644 --- a/lib/volt/models.rb +++ b/lib/volt/models.rb @@ -8,6 +8,7 @@ require 'volt/models/persistors/params' require 'volt/models/persistors/cookies' if RUBY_PLATFORM == 'opal' require 'volt/models/persistors/flash' +require 'volt/models/persistors/session_store' require 'volt/models/persistors/local_store' require 'volt/models/root_models/root_models' # require 'volt/models/root_models/store_root' diff --git a/lib/volt/utils/local_storage.rb b/lib/volt/utils/local_storage.rb index f7253151..8f93a703 100644 --- a/lib/volt/utils/local_storage.rb +++ b/lib/volt/utils/local_storage.rb @@ -1,3 +1,5 @@ +require 'volt/utils/html_storage' + module Volt module LocalStorage extend HtmlStorage diff --git a/lib/volt/utils/session_storage.rb b/lib/volt/utils/session_storage.rb index 55264718..11c3f294 100644 --- a/lib/volt/utils/session_storage.rb +++ b/lib/volt/utils/session_storage.rb @@ -1,3 +1,5 @@ +require 'volt/utils/html_storage' + module Volt module SessionStorage extend HtmlStorage From 3d1a692e5b24b97e755d43bbc76186e0ef22a930 Mon Sep 17 00:00:00 2001 From: balmoral Date: Wed, 4 Nov 2015 16:56:34 +1100 Subject: [PATCH 7/8] add session_store to supplement local_store --- lib/volt/utils/html_storage.rb | 4 ++-- lib/volt/utils/local_storage.rb | 2 +- lib/volt/utils/session_storage.rb | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/volt/utils/html_storage.rb b/lib/volt/utils/html_storage.rb index c4118935..599f8e34 100644 --- a/lib/volt/utils/html_storage.rb +++ b/lib/volt/utils/html_storage.rb @@ -1,6 +1,6 @@ if RUBY_PLATFORM == 'opal' module Volt - module HtmlStorage + class HtmlStorage # Implement in SessionStorage and LocalStorage def self.area @@ -34,7 +34,7 @@ def self.delete(key) end else module Volt - module HtmlStorage + class HtmlStorage @@store = {} def self.[](key) diff --git a/lib/volt/utils/local_storage.rb b/lib/volt/utils/local_storage.rb index 8f93a703..7f95e9b6 100644 --- a/lib/volt/utils/local_storage.rb +++ b/lib/volt/utils/local_storage.rb @@ -1,7 +1,7 @@ require 'volt/utils/html_storage' module Volt - module LocalStorage + class LocalStorage extend HtmlStorage if RUBY_PLATFORM == 'opal' diff --git a/lib/volt/utils/session_storage.rb b/lib/volt/utils/session_storage.rb index 11c3f294..921c4cc4 100644 --- a/lib/volt/utils/session_storage.rb +++ b/lib/volt/utils/session_storage.rb @@ -1,7 +1,7 @@ require 'volt/utils/html_storage' module Volt - module SessionStorage + class SessionStorage extend HtmlStorage if RUBY_PLATFORM == 'opal' From 1255c614cbacaa68a81cb1acf5799dc41c00fa1a Mon Sep 17 00:00:00 2001 From: balmoral Date: Wed, 4 Nov 2015 16:58:31 +1100 Subject: [PATCH 8/8] add session_store to supplement local_store --- lib/volt/utils/local_storage.rb | 4 ++-- lib/volt/utils/session_storage.rb | 3 +-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/volt/utils/local_storage.rb b/lib/volt/utils/local_storage.rb index 7f95e9b6..b1be6c09 100644 --- a/lib/volt/utils/local_storage.rb +++ b/lib/volt/utils/local_storage.rb @@ -1,13 +1,13 @@ require 'volt/utils/html_storage' module Volt - class LocalStorage - extend HtmlStorage + class LocalStorage < HtmlStorage if RUBY_PLATFORM == 'opal' def self.area `localStorage` end end + end end diff --git a/lib/volt/utils/session_storage.rb b/lib/volt/utils/session_storage.rb index 921c4cc4..336dd873 100644 --- a/lib/volt/utils/session_storage.rb +++ b/lib/volt/utils/session_storage.rb @@ -1,8 +1,7 @@ require 'volt/utils/html_storage' module Volt - class SessionStorage - extend HtmlStorage + class SessionStorage < HtmlStorage if RUBY_PLATFORM == 'opal' def self.area