Skip to content

Instantly share code, notes, and snippets.

@gzigzigzeo
Created December 15, 2010 20:13
Show Gist options
  • Save gzigzigzeo/742538 to your computer and use it in GitHub Desktop.
Save gzigzigzeo/742538 to your computer and use it in GitHub Desktop.
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