Module: SimplyStore::Storage

Defined in:
lib/simply_stored/simpledb/storag.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



3
4
5
6
7
8
# File 'lib/simply_stored/simpledb/storag.rb', line 3

def self.included(base)
  base.extend(ClassMethods)
  base.class_eval do
    cattr_accessor :_s3_options
  end
end

Instance Method Details

#s3_attachment_key(name) ⇒ Object



43
44
45
# File 'lib/simply_stored/simpledb/storag.rb', line 43

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

#s3_bucket(name) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'lib/simply_stored/simpledb/storag.rb', line 14

def s3_bucket(name)
  if !@_s3_bucket
    @_s3_bucket = s3_connection.bucket(_s3_options[name][:bucket])
    @_s3_bucket = s3_connection.bucket(_s3_options[name][:bucket], true, 'private') if @_s3_bucket.nil? # create it if it didn't exist, do not do it the first time as the ACL get modified
  end
  @_s3_bucket
rescue Exception => e
  raise ArgumentError, "Could not access/create S3 bucket '#{name}': #{e} #{e.backtrace.join("\n")}"
end

#s3_connectionObject



10
11
12
# File 'lib/simply_stored/simpledb/storag.rb', line 10

def s3_connection
  @_s3_connection ||= RightAws::S3.new(AWS_CONFIG[:aws_access_key_id], AWS_CONFIG[:aws_secret_access_key], :multi_thread => true)
end

#saveObject



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

def save
  if ret = super
    save_attachments
  end
  ret
end

#save_attachmentsObject



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/simply_stored/simpledb/storag.rb', line 31

def save_attachments
  if @_attachments
    @_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