Skip to content

Instantly share code, notes, and snippets.

@benfalk
Last active August 29, 2015 14:06
Show Gist options
  • Save benfalk/deadc6b7e10c9e12c57d to your computer and use it in GitHub Desktop.
Save benfalk/deadc6b7e10c9e12c57d to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'active_record'
require 'pstore'
ActiveRecord::Base.logger = Logger.new(STDERR)
ActiveRecord::Base.establish_connection(
:adapter => "sqlite3",
:database => ":memory:"
)
ActiveRecord::Schema.define do
create_table :narratives do |table|
table.column :title, :string
end
end
class Narrative < ActiveRecord::Base
after_save :prepare_sync_to_sol
def prepare_sync_to_sol
SuperSyncr™.batch.transaction do
SuperSyncr™.batch["narrative.#{id}"] = attributes
end
end
end
class SuperSyncr™
class Failure < Exception; end
def self.batch
Thread.current[:super_syncr_batch]
end
def initialize(batch_id, &block)
@batch = PStore.new("super_syncr_#{batch_id}.pstore")
Thread.current[:super_syncr_batch] = @batch
sync &block if block_given?
end
def sync
ActiveRecord::Base.transaction do
yield
process_batch!
end
rescue SuperSyncr™::Failure => e
puts 'Sorry Bro, SYNC Failed.'
end
def process_batch!
batch.transaction(true) do
batch.roots.each do |root|
puts "Syncing [#{root}]:: #{batch[root]}"
fail SuperSyncr™::Failure if "#{batch[root]}" =~ /28/
end
end
end
private
attr_accessor :batch
end
SuperSyncr™.new('super_batch_007') do
30.times { Narrative.create title: 'Gone with the wind' }
end
puts Narrative.count # => 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment