Created
December 15, 2010 20:13
-
-
Save gzigzigzeo/742538 to your computer and use it in GitHub Desktop.
This file contains 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
module CarrierWave::Conditional | |
extend ActiveSupport::Concern | |
module ClassMethods | |
def process_if(*args) | |
original_args = args.dup | |
options = args.extract_options! | |
c_true = options.delete(:if) | |
c_false = options.delete(:if) | |
raise "No condition specified for process_if specified" if c_true.nil? && c_false.nil? | |
original_args.each do |a| | |
m_name = a.is_a?(Hash) ? a.keys.first : a | |
m_args = a.is_a?(Hash) ? a.values.first : nil | |
if_m_name = :"#{m_name}_if_#{SimpleUUID::UUID.new.to_i}" | |
unless m_name == :if | |
define_method if_m_name do |*args| | |
if !c_true.nil? && instance_exec(&c_true) | |
send(m_name, *args) | |
elsif !c_false.nil? && !instance_exec(&c_false) | |
send(m_name, *args) | |
end | |
end | |
process if_m_name => m_args | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment