# encoding: utf-8modulePaperclip# The Style class holds the definition of a thumbnail style, applying# whatever processing is required to normalize the definition and delaying# the evaluation of block parameters until useful context is available.classStyleattr_reader:name,:attachment,:format# Creates a Style object. +name+ is the name of the attachment,# +definition+ is the style definition from has_attached_file, which# can be string, array or hashdefinitializename,definition,attachment@name=name@attachment=attachmentifdefinition.is_a?Hash@geometry=definition.delete(:geometry)@format=definition.delete(:format)@processors=definition.delete(:processors)@convert_options=definition.delete(:convert_options)@source_file_options=definition.delete(:source_file_options)@other_args=definitionelsifdefinition.is_a?String@geometry=definition@format=nil@other_args={}else@geometry,@format=[definition,nil].flatten[0..1]@other_args={}end@format=default_formatif@format.blank?end# retrieves from the attachment the processors defined in the has_attached_file call# (which method (in the attachment) will call any supplied procs)# There is an important change of interface here: a style rule can set its own processors# by default we behave as before, though.# if a proc has been supplied, we call it heredefprocessors@processors.respond_to?(:call)?@processors.call(attachment.instance):(@processors||attachment.processors)end# retrieves from the attachment the whiny settingdefwhinyattachment.whinyend# returns true if we're inclined to grumbledefwhiny?!!whinyenddefconvert_options@convert_options.respond_to?(:call)?@convert_options.call(attachment.instance):(@convert_options||attachment.send(:extra_options_for,name))enddefsource_file_options@source_file_options.respond_to?(:call)?@source_file_options.call(attachment.instance):(@source_file_options||attachment.send(:extra_source_file_options_for,name))end# returns the geometry string for this style# if a proc has been supplied, we call it heredefgeometry@geometry.respond_to?(:call)?@geometry.call(attachment.instance):@geometryend# Supplies the hash of options that processors expect to receive as their second argument# Arguments other than the standard geometry, format etc are just passed through from# initialization and any procs are called here, just before post-processing.defprocessor_optionsargs={:style=>name}@other_args.eachdo|k,v|args[k]=v.respond_to?(:call)?v.call(attachment):vend[:processors,:geometry,:format,:whiny,:convert_options,:source_file_options].eachdo|k|(arg=send(k))&&args[k]=argendargsend# Supports getting and setting style properties with hash notation to ensure backwards-compatibility# eg. @attachment.styles[:large][:geometry]@ will still workdef[](key)if[:name,:convert_options,:whiny,:processors,:geometry,:format,:animated,:source_file_options].include?(key)send(key)elsifdefined?@other_args[key]@other_args[key]endenddef[]=(key,value)if[:name,:convert_options,:whiny,:processors,:geometry,:format,:animated,:source_file_options].include?(key)send("#{key}=".intern,value)else@other_args[key]=valueendend# defaults to default format (nil by default)defdefault_formatbase=attachment.options[:default_format]base.respond_to?(:call)?base.call(attachment,name):baseendendend