Sha256: d515173f7e1443fd08c3821b2be22d7edc805aa8f880ad343251ccc5aa5acb10

Contents?: true

Size: 1.21 KB

Versions: 22

Compression:

Stored size: 1.21 KB

Contents

require 'volt/models/persistors/base'
require 'volt/utils/local_storage'
require 'json'

module Volt
  module Persistors
    # Backs a collection in the local store
    class LocalStore < Base
      def initialize(model)
        @model = model
      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)
        # 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 = JSON.parse(json_data)

            @loading_data = true
            root_attributes.each_pair do |key, value|
              @model.send(:"_#{key}=", value)
            end
            @loading_data = nil
          end
        end
      end

      # Callled when an item is changed (or removed)
      def changed(attribute_name)
        root_model.persistor.save_all
      end

      # Called on the root
      def save_all
        return if @loading_data

        json_data = JSON.dump(@model.to_h)

        LocalStorage['volt-store'] = json_data
      end
    end
  end
end

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
volt-0.9.3.pre1 lib/volt/models/persistors/local_store.rb
volt-0.9.2 lib/volt/models/persistors/local_store.rb
volt-0.9.1 lib/volt/models/persistors/local_store.rb
volt-0.9.1.pre5 lib/volt/models/persistors/local_store.rb
volt-0.9.1.pre4 lib/volt/models/persistors/local_store.rb
volt-0.9.1.pre3 lib/volt/models/persistors/local_store.rb
volt-0.9.1.pre2 lib/volt/models/persistors/local_store.rb
volt-0.9.1.pre1 lib/volt/models/persistors/local_store.rb
volt-0.9.0 lib/volt/models/persistors/local_store.rb
volt-0.9.0.pre7 lib/volt/models/persistors/local_store.rb
volt-0.9.0.pre6 lib/volt/models/persistors/local_store.rb
volt-0.9.0.pre5 lib/volt/models/persistors/local_store.rb
volt-0.9.0.pre4 lib/volt/models/persistors/local_store.rb
volt-0.9.0.pre3 lib/volt/models/persistors/local_store.rb
volt-0.9.0.pre2 lib/volt/models/persistors/local_store.rb
volt-0.9.0.pre1 lib/volt/models/persistors/local_store.rb
volt-0.8.27.beta9 lib/volt/models/persistors/local_store.rb
volt-0.8.27.beta8 lib/volt/models/persistors/local_store.rb
volt-0.8.27.beta7 lib/volt/models/persistors/local_store.rb
volt-0.8.27.beta6 lib/volt/models/persistors/local_store.rb