Skip to content

Instantly share code, notes, and snippets.

@jhawthorn
Created March 2, 2012 23:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jhawthorn/1962162 to your computer and use it in GitHub Desktop.
Save jhawthorn/1962162 to your computer and use it in GitHub Desktop.
paperclip interpolator
class PaperclipInterpolator
def initialize pattern
@pattern = pattern
@methods = []
@template = Paperclip::Interpolations::all.reverse.inject( pattern.dup ) do |result, tag|
result.gsub(/:#{tag}/) do |match|
@methods << tag.to_sym
"%{#{tag}}"
end
end
@methods.uniq!
end
def call attachment, style_name
values = @methods.map{|method| [method, Paperclip::Interpolations.send(method, attachment, style_name)] }
@template % Hash[values]
end
def interpolate pattern, attachment, style_name
raise "Wrong interpolation pattern: expected '#{@pattern}' got '#{pattern}'" unless pattern == @pattern
call(attachment, style_name)
end
def self.interpolate pattern, attachment, style_name
@interpolators ||= {}
@interpolators[pattern] ||= new(pattern)
@interpolators[pattern].call(attachment, style_name)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment