motion-prime/models/sync.rb in motion-prime-0.3.3 vs motion-prime/models/sync.rb in motion-prime-0.4.0
- old
+ new
@@ -54,31 +54,31 @@
should_update = false
end
fetch_with_url url do |data, status_code|
save if sync_options[:save]
- block.call(data, status_code) if use_callback
+ block.call(data, status_code, data) if use_callback
end if should_fetch
update_with_url url, sync_options do |data, status_code|
save if sync_options[:save] && status_code.to_s =~ /20\d/
# run callback only if it wasn't run on fetch
- block.call(data, status_code) if use_callback && !should_fetch
+ block.call(data, status_code, data) if use_callback && !should_fetch
end if should_update
fetch_associations(sync_options) do |data, status_code|
# run callback only if it wasn't run on fetch or update
- block.call(data, status_code) if use_callback && !should_fetch && !should_update
+ block.call(data, status_code, data) if use_callback && !should_fetch && !should_update
end if should_fetch_associations
end
# fetch from server using url
def fetch_with_url(url, &block)
use_callback = block_given?
api_client.get(url) do |data, status_code|
fetch_with_attributes(data, &block) if data.present?
- block.call(data, status_code) if use_callback
+ block.call(data, status_code, data) if use_callback
end
end
# update on server using url
def update_with_url(url, sync_options = nil, &block)
@@ -96,11 +96,11 @@
api_client.send(method, url, post_data) do |data, status_code|
update_from_response = sync_options.has_key?(:update_from_response) ? sync_options[:update_from_response] : true
if update_from_response && status_code.to_s =~ /20\d/ && data.is_a?(Hash)
set_attributes_from_response(data)
end
- block.call(data, status_code) if use_callback
+ block.call(data, status_code, data) if use_callback
end
end
def set_attributes_from_response(data)
self.id ||= data.delete('id')
@@ -151,11 +151,11 @@
def fetch_has_many(key, options = {}, sync_options = {}, &block)
old_collection = self.send(key)
use_callback = block_given?
- puts "SYNC: started sync for #{key} in #{self.class_name_without_kvo}"
+ NSLog("SYNC: started sync for #{key} in #{self.class_name_without_kvo}")
api_client.get normalize_sync_url(options[:sync_url]) do |response, status_code|
data = options[:sync_key] && response ? response[options[:sync_key]] : response
if data
# Update/Create existing records
data.each do |attributes|
@@ -172,22 +172,22 @@
unless model
old_model.delete
end
end
save if sync_options[:save]
- puts "SYNC: finished sync for #{key} in #{self.class_name_without_kvo}"
+ NSLog("SYNC: finished sync for #{key} in #{self.class_name_without_kvo}")
block.call(data, status_code, response) if use_callback
else
- puts "SYNC ERROR: failed sync for #{key} in #{self.class_name_without_kvo}"
+ NSLog("SYNC ERROR: failed sync for #{key} in #{self.class_name_without_kvo}")
block.call(data, status_code, response) if use_callback
end
end
end
def fetch_has_one(key, options = {}, &block)
use_callback = block_given?
- puts "SYNC: started sync for #{key} in #{self.class_name_without_kvo}"
+ NSLog("SYNC: started sync for #{key} in #{self.class_name_without_kvo}")
api_client.get normalize_sync_url(options[:sync_url]) do |response, status_code|
data = options.has_key?(:sync_key) ? response[options[:sync_key]] : response
if data.present?
model = self.send(key)
unless model
@@ -196,10 +196,10 @@
end
model.fetch_with_attributes(data)
model.save if sync_options[:save]
block.call(data, status_code, response) if use_callback
else
- puts "SYNC ERROR: failed sync for #{key} in #{self.class_name_without_kvo}"
+ NSLog("SYNC ERROR: failed sync for #{key} in #{self.class_name_without_kvo}")
block.call(data, status_code, response) if use_callback
end
end
end
\ No newline at end of file