Sha256: d5f4a5f0298f299020e042debfbf13c460ee7c2ecbc0c50deaca93b2e73ebb53

Contents?: true

Size: 1.36 KB

Versions: 3

Compression:

Stored size: 1.36 KB

Contents

require 'mongo'
require 'volt/models'

class StoreTasks < Volt::TaskHandler
  def initialize(channel = nil, dispatcher = nil)
    @channel = channel
    @dispatcher = dispatcher
  end

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

  def load_model(collection, path, data)
    model_name = collection.singularize.camelize

    # Fetch the model
    collection = store.send(:"_#{path[-2]}")

    # See if the model has already been made
    model = collection.find_one({_id: data[:_id]})

    # Otherwise assign to the collection
    model ||= collection


    # Create a buffer
    buffer = model.buffer

    # Assign the data
    buffer.attributes = data

    return buffer
  end

  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

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

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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
volt-0.8.22 app/volt/tasks/store_tasks.rb
volt-0.8.22.beta2 app/volt/tasks/store_tasks.rb
volt-0.8.22.beta1 app/volt/tasks/store_tasks.rb