Sha256: 52a771b721afb44dc9dfae028dc4680cbad518e44863fc4ebd4c09571c419989

Contents?: true

Size: 1.35 KB

Versions: 7

Compression:

Stored size: 1.35 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

    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

    promise
  end

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

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

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
volt-0.8.27.beta3 app/volt/tasks/store_tasks.rb
volt-0.8.27.beta2 app/volt/tasks/store_tasks.rb
volt-0.8.27.beta1 app/volt/tasks/store_tasks.rb
volt-0.8.26.beta1 app/volt/tasks/store_tasks.rb
volt-0.8.26 app/volt/tasks/store_tasks.rb
volt-0.8.24 app/volt/tasks/store_tasks.rb
volt-0.8.23 app/volt/tasks/store_tasks.rb