If one was inclined to use the acts_as_yaffle pattern, they would probably use the second one, rather than the heavily cargo-culted first one.
-
-
Save andrew/435678 to your computer and use it in GitHub Desktop.
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
module Yaffle | |
def self.included(base) | |
base.send :extend, ClassMethods | |
end | |
module ClassMethods | |
def acts_as_yaffle(options = {}) | |
cattr_accessor :yaffle_text_field | |
self.yaffle_text_field = (options[:yaffle_text_field] || :last_squawk).to_s | |
send :include, InstanceMethods | |
end | |
end | |
module InstanceMethods | |
def squawk(string) | |
write_attribute(self.class.yaffle_text_field, string.to_squawk) | |
end | |
end | |
end | |
ActiveRecord::Base.send :include, Yaffle |
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
module Yaffle | |
def acts_as_yaffle(options = {}) | |
cattr_accessor :yaffle_text_field | |
self.yaffle_text_field = (options[:yaffle_text_field] || :last_squawk).to_s | |
include InstanceMethods | |
end | |
module InstanceMethods | |
def squawk(string) | |
write_attribute(self.class.yaffle_text_field, string.to_squawk) | |
end | |
end | |
end | |
ActiveRecord::Base.extend(Yaffle) |
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
module Yaffle | |
# since this is for internal consumption only, just extend directly; | |
# don't rewrite the include method to mean extend | |
def self.included(base) | |
# extend is a public method | |
base.send :extend, ClassMethods | |
end | |
# if we extended directly, we could get rid of the ClassMethods | |
# module and move the methods into the Yaffle module. You onluy | |
# ever need one of ClassMethods or InstanceMethods | |
module ClassMethods | |
def acts_as_yaffle(options = {}) | |
cattr_accessor :yaffle_text_field | |
self.yaffle_text_field = (options[:yaffle_text_field] || :last_squawk).to_s | |
# we're inside the class, so we don't need to use send to | |
# use a private method | |
send :include, InstanceMethods | |
end | |
end | |
module InstanceMethods | |
def squawk(string) | |
write_attribute(self.class.yaffle_text_field, string.to_squawk) | |
end | |
end | |
end | |
# If we'd used extend, we wouldn't need a send | |
ActiveRecord::Base.send :include, Yaffle |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment