Skip to content

Instantly share code, notes, and snippets.

@ismasan
Created November 25, 2008 11:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save ismasan/28885 to your computer and use it in GitHub Desktop.
Save ismasan/28885 to your computer and use it in GitHub Desktop.
module BlahPlugin
VERSION = '1.0.0'
class << self
# add plugin to AR only if it hasn't been included before,
# which may cause stack-level-too-deep errors if you're aliasing methods
#
def enable_activerecord
return if ActiveRecord::Base.respond_to? :acts_as_blah
ActiveRecord::Base.extend BlahPlugin::Macro
end
end
module Macro
# his class level method injects the rest of the plugin behaviour in the affected class.
# use like this:
# class Post < ActiveRecord::Base
# acts_as_blah
# end
#
def acts_as_blah(opts = {:some_option => true}
# don't include modules twice
return if self.included_modules.include?(BlahPlugin::InstanceMethods)
# include class and instanc methods
extend BlahPlugin::ClassMethods
include BlahPlugin::InstanceMethods
# do something else, like initializing options
end
end
module ClassMethods
# class methods injected by the plugin, alias_method chaining, macro calls (has_many, after_save, validates),etc.
end
module InstanceMethods
# instance methods injected by the plugin. callbacks, etc.
end
end
require 'rubygems'
require 'active_record'
# enable the thing!
BlahPlugin.enable_activerecord
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment