Module: SimplyStored::Storage::InstanceMethods

Defined in:
lib/simply_stored/storage.rb

Instance Method Summary collapse

Instance Method Details

#_s3_optionsObject



4
5
6
# File 'lib/simply_stored/storage.rb', line 4

def _s3_options
  self.class._s3_options
end

#delete(*args) ⇒ Object



37
38
39
40
# File 'lib/simply_stored/storage.rb', line 37

def delete(*args)
  delete_attachments
  super
end

#delete_attachmentsObject



60
61
62
63
64
65
66
67
68
# File 'lib/simply_stored/storage.rb', line 60

def delete_attachments
  return unless id.present?
  (@_s3_attachments || {}).each do |name, attachment|
    if _s3_options[name][:after_delete] == :delete
      key = s3_bucket(name).key(s3_attachment_key(name), true)
      key.delete
    end
  end
end

#destroy(*args) ⇒ Object



42
43
44
45
# File 'lib/simply_stored/storage.rb', line 42

def destroy(*args)
  delete_attachments
  super
end

#s3_attachment_key(name) ⇒ Object



81
82
83
# File 'lib/simply_stored/storage.rb', line 81

def s3_attachment_key(name)
  "#{self.class.name.tableize}/#{name}/#{id}"
end

#s3_bucket(name) ⇒ Object



12
13
14
15
16
17
18
19
20
21
# File 'lib/simply_stored/storage.rb', line 12

def s3_bucket(name)
  if !@_s3_bucket
    @_s3_bucket = s3_connection(name).bucket(_s3_options[name][:bucket])
    location = (_s3_options[name][:location] == :eu) ? :eu : nil
    @_s3_bucket = s3_connection(name).bucket(_s3_options[name][:bucket], true, _s3_options[name][:permissions], :location => location) if @_s3_bucket.nil?
  end
  @_s3_bucket
rescue Exception => e
  raise ArgumentError, "Could not access/create S3 bucket '#{name}': #{e} #{e.backtrace.join("\n")}"
end

#s3_connection(name) ⇒ Object



8
9
10
# File 'lib/simply_stored/storage.rb', line 8

def s3_connection(name)
  @_s3_connection ||= RightAws::S3.new(_s3_options[name][:access_key], _s3_options[name][:secret_access_key], :multi_thread => true, :ca_file => _s3_options[name][:ca_file], :logger => _s3_options[name][:logger])
end

#save(validate = true) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/simply_stored/storage.rb', line 23

def save(validate = true)
  update_attachment_sizes
  if ret = super(validate)
    save_attachments
  end
  ret
end

#save!(*args) ⇒ Object



31
32
33
34
35
# File 'lib/simply_stored/storage.rb', line 31

def save!(*args)
  update_attachment_sizes
  super
  save_attachments
end

#save_attachmentsObject



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/simply_stored/storage.rb', line 47

def save_attachments
  return unless id.present?
  if @_s3_attachments
    @_s3_attachments.each do |name, attachment|
      if attachment[:dirty]
        value = attachment[:value].is_a?(String) ? attachment[:value] : attachment[:value].to_json
        s3_bucket(name).put(s3_attachment_key(name), value, {}, _s3_options[name][:permissions])
        attachment[:dirty] = false
      end
    end
  end
end

#update_attachment_sizesObject



70
71
72
73
74
75
76
77
78
79
# File 'lib/simply_stored/storage.rb', line 70

def update_attachment_sizes
  if @_s3_attachments
    @_s3_attachments.each do |name, attachment|
      if attachment[:dirty]
        value = attachment[:value].is_a?(String) ? attachment[:value] : attachment[:value].to_json
        send("#{name}_size=", (value.size rescue nil))
      end
    end
  end
end