Module: SimplyStored::Storage::ClassMethods

Included in:
Couch::ClassMethods, SimplyStored::Simple
Defined in:
lib/simply_stored/storage.rb

Instance Method Summary collapse

Instance Method Details

#define_attachment_accessors(name) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/simply_stored/storage.rb', line 100

def define_attachment_accessors(name)
  define_method(name) do
    unless @_s3_attachments and @_s3_attachments[name]
      @_s3_attachments = {name => {}}
      @_s3_attachments[name][:value] = s3_bucket(name).get(s3_attachment_key(name))
    end
    @_s3_attachments[name][:value]
  end
  
  define_method("#{name}=") do |value|
    @_s3_attachments ||= {}
    @_s3_attachments[name] ||= {}
    @_s3_attachments[name].update(:value => value, :dirty => true)
    value
  end
  
  define_method("#{name}_url") do
    if _s3_options[name][:permissions] == 'private'
      RightAws::S3Generator.new(_s3_options[name][:access_key], _s3_options[name][:secret_access_key], :multi_thread => true, :ca_file => _s3_options[name][:ca_file]).bucket(_s3_options[name][:bucket]).get(s3_attachment_key(name), 5.minutes)
    else
      "http://#{_s3_options[name][:bucket].to_s}.s3.amazonaws.com/#{s3_attachment_key(name)}"
    end
  end
end

#has_s3_attachment(name, options = {}) ⇒ Object

Raises:

  • (ArgumentError)


74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/simply_stored/storage.rb', line 74

def has_s3_attachment(name, options = {})
  require 'awsbase/right_awsbase'
  require 's3/right_s3'
  require 's3/right_s3_interface'
  
  self.class.instance_eval do
    attr_accessor :_s3_options
  end
  
  name = name.to_sym
  raise ArgumentError, "No bucket name specified for attachment #{name}" if options[:bucket].blank?
  options = {
    :permissions => 'private', 
    :ssl => true, 
    :location => :us, # use :eu for European buckets
    :ca_file => nil, # point to CA file for SSL certificate verification
    :after_delete => :nothing # or :delete to delete the item on S3 after it is deleted in the DB
  }.update(options)
  self._s3_options ||= {}
  self._s3_options[name] = options
  
  define_attachment_accessors(name)
  attr_reader :_s3_attachments
  include InstanceMethods
end