Sha256: 0d3b0fe79eef2888a52863dd6d5cece60e2dbb91b8f7bd22185f9b2fe0c93613

Contents?: true

Size: 1.93 KB

Versions: 17

Compression:

Stored size: 1.93 KB

Contents

require 'volt/models/persistors/store'

module Persistors
  class ArrayStore < Store
    
    # Called when a collection loads
    def loaded
      scope = {}
    
    
    
      # Scope to the parent
      if @model.path.size > 1
        parent = @model.parent
        
        parent.persistor.ensure_setup if parent.persistor
        puts @model.parent.inspect
        
        if parent && (attrs = parent.attributes) && attrs[:_id].true?
          scope[:"#{@model.path[-3].singularize}_id"] = attrs[:_id]
        end
      end
      
      puts "Load At Scope: #{scope.inspect}"
      
      query(scope)
      
      change_channel_connection('add', 'added')
      change_channel_connection('add', 'removed')
    end
    
    def query(query)
      @tasks.call('StoreTasks', 'find', @model.path.last, query) do |results|
        # TODO: Globals evil, replace
        $loading_models = true
        
        new_options = @model.options.merge(path: @model.path + [:[]], parent: @model)
        
        results.each do |result|
          @model << Model.new(result, new_options)
        end
        $loading_models = false
      end
    end
    
    def channel_name
      @model.path[-1]
    end
    
    
    # When a model is added to this collection, we call its "changed"
    # method.  This should trigger a save.
    def added(model)
      unless defined?($loading_models) && $loading_models
        model.persistor.changed
      end
      
      if model.persistor
        # Tell the persistor it was added
        model.persistor.add_to_collection
      end
    end
    
    def removed(model)      
      if model.persistor
        # Tell the persistor it was removed
        model.persistor.remove_from_collection
      end
      
      if $loading_models
        return
      else
        puts "delete #{channel_name} - #{model.attributes[:_id]}"
        @tasks.call('StoreTasks', 'delete', channel_name, model.attributes[:_id])
      end
    end
    
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
volt-0.5.18 lib/volt/models/persistors/array_store.rb
volt-0.5.17 lib/volt/models/persistors/array_store.rb
volt-0.5.16 lib/volt/models/persistors/array_store.rb
volt-0.5.15 lib/volt/models/persistors/array_store.rb
volt-0.5.14 lib/volt/models/persistors/array_store.rb
volt-0.5.13 lib/volt/models/persistors/array_store.rb
volt-0.5.12 lib/volt/models/persistors/array_store.rb
volt-0.5.11 lib/volt/models/persistors/array_store.rb
volt-0.5.10 lib/volt/models/persistors/array_store.rb
volt-0.5.9 lib/volt/models/persistors/array_store.rb
volt-0.5.8 lib/volt/models/persistors/array_store.rb
volt-0.5.7 lib/volt/models/persistors/array_store.rb
volt-0.5.6 lib/volt/models/persistors/array_store.rb
volt-0.5.4 lib/volt/models/persistors/array_store.rb
volt-0.5.3 lib/volt/models/persistors/array_store.rb
volt-0.5.2 lib/volt/models/persistors/array_store.rb
volt-0.5.1 lib/volt/models/persistors/array_store.rb