Module: Paperclip::Storage::Riak

Defined in:
lib/paperclip-riak.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object



8
9
10
11
12
13
14
# File 'lib/paperclip-riak.rb', line 8

def self.extended(base)
  attr_accessor :riak_hosts, :riak_bucket, :riak_endpoint, :riak_client

  base.instance_eval do
    self.setup_options
  end
end

Instance Method Details

#bucketObject



28
29
30
# File 'lib/paperclip-riak.rb', line 28

def bucket
  @bucket ||= riak.bucket(@riak_bucket)
end

#copy_to_local_file(style, local_dest_path) ⇒ Object



66
67
68
69
70
71
# File 'lib/paperclip-riak.rb', line 66

def copy_to_local_file(style, local_dest_path)
  ::File.open(local_dest_path, 'wb') do |local_file|
    file = bucket.get(path(style))
    local_file.write(file.raw_data)
  end
end

#exists?(style = default_style) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
44
45
46
47
# File 'lib/paperclip-riak.rb', line 41

def exists?(style = default_style)
  if original_filename
    bucket.exists?(path(style))
  else
    false
  end
end

#flush_deletesObject



59
60
61
62
63
64
# File 'lib/paperclip-riak.rb', line 59

def flush_deletes
  @queued_for_delete.each do |path|
    bucket.delete path
  end
  @queued_for_delete = []
end

#flush_writesObject



49
50
51
52
53
54
55
56
57
# File 'lib/paperclip-riak.rb', line 49

def flush_writes
  @queued_for_write.each do |style, file|
    object = ::Riak::RObject.new(bucket, path(style))
    object.raw_data = File.read(file.path)
    object.content_type = file.content_type.to_s.strip
    object.store
  end
  @queued_for_write = {}
end

#riakObject



24
25
26
# File 'lib/paperclip-riak.rb', line 24

def riak
  @riak ||= @riak_client || ::Riak::Client.new(@client_options)
end

#setup_optionsObject



32
33
34
35
36
37
38
39
# File 'lib/paperclip-riak.rb', line 32

def setup_options
  @client_options = {
    :nodes => @options[:riak_hosts]
  }
  @riak_bucket = @options[:riak_bucket]
  @riak_endpoint = @options[:riak_endpoint]
  @riak_client = @options[:riak_client]
end

#url(style = default_style, options = {}) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/paperclip-riak.rb', line 16

def url(style=default_style, options={})
  if @riak_endpoint
    "#{@riak_endpoint}/#{@riak_bucket}/#{path(style)}"
  else
    @url_generator.for(style, options)
  end
end