Define variable dependent classes without evalling
I think we as Ruby programmers are blessed with the power of meta programming as we can program classes and instances dynamically within the Ruby language. These are just a couple of methods we can use as tools:
- eval / module_eval / class_eval / instance_eval
- class_variables / instance_variables
- class_variable_get / class_variable_set
- instance_variable_get / instance_variable_set
- define_method / undef_method / remove_method
- send
- method_missing
- Class.new
Some of the methods (such as eval
, class_eval
and instance_eval
) are often seen as evil and also, they can be very dangerous.
In my case, I had to define CarrierWave uploader classes dynamically as they are dependent of certain variables. Choosing not to use instance_eval
for this task, I have created the NewClass
gem to accomplish this.
Instead of defining a CarrierWave uploader class with something like this:
config = { :processor => CarrierWave::RMagick, :storage => :file, :versions => [[800, 800], {:thumb => [200, 200]}, {:icon => [100, 100]}], :extension_white_list => %w(jpg jpeg gif png) } avatar_uploader = Class.new(CarrierWave::Uploader::Base).tap do |klass| klass.instance_eval <<-RUBY include #{config[:processor].to_s} storage :#{config[:storage]} def store_dir "uploads/\#{model.class.to_s.underscore}/\#{mounted_as}/\#{model.id}" end def cache_dir "tmp" end def extension_white_list %w(#{config[:extension_white_list].join " "}) end RUBY config[:versions].each do |v| case v.class.name when "Array" klass.process :resize_to_fit => v when "Hash" klass.instance_eval <<-RUBY version :#{v.keys.first} do process :resize_to_fit => [#{v.values.first[0]}, #{v.values.first[1]}] end RUBY end end end
I am able to define the class like this:
config = { :processor => CarrierWave::RMagick, :storage => :file, :versions => [[800, 800], {:thumb => [200, 200]}, {:icon => [100, 100]}], :extension_white_list => %w(jpg jpeg gif png) } class Uploader < CarrierWave::Uploader::Base include NewClass def self.defined include config[:processor] storage config[:storage] config[:versions].each do |v| case v.class.name when "Array" process :resize_to_fit => v when "Hash" version v.keys.first do process :resize_to_fit => v.values.first end end end end def store_dir "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}" end def cache_dir "tmp" end def extension_white_list config[:extension_white_list] end end avatar_uploader = Uploader.new_class(:config => config)
A much cleaner solution right? Try out NewClass
right now and spread the word if you like it! ^^
Add NewClass in Gemfile
as a gem dependency:
gem "new_class"
Run the following in your console to install with Bundler:
bundle install
Please check out test/unit/test_carrierwave.rb and test/unit/test_class.rb for most of the tests available. You can run the unit tests with rake
within the terminal.
Also, the NewClass repo is provided with script/console
which you can use for testing purposes.
For support, remarks and requests please mail me at paul.engel@holder.nl.
Copyright © 2012 Paul Engel, released under the MIT license
http://holder.nl – http://codehero.es – http://gettopup.com – http://github.com/archan937 – http://twitter.com/archan937 – paul.engel@holder.nl
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.