Permalink
Browse files

Remove the Options class, replacing it with a hash.

  • Loading branch information...
1 parent 2c08152 commit 5a7769b61f403b4598fa905d20e5da083dc96df2 @mike-burns mike-burns committed Dec 2, 2011
View
@@ -28,7 +28,6 @@
require 'erb'
require 'digest'
require 'tempfile'
-require 'paperclip/options'
require 'paperclip/version'
require 'paperclip/upfile'
require 'paperclip/iostream'
@@ -33,6 +33,7 @@ def self.default_options
end
attr_reader :name, :instance, :default_style, :convert_options, :queued_for_write, :whiny, :options, :interpolator
+ attr_reader :source_file_options, :whiny
attr_accessor :post_processing
# Creates an Attachment object. +name+ is the name of the attachment,
@@ -66,26 +67,20 @@ def initialize(name, instance, options = {})
options = self.class.default_options.merge(options)
- @options = Paperclip::Options.new(self, options)
+ @options = options
@post_processing = true
@queued_for_delete = []
@queued_for_write = {}
@errors = {}
@dirty = false
@interpolator = options[:interpolator]
@url_generator = options[:url_generator].new(self, @options)
+ @source_file_options = options[:source_file_options]
+ @whiny = options[:whiny]
initialize_storage
end
- # [:url, :path, :only_process, :normalized_styles, :default_url, :default_style,
- # :storage, :use_timestamp, :whiny, :use_default_time_zone, :hash_digest, :hash_secret,
- # :convert_options, :preserve_files].each do |field|
- # define_method field do
- # @options.send(field)
- # end
- # end
-
# What gets called when you call instance.attachment = File. It clears
# errors, assigns attributes, and processes the file. It
# also queues up the previous file for deletion, to be flushed away on
@@ -120,7 +115,7 @@ def assign uploaded_file
@dirty = true
- post_process(*@options.only_process) if post_processing
+ post_process(*@options[:only_process]) if post_processing
# Reset the file size if the original file was reprocessed.
instance_write(:file_size, @queued_for_write[:original].size.to_i)
@@ -149,10 +144,10 @@ def assign uploaded_file
# As mentioned just above, the object that generates this URL can be passed
# in, for finer control. This object must respond to two methods:
#
- # +#new(Paperclip::Attachment, Paperclip::Options)+
+ # +#new(Paperclip::Attachment, options_hash)+
# +#for(style_name, options_hash)+
def url(style_name = default_style, options = {})
- default_options = {:timestamp => @options.use_timestamp, :escape => true}
+ default_options = {:timestamp => @options[:use_timestamp], :escape => true}
if options == true || options == false # Backwards compatibility.
@url_generator.for(style_name, default_options.merge(:timestamp => options))
@@ -166,7 +161,7 @@ def url(style_name = default_style, options = {})
# on disk. If the file is stored in S3, the path is the "key" part of the
# URL, and the :bucket option refers to the S3 bucket.
def path(style_name = default_style)
- path = original_filename.nil? ? nil : interpolate(@options.path, style_name)
+ path = original_filename.nil? ? nil : interpolate(path_option, style_name)
path.respond_to?(:unescape) ? path.unescape : path
end
@@ -176,11 +171,28 @@ def to_s style_name = default_style
end
def default_style
- @options.default_style
+ @options[:default_style]
end
def styles
- @options.styles
+ styling_option = @options[:styles]
+ if styling_option.respond_to?(:call) || !@normalized_styles
+ @normalized_styles = ActiveSupport::OrderedHash.new
+ (styling_option.respond_to?(:call) ? styling_option.call(self) : styling_option).each do |name, args|
+ @normalized_styles[name] = Paperclip::Style.new(name, args.dup, self)
+ end
+ end
+ @normalized_styles
+ end
+
+ def processors
+ processing_option = @options[:processors]
+
+ if processing_option.respond_to?(:call)
+ processing_option.call(instance)
+ else
+ processing_option
+ end
end
# Returns an array containing the errors on this attachment.
@@ -215,7 +227,7 @@ def clear
# nil to the attachment *and saving*. This is permanent. If you wish to
# wipe out the existing attachment but not save, use #clear.
def destroy
- unless @options.preserve_files
+ unless @options[:preserve_files]
clear
save
end
@@ -260,16 +272,16 @@ def updated_at
# The time zone to use for timestamp interpolation. Using the default
# time zone ensures that results are consistent across all threads.
def time_zone
- @options.use_default_time_zone ? Time.zone_default : Time.zone
+ @options[:use_default_time_zone] ? Time.zone_default : Time.zone
end
# Returns a unique hash suitable for obfuscating the URL of an otherwise
# publicly viewable attachment.
def hash(style_name = default_style)
- raise ArgumentError, "Unable to generate hash without :hash_secret" unless @options.hash_secret
+ raise ArgumentError, "Unable to generate hash without :hash_secret" unless @options[:hash_secret]
require 'openssl' unless defined?(OpenSSL)
- data = interpolate(@options.hash_data, style_name)
- OpenSSL::HMAC.hexdigest(OpenSSL::Digest.const_get(@options.hash_digest).new, @options.hash_secret, data)
+ data = interpolate(@options[:hash_data], style_name)
+ OpenSSL::HMAC.hexdigest(OpenSSL::Digest.const_get(@options[:hash_digest]).new, @options[:hash_secret], data)
end
def generate_fingerprint(source)
@@ -350,6 +362,10 @@ def instance_read(attr)
private
+ def path_option
+ @options[:path].respond_to?(:call) ? @options[:path].call(self) : @options[:path]
+ end
+
def ensure_required_accessors! #:nodoc:
%w(file_name).each do |field|
unless @instance.respond_to?("#{name}_#{field}") && @instance.respond_to?("#{name}_#{field}=")
@@ -367,7 +383,7 @@ def valid_assignment? file #:nodoc:
end
def initialize_storage #:nodoc:
- storage_class_name = @options.storage.to_s.downcase.camelize
+ storage_class_name = @options[:storage].to_s.downcase.camelize
begin
storage_module = Paperclip::Storage.const_get(storage_class_name)
rescue NameError
@@ -377,18 +393,18 @@ def initialize_storage #:nodoc:
end
def extra_options_for(style) #:nodoc:
- all_options = @options.convert_options[:all]
+ all_options = @options[:convert_options][:all]
all_options = all_options.call(instance) if all_options.respond_to?(:call)
- style_options = @options.convert_options[style]
+ style_options = @options[:convert_options][style]
style_options = style_options.call(instance) if style_options.respond_to?(:call)
[ style_options, all_options ].compact.join(" ")
end
def extra_source_file_options_for(style) #:nodoc:
- all_options = @options.source_file_options[:all]
+ all_options = @options[:source_file_options][:all]
all_options = all_options.call(instance) if all_options.respond_to?(:call)
- style_options = @options.source_file_options[style]
+ style_options = @options[:source_file_options][style]
style_options = style_options.call(instance) if style_options.respond_to?(:call)
[ style_options, all_options ].compact.join(" ")
@@ -404,7 +420,7 @@ def post_process(*style_args) #:nodoc:
end
def post_process_styles(*style_args) #:nodoc:
- @options.styles.each do |name, style|
+ styles.each do |name, style|
begin
if style_args.empty? || style_args.include?(name)
raise RuntimeError.new("Style #{name} has no processors defined.") if style.processors.blank?
@@ -414,7 +430,7 @@ def post_process_styles(*style_args) #:nodoc:
end
rescue PaperclipError => e
log("An error was received while processing: #{e.inspect}")
- (@errors[:processing] ||= []) << e.message if @options.whiny
+ (@errors[:processing] ||= []) << e.message if @options[:whiny]
end
end
end
@@ -424,8 +440,8 @@ def interpolate(pattern, style_name = default_style) #:nodoc:
end
def queue_existing_for_delete #:nodoc:
- return if @options.preserve_files || !file?
- @queued_for_delete += [:original, *@options.styles.keys].uniq.map do |style|
+ return if @options[:preserve_files] || !file?
+ @queued_for_delete += [:original, *styles.keys].uniq.map do |style|
path(style) if exists?(style)
end.compact
instance_write(:file_name, nil)
View
@@ -1,81 +0,0 @@
-module Paperclip
- class Options
-
- attr_accessor :url, :path, :only_process, :normalized_styles, :default_url, :default_style,
- :storage, :use_timestamp, :whiny, :use_default_time_zone, :hash_digest, :hash_secret,
- :convert_options, :source_file_options, :preserve_files, :http_proxy
-
- attr_accessor :s3_credentials, :s3_host_name, :s3_options, :s3_permissions, :s3_protocol,
- :s3_headers, :s3_host_alias, :bucket
-
- attr_accessor :fog_directory, :fog_credentials, :fog_host, :fog_public, :fog_file
-
- def initialize(attachment, hash)
- @attachment = attachment
-
- @url = hash[:url]
- @url = @url.call(@attachment) if @url.is_a?(Proc)
- @path = hash[:path]
- @path = @path.call(@attachment) if @path.is_a?(Proc)
- @styles = hash[:styles]
- @only_process = hash[:only_process]
- @normalized_styles = nil
- @default_url = hash[:default_url]
- @default_style = hash[:default_style]
- @storage = hash[:storage]
- @use_timestamp = hash[:use_timestamp]
- @whiny = hash[:whiny_thumbnails] || hash[:whiny]
- @use_default_time_zone = hash[:use_default_time_zone]
- @hash_digest = hash[:hash_digest]
- @hash_data = hash[:hash_data]
- @hash_secret = hash[:hash_secret]
- @convert_options = hash[:convert_options]
- @source_file_options = hash[:source_file_options]
- @processors = hash[:processors]
- @preserve_files = hash[:preserve_files]
- @http_proxy = hash[:http_proxy]
- @interpolator = hash[:interpolator]
- @escape = hash[:escape]
- @url_generator = hash[:url_generator]
-
- #s3 options
- @s3_credentials = hash[:s3_credentials]
- @s3_host_name = hash[:s3_host_name]
- @bucket = hash[:bucket]
- @s3_options = hash[:s3_options]
- @s3_permissions = hash[:s3_permissions]
- @s3_protocol = hash[:s3_protocol]
- @s3_headers = hash[:s3_headers]
- @s3_host_alias = hash[:s3_host_alias]
-
- #fog options
- @fog_directory = hash[:fog_directory]
- @fog_credentials = hash[:fog_credentials]
- @fog_host = hash[:fog_host]
- @fog_public = hash[:fog_public]
- @fog_file = hash[:fog_file]
- end
-
- def method_missing(method, *args, &blk)
- if method.to_s[-1,1] == "="
- instance_variable_set("@#{method[0..-2]}", args[0])
- else
- instance_variable_get("@#{method}")
- end
- end
-
- def processors
- @processors.respond_to?(:call) ? @processors.call(@attachment.instance) : @processors
- end
-
- def styles
- if @styles.respond_to?(:call) || !@normalized_styles
- @normalized_styles = ActiveSupport::OrderedHash.new
- (@styles.respond_to?(:call) ? @styles.call(@attachment) : @styles).each do |name, args|
- normalized_styles[name] = Paperclip::Style.new(name, args.dup, @attachment)
- end
- end
- @normalized_styles
- end
- end
-end
@@ -41,9 +41,9 @@ def self.extended base
end unless defined?(Fog)
base.instance_eval do
- unless @options.url.to_s.match(/^:fog.*url$/)
- @options.path = @options.path.gsub(/:url/, @options.url)
- @options.url = ':fog_public_url'
+ unless @options[:url].to_s.match(/^:fog.*url$/)
+ @options[:path] = @options[:path].gsub(/:url/, @options[:url])
+ @options[:url] = ':fog_public_url'
end
Paperclip.interpolates(:fog_public_url) do |attachment, style|
attachment.public_url(style)
@@ -60,16 +60,16 @@ def exists?(style = default_style)
end
def fog_credentials
- @fog_credentials ||= parse_credentials(@options.fog_credentials)
+ @fog_credentials ||= parse_credentials(@options[:fog_credentials])
end
def fog_file
- @fog_file ||= @options.fog_file || {}
+ @fog_file ||= @options[:fog_file] || {}
end
def fog_public
return @fog_public if defined?(@fog_public)
- @fog_public = defined?(@options.fog_public) ? @options.fog_public : true
+ @fog_public = defined?(@options[:fog_public]) ? @options[:fog_public] : true
end
def flush_writes
@@ -122,8 +122,8 @@ def to_file(style = default_style)
end
def public_url(style = default_style)
- if @options.fog_host
- host = (@options.fog_host =~ /%d/) ? @options.fog_host % (path(style).hash % 4) : @options.fog_host
+ if @options[:fog_host]
+ host = (@options[:fog_host] =~ /%d/) ? @options[:fog_host] % (path(style).hash % 4) : @options[:fog_host]
"#{host}/#{path(style)}"
else
directory.files.new(:key => path(style)).public_url
@@ -156,7 +156,7 @@ def connection
end
def directory
- @directory ||= connection.directories.new(:key => @options.fog_directory)
+ @directory ||= connection.directories.new(:key => @options[:fog_directory])
end
end
end
Oops, something went wrong.

0 comments on commit 5a7769b

Please sign in to comment.