Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save floere/338301 to your computer and use it in GitHub Desktop.
Save floere/338301 to your computer and use it in GitHub Desktop.
# So Err the Blog really likes returning
# http://errtheblog.com/posts/27-quickly-returning
# (Ok, it's from 2006, but still ;) )
#
# I object to the first few examples (ask me why),
# but let's look at the code he shows:
#
def self.view_class
@view_class ||=
# create a new class based on the default template class
# and include helper methods
returning Class.new(ActionView::Base) do |view_class|
view_class.send(:include, master_helper_module)
end
end
# The tune seems to be that this is incredibly cool.
# To me it sounds like: We had a problem and built this
# funky contraption to deal with the problem! Gosh!
#
# How about something we all know and can use, and is
# elegant and testable too?
#
def self.view_class
@view_class ||= generate_view_class
end
def self.generate_view_class
new_view_subclass.send :include, master_helper_module
end
def self.new_view_subclass
Class.new ActionView::Base
end
# 1. Every method says exactly what it does.
# 2. The original method could not be much clearer:
# We cache the view class we generate the first time.
# 3. This is nicely testable.
#
# Note: Not using parentheses helps me see whether
# a method could potentially be too complex.
#
# Also note: That include returns the Class instance it was called on.
#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment