Skip to content

Instantly share code, notes, and snippets.

@gerep
Last active December 25, 2015 21:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gerep/7042091 to your computer and use it in GitHub Desktop.
Save gerep/7042091 to your computer and use it in GitHub Desktop.
# HERE IS HOW I USE RIAKWRAPPER
#
def destroy_on_riak
# DELETING ON RIAK
acronym = current_company.acronym
wrapper = RiakWrapper.new(walk_server_client)
wrapper.delete("#{acronym}_#{self.number}", 'terminals')
wrapper.delete("#{acronym}_#{self.number}_params.dat", 'assets')
wrapper.delete("#{acronym}_#{self.number}_params.dat", 'files')
end
# THIS IS MY CALLBACK
#
class RiakCallback
def before_destroy(record)
raise 'Invalid Riak Client' unless record.walk_server_client.is_a? Riak::Client
@client = record.walk_server_client
record.riak_keys.each do |key|
delete(key[0], key[1])
end
end
def exists?(key, bucket_name)
bucket(bucket_name).exists?(key)
end
def delete(key, bucket_name)
bucket(bucket_name).delete(key) if exists?(key, bucket_name)
end
def bucket(bucket_name)
@client.bucket(bucket_name)
end
end
class RiakWrapper
def initialize(client)
raise 'Invalid Riak client' unless client.is_a?(Riak::Client)
@client = client
end
def exists?(key, bucket_name)
bucket(bucket_name).exists?(key)
end
def get(key, bucket_name)
bucket(bucket_name).get(key)
raise Riak::ProtobuffsFailedRequest => e
false
end
def delete(key, bucket_name)
bucket(bucket_name).delete(key) if exists?(key, bucket_name)
end
def bucket(bucket_name)
@client.bucket(bucket_name)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment