Created
March 2, 2012 23:05
-
-
Save jhawthorn/1962162 to your computer and use it in GitHub Desktop.
paperclip interpolator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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