Sha256: d911f54086ffbb98f9ac29311b8368972806422208f6ec42c0511d66089f2d51

Contents?: true

Size: 1.34 KB

Versions: 24

Compression:

Stored size: 1.34 KB

Contents

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

module Persistors

  # Backs a collection in the local store
  class LocalStore < Base
    def initialize(model)
      @model = model
    end

    # Find the root for this model
    def root_model
      node = @model

      loop do
        parent = node.parent
        if parent
          node = parent
        else
          break
        end
      end

      return node
    end

    # Called when a model is added to the collection
    def added(model, index)
      root_model.persistor.save_all
    end

    def loaded
      # 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.deep_cur)

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

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
volt-0.8.1 lib/volt/models/persistors/local_store.rb
volt-0.8.0 lib/volt/models/persistors/local_store.rb
volt-0.7.23 lib/volt/models/persistors/local_store.rb
volt-0.7.22 lib/volt/models/persistors/local_store.rb
volt-0.7.21 lib/volt/models/persistors/local_store.rb
volt-0.7.20 lib/volt/models/persistors/local_store.rb
volt-0.7.19 lib/volt/models/persistors/local_store.rb
volt-0.7.18 lib/volt/models/persistors/local_store.rb
volt-0.7.17 lib/volt/models/persistors/local_store.rb
volt-0.7.16 lib/volt/models/persistors/local_store.rb
volt-0.7.15 lib/volt/models/persistors/local_store.rb
volt-0.7.14 lib/volt/models/persistors/local_store.rb
volt-0.7.13 lib/volt/models/persistors/local_store.rb
volt-0.7.12 lib/volt/models/persistors/local_store.rb
volt-0.7.10 lib/volt/models/persistors/local_store.rb
volt-0.7.9 lib/volt/models/persistors/local_store.rb
volt-0.7.8 lib/volt/models/persistors/local_store.rb
volt-0.7.7 lib/volt/models/persistors/local_store.rb
volt-0.7.6 lib/volt/models/persistors/local_store.rb
volt-0.7.5 lib/volt/models/persistors/local_store.rb