class ReactiveRecord::ServerDataCache

Todo, [find, 119], owner, todos, active, *all

-> [[Todo, [find, 119], owner, todos, active, *all], [119, 123], 119, 12]

Public Class Methods

[](models, associations, vectors, acting_user) click to toggle source
# File lib/reactive_record/server_data_cache.rb, line 108
def self.[](models, associations, vectors, acting_user)
  ActiveRecord::Base.public_columns_hash
  result = nil
  ActiveRecord::Base.transaction do
    cache = new(acting_user, ReactiveRecord::Base.save_records(models, associations, acting_user, false, false))
    vectors.each { |vector| cache[*vector] }
    result = cache.as_json
    raise ActiveRecord::Rollback
  end
  result
end
load_from_json(tree, target = nil) click to toggle source
# File lib/reactive_record/server_data_cache.rb, line 293
def self.load_from_json(tree, target = nil)
  ignore_all = nil

  # have to process *all before any other items
  # we leave the "*all" key in just for debugging purposes, and then skip it below

  if sorted_collection = tree["*all"]
    target.replace sorted_collection.collect { |id| target.proxy_association.klass.find(id) }
  end

  if id_value = tree["id"] and id_value.is_a? Array
    target.id = id_value.first
  end
  tree.each do |method, value|
    method = JSON.parse(method) rescue method
    new_target = nil
    if method == "*all"
      next # its already been processed above
    elsif !target
      load_from_json(value, Object.const_get(method))
    elsif method == "*count"
      target.set_count_state(value.first)
    elsif method.is_a? Integer or method =~ /^[0-9]+$/
      new_target = target.push_and_update_belongs_to(method)
      #target << (new_target = target.proxy_association.klass.find(method))
    elsif method.is_a? Array
      if method[0] == "new"
        new_target = ReactiveRecord::Base.find_by_object_id(target.base_class, method[1])
      elsif !(target.class < ActiveRecord::Base)
        new_target = target.send *method
        # value is an array if scope returns nil, so we destroy the bogus record
        new_target.destroy and new_target = nil if value.is_a? Array
      else
        target.backing_record.update_attribute([method], target.backing_record.convert(method, value.first))
      end
    elsif target.class.respond_to?(:reflect_on_aggregation) and aggregation = target.class.reflect_on_aggregation(method) and
    !(aggregation.klass < ActiveRecord::Base)
      target.send "#{method}=", aggregation.deserialize(value.first)
    elsif value.is_a? Array
      target.send "#{method}=", value.first unless method == "id" # we handle ids first so things sync nicely
    elsif value.is_a? Hash and value[:id] and value[:id].first and association = target.class.reflect_on_association(method)
      # not sure if its necessary to check the id above... is it possible to for the method to be an association but not have an id?
      new_target = association.klass.find(value[:id].first)
      target.send "#{method}=", new_target
    elsif !(target.class < ActiveRecord::Base)
      new_target = target.send *method
      # value is an array if scope returns nil, so we destroy the bogus record
      new_target.destroy and new_target = nil if value.is_a? Array
    else
      new_target = target.send("#{method}=", target.send(method))
    end
    load_from_json(value, new_target) if new_target
  end
end
new(acting_user, preloaded_records) click to toggle source
# File lib/reactive_record/server_data_cache.rb, line 90
def initialize(acting_user, preloaded_records)
  @acting_user = acting_user
  @cache = []
  @requested_cache_items = []
  @preloaded_records = preloaded_records
end

Public Instance Methods

[](*vector) click to toggle source
# File lib/reactive_record/server_data_cache.rb, line 99
def [](*vector)
  root = CacheItem.new(@cache, @acting_user, vector[0], @preloaded_records)
  vector[1..-1].inject(root) { |cache_item, method| cache_item.apply_method method if cache_item }
  vector[0] = vector[0].constantize
  new_items = @cache.select { | cache_item | cache_item.root == root }
  @requested_cache_items += new_items
  new_items.last.value if new_items.last
end
as_json() click to toggle source
# File lib/reactive_record/server_data_cache.rb, line 124
def as_json
  @requested_cache_items.inject({}) do | hash, cache_item|
    hash.deep_merge! cache_item.as_hash
  end
end
clear_requests() click to toggle source
# File lib/reactive_record/server_data_cache.rb, line 120
def clear_requests
  @requested_cache_items = []
end
detect(&block) click to toggle source
# File lib/reactive_record/server_data_cache.rb, line 132
def detect(&block); @cache.detect &block; end
inject(initial, &block) click to toggle source
# File lib/reactive_record/server_data_cache.rb, line 134
def inject(initial, &block); @cache.inject(initial) &block; end
select(&block) click to toggle source
# File lib/reactive_record/server_data_cache.rb, line 130
def select(&block); @cache.select &block; end