Module: SimplyStore::Storage::ClassMethods

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

Instance Method Summary collapse

Instance Method Details

#define_attachment_accessors(name) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/simply_stored/simpledb/storag.rb', line 59

def define_attachment_accessors(name)
  define_method(name) do
    unless @_attachments and @_attachments[name]
      @_attachments = {name => {}}
      @_attachments[name][:value] = s3_bucket(name).get(s3_attachment_key(name))
    end
    @_attachments[name][:value]
  end
  
  define_method("#{name}=") do |value|
    @_attachments ||= {}
    @_attachments[name] ||= {}
    @_attachments[name].update(:value => value, :dirty => true)
    value
  end
  
  define_method("#{name}_url") do
    if _s3_options[name][:permissions] == 'private'
      RightAws::S3Generator.new(AWS_CONFIG[:aws_access_key_id], AWS_CONFIG[:aws_secret_access_key], :multi_thread => true).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)


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

def has_s3_attachment(name, options = {})
  name = name.to_sym
  raise ArgumentError, "No bucket name specified for attachment #{name}" if options[:bucket].blank?
  options.update(:permissions => 'private', :ssl => true)
  self._s3_options ||= {}
  self._s3_options[name] = options
  
  define_attachment_accessors(name)
  attr_reader :_attachments
end