Skip to content

Commit

Permalink
Added parenthesis around function calls
Browse files Browse the repository at this point in the history
Removed unneeded functions
  • Loading branch information
michaeldauria committed Dec 24, 2008
1 parent a231498 commit d072240
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 63 deletions.
8 changes: 4 additions & 4 deletions lib/dm-paperclip.rb
Expand Up @@ -54,7 +54,7 @@ def options
}
end

def path_for_command command #:nodoc:
def path_for_command(command) #:nodoc:
path = [options[:image_magick_path], command].compact
File.join(*path)
end
Expand Down Expand Up @@ -139,7 +139,7 @@ module ClassMethods
# choices are :filesystem and :s3. The default is :filesystem. Make sure you read the
# documentation for Paperclip::Storage::Filesystem and Paperclip::Storage::S3
# for backend-specific options.
def has_attached_file name, options = {}
def has_attached_file(name, options = {})
include InstanceMethods

self.attachment_definitions = {} if self.attachment_definitions.nil?
Expand Down Expand Up @@ -187,7 +187,7 @@ def validates_attachment_size(*fields)
end

# Adds errors if thumbnail creation fails. The same as specifying :whiny_thumbnails => true.
def validates_attachment_thumbnails name, options = {}
def validates_attachment_thumbnails(name, options = {})
self.attachment_definitions[name][:whiny_thumbnails] = true
end

Expand All @@ -211,7 +211,7 @@ def validates_attachment_content_type(*fields)
end

module InstanceMethods #:nodoc:
def attachment_for name
def attachment_for(name)
@attachments ||= {}
@attachments[name] ||= Attachment.new(name, self, self.class.attachment_definitions[name])
end
Expand Down
15 changes: 7 additions & 8 deletions lib/dm-paperclip/attachment.rb
@@ -1,5 +1,4 @@
# TODO: user RDoc format for comments
# wrap function call with parenthesis
# create methods to manage interpolations
# allow the use of object instance methods or properties as interpolations
# add better mime type checking on *nix systems, absorb mimetype-fu into upfile
Expand Down Expand Up @@ -30,7 +29,7 @@ def self.default_options
# Creates an Attachment object. +name+ is the name of the attachment, +instance+ is the
# ActiveRecord object instance it's attached to, and +options+ is the same as the hash
# passed to +has_attached_file+.
def initialize name, instance, options = {}
def initialize(name, instance, options = {})
@name = name
@instance = instance

Expand Down Expand Up @@ -63,7 +62,7 @@ def initialize name, instance, options = {}
# the previous file for deletion, to be flushed away on #save of its host.
# In addition to form uploads, you can also assign another Paperclip attachment:
# new_user.avatar = old_user.avatar
def assign uploaded_file
def assign(uploaded_file)
if uploaded_file.is_a?(Paperclip::Attachment)
uploaded_file = uploaded_file.to_file(:original)
end
Expand Down Expand Up @@ -116,7 +115,7 @@ def assign uploaded_file
# and can point to an action in your app, if you need fine grained security.
# This is not recommended if you don't need the security, however, for
# performance reasons.
def url style = default_style
def url(style = default_style)
url = original_filename.nil? ? interpolate(@default_url, style) : interpolate(@url, style)
updated_at ? [url, updated_at].compact.join(url.include?("?") ? "&" : "?") : url
end
Expand All @@ -125,12 +124,12 @@ def url style = default_style
# file is stored in the filesystem the path refers to the path of the file 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 = nil #:nodoc:
def path(style = nil) #:nodoc:
interpolate(@path, style)
end

# Alias to +url+
def to_s style = nil
def to_s(style = nil)
url(style)
end

Expand Down Expand Up @@ -247,7 +246,7 @@ def logger
instance.logger
end

def valid_assignment? file #:nodoc:
def valid_assignment?(file) #:nodoc:
return true if file.nil?
if(file.is_a?(File) || file.is_a?(Tempfile))
(file.respond_to?(:original_filename) && file.respond_to?(:content_type))
Expand Down Expand Up @@ -301,7 +300,7 @@ def post_process #:nodoc:
end
end

def interpolate pattern, style = default_style #:nodoc:
def interpolate(pattern, style = default_style) #:nodoc:
interpolations = self.class.interpolations.sort{|a,b| a.first.to_s <=> b.first.to_s }
interpolations.reverse.inject( pattern.dup ) do |result, interpolation|
tag, blk = interpolation
Expand Down
12 changes: 6 additions & 6 deletions lib/dm-paperclip/geometry.rb
Expand Up @@ -5,7 +5,7 @@ class Geometry
attr_accessor :height, :width, :modifier

# Gives a Geometry representing the given height and width
def initialize width = nil, height = nil, modifier = nil
def initialize(width = nil, height = nil, modifier = nil)
height = nil if height == ""
width = nil if width == ""
@height = (height || width).to_f
Expand All @@ -15,14 +15,14 @@ def initialize width = nil, height = nil, modifier = nil

