Class: Persistors::ArrayStore

Inherits:
Store show all
Defined in:
lib/volt/models/persistors/array_store.rb

Instance Method Summary collapse

Methods inherited from Store

#change_channel_connection, #initialize, #read_new_model

Methods inherited from Base

#changed, #event_added, #event_removed

Constructor Details

This class inherits a constructor from Persistors::Store

Instance Method Details

#added(model) ⇒ Object

When a model is added to this collection, we call its “changed” method. This should trigger a save.



53
54
55
56
57
58
59
60
61
62
# File 'lib/volt/models/persistors/array_store.rb', line 53

def added(model)      
  unless $loading_models
    model.persistor.changed
  end
  
  if model.persistor
    # Tell the persistor it was added
    model.persistor.add_to_collection
  end
end

#channel_nameObject



46
47
48
# File 'lib/volt/models/persistors/array_store.rb', line 46

def channel_name
  @model.path[-1]
end

#loadedObject

Called when a collection loads



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/volt/models/persistors/array_store.rb', line 7

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

#query(query) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/volt/models/persistors/array_store.rb', line 32

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

#removed(model) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/volt/models/persistors/array_store.rb', line 64

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