Module: SimplyStored::Storage::ClassMethods

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

Instance Method Summary collapse

Instance Method Details

#define_attachment_accessors(name) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/simply_stored/storage.rb', line 121

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)


87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/simply_stored/storage.rb', line 87

def has_s3_attachment(name, options = {})
  require 'right_aws'
  
  name = name.to_sym
  
  self.class.instance_eval do
    attr_accessor :_s3_options
  end
  
  self.class_eval do
    if respond_to?(:property)
      property "#{name}_size"
    else
      simpledb_integer "#{name}_size"
    end
  end
  
  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,
    :logger => nil # use the default RightAws logger (stdout)
  }.update(options)
  self._s3_options ||= {}
  self._s3_options[name] = options
  
  define_attachment_accessors(name)
  attr_reader :_s3_attachments
  include InstanceMethods
end