Created
November 13, 2012 17:02
-
-
Save janko/4067009 to your computer and use it in GitHub Desktop.
Eliminate need for class eval
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
# lib/carrierwave/orm/activerecord.rb from jnicklas/carrierwave | |
# before | |
class_eval <<-RUBY, __FILE__, __LINE__+1 | |
def #{column}=(new_file) | |
column = _mounter(:#{column}).serialization_column | |
send(:"\#{column}_will_change!") | |
super | |
end | |
def remote_#{column}_url=(url) | |
column = _mounter(:#{column}).serialization_column | |
send(:"\#{column}_will_change!") | |
super | |
end | |
def remove_#{column}! | |
super | |
_mounter(:#{column}).remove = true | |
_mounter(:#{column}).write_identifier | |
end | |
def serializable_hash(options=nil) | |
hash = {} | |
except = options && options[:except] && Array.wrap(options[:except]).map(&:to_s) | |
only = options && options[:only] && Array.wrap(options[:only]).map(&:to_s) | |
self.class.uploaders.each do |column, uploader| | |
if (!only && !except) || (only && only.include?(column.to_s)) || (except && !except.include?(column.to_s)) | |
hash[column.to_s] = _mounter(column).uploader.serializable_hash | |
end | |
end | |
super(options).merge(hash) | |
end | |
RUBY | |
# after | |
define_method("#{column}=") do |new_file| | |
serialization_column = _mounter(column.to_sym).serialization_column | |
send(:"#{serialization_column}_will_change!") | |
super | |
end | |
define_method("remote_#{column}_url=") do |url| | |
serialization_column = _mounter(column.to_sym).serialization_column | |
send(:"#{serialization_column}_will_change!") | |
super | |
end | |
define_method("remove_#{column}!") do | |
super | |
_mounter(column.to_sym).remove = true | |
_mounter(column.to_sym).write_identifier | |
end | |
def serializable_hash(options = nil) | |
hash = {} | |
except = options && options[:except] && Array.wrap(options[:except]).map(&:to_s) | |
only = options && options[:only] && Array.wrap(options[:only]).map(&:to_s) | |
self.class.uploaders.each do |column, uploader| | |
if (!only && !except) || (only && only.include?(column.to_s)) || (except && !except.include?(column.to_s)) | |
hash[column.to_s] = _mounter(column).uploader.serializable_hash | |
end | |
end | |
super(options).merge(hash) | |
end |
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
# lib/faraday/connection.rb from technoweenie/faraday | |
# before | |
%w[get head delete].each do |method| | |
class_eval <<-RUBY, __FILE__, __LINE__ + 1 | |
def #{method}(url = nil, params = nil, headers = nil) | |
run_request(:#{method}, url, nil, headers) { |request| | |
request.params.update(params) if params | |
yield request if block_given? | |
} | |
end | |
RUBY | |
end | |
# after | |
[:get, :head, :delete].each do |method| | |
define_method(method) do |url = nil, params = nil, headers = nil| | |
run_request(method, url, nil, headers) { |request| | |
request.params.update(params) if params | |
yield request if block_given? | |
} | |
end | |
end |
Also, by "ugly" I obviously meant class_eval
, not define_method
:P
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you, I really didn't know about most of those things. Now it's much clearer why do many people use it.
But I don't think this benchmark is significant, because then there would be a downsize in refactoring a big method into many smaller ones.