NewClass

Define variable dependent classes without evalling

Introduction

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.

Dynamic CarrierWave uploader classes

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! ^^

Installation

Using Bundler

Add NewClass in Gemfile as a gem dependency:


  gem "new_class"

Run the following in your console to install with Bundler:


  bundle install

Last remarks

Please check out test/carrierwave_test.rb and test/class_test.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.

Note: NewClass is successfully tested using Ruby 1.8.7 and Ruby 1.9.2

Contact me

For support, remarks and requests please mail me at [email protected].

License

Copyright © 2011 Paul Engel, released under the MIT license

http://holder.nlhttp://codehero.eshttp://gettopup.comhttp://github.com/archan937http://twitter.com/archan937[email protected]

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.