public
Created

  • Download Gist
plugin.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
require "rubygems"
require "ostruct"
require "active_support"
 
module PluginSupport
module Helpers
def __setup_cattr_accessor(base, name, initial_value=nil, default=nil)
base.send(:cattr_accessor, name)
base.send("#{name}=", initial_value || default)
end
end
def use(plugin)
cfg = OpenStruct.new
yield(cfg) if block_given?
plugin.setup(self, cfg)
extend plugin::ClassMethods
include plugin::InstanceMethods
end
end
 
module Yaffle
extend PluginSupport::Helpers
def self.setup(base, cfg)
__setup_cattr_accessor base, :yaffle_text_field, cfg.text_field, "foobaz"
end
module ClassMethods
def yaffle?
true
end
end
module InstanceMethods
def yaffle!
puts "I'm acting as yaffle using `#{yaffle_text_field}`!"
end
end
end
 
def Yaffle(cfg={})
cfg = OpenStruct.new(cfg)
Yaffle.setup(self, cfg)
extend Yaffle::ClassMethods
include Yaffle::InstanceMethods
end
 
class Base
extend PluginSupport
use Yaffle do |cfg|
cfg.field_name = "foobar"
end
 
# Yaffle(:field_name => "foobar")
end
 
puts Base.yaffle_text_field
puts Base.yaffle?
Base.new.yaffle!

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.