Commit 5840b757 by Prem Sichanugrist

Use more duck typing instead of class checking

        ,----,      _____________
   ___.`      `,   /             \
   `===  D     :  <  Quack Quack! |
     `'.      .'   \_____________/
        )    (                   ,
       /      \_________________/|
      /                          |
     |                           ;
     |               _____       /
     |      \       ______7    ,'
     |       \    ______7     /
      \       `-,____7      ,'   jgs
^~^~^~^`\                  /~^~^~^~^
  ~^~^~^ `----------------' ~^~^~^
 ~^~^~^~^~^^~^~^~^~^~^~^~^~^~^~^~
parent 073152ee
...@@ -119,12 +119,12 @@ module Paperclip ...@@ -119,12 +119,12 @@ module Paperclip
@s3_protocol = @options[:s3_protocol] || @s3_protocol = @options[:s3_protocol] ||
Proc.new do |style, attachment| Proc.new do |style, attachment|
permission = (@s3_permissions[style.to_s.to_sym] || @s3_permissions[:default]) permission = (@s3_permissions[style.to_s.to_sym] || @s3_permissions[:default])
permission = permission.call(attachment, style) if permission.is_a?(Proc) permission = permission.call(attachment, style) if permission.respond_to?(:call)
(permission == :public_read) ? 'http' : 'https' (permission == :public_read) ? 'http' : 'https'
end end
@s3_metadata = @options[:s3_metadata] || {} @s3_metadata = @options[:s3_metadata] || {}
@s3_headers = @options[:s3_headers] || {} @s3_headers = @options[:s3_headers] || {}
@s3_headers = @s3_headers.call(instance) if @s3_headers.is_a?(Proc) @s3_headers = @s3_headers.call(instance) if @s3_headers.respond_to?(:call)
@s3_headers = (@s3_headers).inject({}) do |headers,(name,value)| @s3_headers = (@s3_headers).inject({}) do |headers,(name,value)|
case name.to_s case name.to_s
when /^x-amz-meta-(.*)/i when /^x-amz-meta-(.*)/i
...@@ -180,19 +180,19 @@ module Paperclip ...@@ -180,19 +180,19 @@ module Paperclip
def s3_host_alias def s3_host_alias
@s3_host_alias = @options[:s3_host_alias] @s3_host_alias = @options[:s3_host_alias]
@s3_host_alias = @s3_host_alias.call(self) if @s3_host_alias.is_a?(Proc) @s3_host_alias = @s3_host_alias.call(self) if @s3_host_alias.respond_to?(:call)
@s3_host_alias @s3_host_alias
end end
def s3_url_options def s3_url_options
s3_url_options = @options[:s3_url_options] || {} s3_url_options = @options[:s3_url_options] || {}
s3_url_options = s3_url_options.call(instance) if s3_url_options.is_a?(Proc) s3_url_options = s3_url_options.call(instance) if s3_url_options.respond_to?(:call)
s3_url_options s3_url_options
end end
def bucket_name def bucket_name
@bucket = @options[:bucket] || s3_credentials[:bucket] @bucket = @options[:bucket] || s3_credentials[:bucket]
@bucket = @bucket.call(self) if @bucket.is_a?(Proc) @bucket = @bucket.call(self) if @bucket.respond_to?(:call)
@bucket or raise ArgumentError, "missing required :bucket option" @bucket or raise ArgumentError, "missing required :bucket option"
end end
...@@ -249,11 +249,8 @@ module Paperclip ...@@ -249,11 +249,8 @@ module Paperclip
end end
def set_permissions permissions def set_permissions permissions
if permissions.is_a?(Hash) permissions = { :default => permissions } unless permissions.respond_to?(:merge)
permissions[:default] = permissions[:default] || :public_read permissions[:default] ||= :public_read
else
permissions = { :default => permissions || :public_read }
end
permissions permissions
end end
...@@ -276,12 +273,12 @@ module Paperclip ...@@ -276,12 +273,12 @@ module Paperclip
def s3_permissions(style = default_style) def s3_permissions(style = default_style)
s3_permissions = @s3_permissions[style] || @s3_permissions[:default] s3_permissions = @s3_permissions[style] || @s3_permissions[:default]
s3_permissions = s3_permissions.call(self, style) if s3_permissions.is_a?(Proc) s3_permissions = s3_permissions.call(self, style) if s3_permissions.respond_to?(:call)
s3_permissions s3_permissions
end end
def s3_protocol(style = default_style) def s3_protocol(style = default_style)
protocol = if @s3_protocol.is_a?(Proc) protocol = if @s3_protocol.respond_to?(:call)
@s3_protocol.call(style, self) @s3_protocol.call(style, self)
else else
@s3_protocol @s3_protocol
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment