Class: StoreTasks

Inherits:
Volt::TaskHandler show all
Defined in:
app/volt/tasks/store_tasks.rb

Instance Method Summary collapse

Methods inherited from Volt::TaskHandler

inherited, known_handlers, method_missing, #store

Constructor Details

#initialize(channel = nil, dispatcher = nil) ⇒ StoreTasks

Returns a new instance of StoreTasks.



5
6
7
8
# File 'app/volt/tasks/store_tasks.rb', line 5

def initialize(channel = nil, dispatcher = nil)
  @channel = channel
  @dispatcher = dispatcher
end

Instance Method Details

#dbObject



10
11
12
# File 'app/volt/tasks/store_tasks.rb', line 10

def db
  @@db ||= Volt::DataStore.fetch
end

#delete(collection, id) ⇒ Object



61
62
63
64
65
# File 'app/volt/tasks/store_tasks.rb', line 61

def delete(collection, id)
  db[collection].remove('_id' => id)

  QueryTasks.live_query_pool.updated_collection(collection, @channel)
end

#load_model(collection, path, data) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'app/volt/tasks/store_tasks.rb', line 14

def load_model(collection, path, data)
  puts "Load Model: #{path.inspect}"
  model_name = collection.singularize.camelize

  # TODO: Security check to make sure we have a valid model
  # and don't load classes we shouldn't
  begin
    model_class = Object.send(:const_get, model_name)
  rescue NameError => e
    model_class = Volt::Model
  end

  # Load the model, use the Store persistor and set the path
  model = model_class.new({}, persistor: Volt::Persistors::StoreFactory.new(nil), path: path)
  model.persistor.change_state_to(:loaded)

  # Create a buffer
  buffer = model.buffer

  # Assign the data
  buffer.attributes = data

  return buffer
end

#save(collection, path, data) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'app/volt/tasks/store_tasks.rb', line 39

def save(collection, path, data)
  data = data.symbolize_keys
  model = nil
  Volt::Model.nosave do
    model = load_model(collection, path, data)
  end

  # On the backend, the promise is resolved before its returned, so we can
  # return from within it.
  #
  # Pass the channel as a thread-local so that we don't update the client
  # who sent the update.
  Thread.current['in_channel'] = @channel
  promise = model.save!.then do |result|
    return nil
  end

  Thread.current['in_channel'] = nil

  return promise
end