# Uses ImageMagick to determing the dimensions of a file, passed in as either a
# File or path.
def self.from_file file
def self.from_file(file)
file = file.path if file.respond_to? "path"
parse(`#{Paperclip.path_for_command('identify')} "#{file}"`) ||
raise(NotIdentifiedByImageMagickError.new("#{file} is not recognized by the 'identify' command."))
end

# Parses a "WxH" formatted string, where W is the width and H is the height.
def self.parse string
def self.parse(string)
if match = (string && string.match(/\b(\d*)x(\d*)\b([\>\<\#\@\%^!])?/))
Geometry.new(*match[1,3])
end
Expand Down Expand Up @@ -75,7 +75,7 @@ def inspect
# destination Geometry would be completely filled by the source image, and any
# overhanging image would be cropped. Useful for square thumbnail images. The cropping
# is weighted at the center of the Geometry.
def transformation_to dst, crop = false
def transformation_to(dst, crop = false)

if crop
ratio = Geometry.new( dst.width / self.width, dst.height / self.height )
Expand All @@ -90,15 +90,15 @@ def transformation_to dst, crop = false

private

def scaling dst, ratio
def scaling(dst, ratio)
if ratio.horizontal? || ratio.square?
[ "%dx" % dst.width, ratio.width ]
else
[ "x%d" % dst.height, ratio.height ]
end
end

def cropping dst, ratio, scale
def cropping(dst, ratio, scale)
if ratio.horizontal? || ratio.square?
"%dx%d+%d+%d" % [ dst.width, dst.height, 0, (self.height * scale - dst.height) / 2 ]
else
Expand Down
2 changes: 1 addition & 1 deletion lib/dm-paperclip/iostream.rb
Expand Up @@ -14,7 +14,7 @@ def to_tempfile
# StringIO and Tempfile, then either can have its data copied anywhere else without typing
# worries or memory overhead worries. Returns a File if a String is passed in as the destination
# and returns the IO or Tempfile as passed in if one is sent as the destination.
def stream_to path_or_file, in_blocks_of = 8192
def stream_to(path_or_file, in_blocks_of = 8192)
dstio = case path_or_file
when String then File.new(path_or_file, "wb+")
when IO then path_or_file
Expand Down
68 changes: 30 additions & 38 deletions lib/dm-paperclip/storage.rb
Expand Up @@ -10,18 +10,18 @@ module Storage
# * +path+: The location of the repository of attachments on disk. This can (and, in
# almost all cases, should) be coordinated with the value of the +url+ option to
# allow files to be saved into a place where Apache can serve them without
# hitting your app. Defaults to
# hitting your app. Defaults to
# ":rails_root/public/:attachment/:id/:style/:basename.:extension"
# By default this places the files in the app's public directory which can be served
# directly. If you are using capistrano for deployment, a good idea would be to
# make a symlink to the capistrano-created system directory from inside your app's
# By default this places the files in the app's public directory which can be served
# directly. If you are using capistrano for deployment, a good idea would be to
# make a symlink to the capistrano-created system directory from inside your app's
# public directory.
# See Paperclip::Attachment#interpolate for more information on variable interpolaton.
# :path => "/var/app/attachments/:class/:id/:style/:filename"
module Filesystem
def self.extended base
def self.extended(base)
end

def exists?(style = default_style)
if original_filename
File.exist?(path(style))
Expand All @@ -32,7 +32,7 @@ def exists?(style = default_style)

# Returns representation of the data of the file assigned to the given
# style, in the format most representative of the current storage.
def to_file style = default_style
def to_file(style = default_style)
@queued_for_write[style] || (File.new(path(style)) if exists?(style))
end
alias_method :to_io, :to_file
Expand Down Expand Up @@ -72,25 +72,25 @@ def flush_deletes #:nodoc:
# database.yml file, so different environments can use different accounts:
# development:
# access_key_id: 123...
# secret_access_key: 123...
# secret_access_key: 123...
# test:
# access_key_id: abc...
# secret_access_key: abc...
# secret_access_key: abc...
# production:
# access_key_id: 456...
# secret_access_key: 456...
# secret_access_key: 456...
# This is not required, however, and the file may simply look like this:
# access_key_id: 456...
# secret_access_key: 456...
# secret_access_key: 456...
# In which case, those access keys will be used in all environments. You can also
# put your bucket name in this file, instead of adding it to the code directly.
# This is useful when you want the same account but a different bucket for
# This is useful when you want the same account but a different bucket for
# development versus production.
# * +s3_permissions+: This is a String that should be one of the "canned" access
# policies that S3 provides (more information can be found here:
# http://docs.amazonwebservices.com/AmazonS3/2006-03-01/RESTAccessPolicy.html#RESTCannedAccessPolicies)
# The default for Paperclip is "public-read".
# * +s3_protocol+: The protocol for the URLs generated to your S3 assets. Can be either
# * +s3_protocol+: The protocol for the URLs generated to your S3 assets. Can be either
# 'http' or 'https'. Defaults to 'http' when your :s3_permissions are 'public-read' (the
# default), and 'https' when your :s3_permissions are anything else.
# * +bucket+: This is the name of the S3 bucket that will store your files. Remember
Expand All @@ -107,7 +107,7 @@ def flush_deletes #:nodoc:
# S3 (strictly speaking) does not support directories, you can still use a / to
# separate parts of your file name.
module S3
def self.extended base
def self.extended(base)
require 'right_aws'
base.instance_eval do
@s3_credentials = parse_credentials(@options[:s3_credentials])
Expand Down Expand Up @@ -140,11 +140,16 @@ def bucket_name
@bucket
end

def parse_credentials creds
creds = stringify_keys(find_credentials(creds))
symbolize_keys((creds[Merb.env] || creds))
def parse_credentials(creds)
creds = find_credentials(creds)

unless creds[Merb.env].nil?
creds[Merb.env]
else
creds
end
end

def exists?(style = default_style)
s3_bucket.key(path(style)) ? true : false
end
Expand All @@ -155,7 +160,7 @@ def s3_protocol

# Returns representation of the data of the file assigned to the given
# style, in the format most representative of the current storage.
def to_file style = default_style
def to_file(style = default_style)
@queued_for_write[style] || s3_bucket.key(path(style))
end
alias_method :to_io, :to_file
Expand Down Expand Up @@ -189,9 +194,11 @@ def flush_deletes #:nodoc:
end
@queued_for_delete = []
end

def find_credentials creds
case creds

private

def find_credentials(creds)
creds = case creds
when File:
YAML.load_file(creds.path)
when String:
Expand All @@ -201,23 +208,8 @@ def find_credentials creds
else
raise ArgumentError, "Credentials are not a path, file, or hash."
end
end
private :find_credentials

private

def stringify_keys(hash)
hash.inject({}) do |options, (key, value)|
options[key.to_s] = value
options
end
end

def symbolize_keys(hash)
hash.inject({}) do |options, (key, value)|
options[key.to_sym || key] = value
options
end
creds.to_mash
end

end
Expand Down
4 changes: 2 additions & 2 deletions lib/dm-paperclip/thumbnail.rb
Expand Up @@ -12,7 +12,7 @@ class Thumbnail
# unless specified. Thumbnail creation will raise no errors unless
# +whiny_thumbnails+ is true (which it is, by default. If +convert_options+ is
# set, the options will be appended to the convert command upon image conversion
def initialize file, target_geometry, format = nil, convert_options = nil, whiny_thumbnails = true
def initialize(file, target_geometry, format = nil, convert_options = nil, whiny_thumbnails = true)
@file = file
@crop = target_geometry[-1,1] == '#'
@target_geometry = Geometry.parse target_geometry
Expand All @@ -28,7 +28,7 @@ def initialize file, target_geometry, format = nil, convert_options = nil, whiny

# Creates a thumbnail, as specified in +initialize+, +make+s it, and returns the
# resulting Tempfile.
def self.make file, dimensions, format = nil, convert_options = nil, whiny_thumbnails = true
def self.make(file, dimensions, format = nil, convert_options = nil, whiny_thumbnails = true)
new(file, dimensions, format, convert_options, whiny_thumbnails).make
end

Expand Down
8 changes: 4 additions & 4 deletions test/storage_test.rb
Expand Up @@ -19,21 +19,21 @@ class StorageTest < Test::Unit::TestCase

should "get the correct credentials when RAILS_ENV is production" do
ENV['RAILS_ENV'] = 'production'
assert_equal({:key => "12345"},
assert_equal({"key" => "12345"},
@avatar.parse_credentials('production' => {:key => '12345'},
:development => {:key => "54321"}))
end

should "get the correct credentials when RAILS_ENV is development" do
ENV['RAILS_ENV'] = 'development'
assert_equal({:key => "54321"},
assert_equal({"key" => "54321"},
@avatar.parse_credentials('production' => {:key => '12345'},
:development => {:key => "54321"}))
end

should "return the argument if the key does not exist" do
ENV['RAILS_ENV'] = "not really an env"
assert_equal({:test => "12345"}, @avatar.parse_credentials(:test => "12345"))
assert_equal({"test" => "12345"}, @avatar.parse_credentials(:test => "12345"))
end
end

Expand All @@ -47,7 +47,7 @@ class StorageTest < Test::Unit::TestCase
@dummy = Dummy.new
end

should "get the right bucket in production", :before => lambda{ ENV.expects(:[]).returns('production') } do
should "get the right bucket in production" do
assert_equal "prod_bucket", @dummy.avatar.bucket_name
end

Expand Down

0 comments on commit d072240

Please sign in to comment.