Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/archan937/new_class
Define variable dependent classes without evalling
https://github.com/archan937/new_class
Last synced: about 2 months ago
JSON representation
Define variable dependent classes without evalling
- Host: GitHub
- URL: https://github.com/archan937/new_class
- Owner: archan937
- License: mit
- Created: 2011-11-19T16:07:00.000Z (about 13 years ago)
- Default Branch: master
- Last Pushed: 2012-09-16T10:35:04.000Z (over 12 years ago)
- Last Synced: 2024-10-06T04:38:46.815Z (3 months ago)
- Language: Ruby
- Homepage: https://github.com/archan937/new_class
- Size: 180 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.textile
- Changelog: CHANGELOG.rdoc
- License: MIT-LICENSE
Awesome Lists containing this project
README
h1. NewClass "!https://secure.travis-ci.org/archan937/new_class.png!":http://travis-ci.org/archan937/new_class
Define variable dependent classes without evalling
h2. 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.newSome of the methods (such as @eval@, @class_eval@ and @instance_eval@) are often seen as evil and also, they can be very dangerous.
h3. Dynamic CarrierWave uploader classes
In my case, I had to define "CarrierWave":https://github.com/jnicklas/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}"
enddef cache_dir
"tmp"
enddef extension_white_list
%w(#{config[:extension_white_list].join " "})
end
RUBYconfig[: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
endI 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 NewClassdef 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
enddef store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
enddef cache_dir
"tmp"
enddef extension_white_list
config[:extension_white_list]
end
endavatar_uploader = Uploader.new_class(:config => config)
A much cleaner solution right? Try out @NewClass@ right now and spread the word if you like it! ^^
h2. Installation
h3. Using Bundler
Add NewClass in @Gemfile@ as a gem dependency:
gem "new_class"Run the following in your console to install with Bundler:
bundle installh2. Last remarks
Please check out "test/unit/test_carrierwave.rb":https://github.com/archan937/new_class/blob/master/test/unit/test_carrierwave.rb and "test/unit/test_class.rb":https://github.com/archan937/new_class/blob/master/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.
h2. Contact me
For support, remarks and requests please mail me at "[email protected]":mailto:[email protected].
h2. License
Copyright (c) 2012 Paul Engel, released under the MIT license
"http://holder.nl":http://holder.nl – "http://codehero.es":http://codehero.es – "http://gettopup.com":http://gettopup.com – "http://github.com/archan937":http://github.com/archan937 – "http://twitter.com/archan937":http://twitter.com/archan937 – "[email protected]":mailto:[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.