Created
September 5, 2012 20:34
-
-
Save mrgenixus/3644294 to your computer and use it in GitHub Desktop.
testing controlled inclusiong
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
class DataStore < Hash | |
def store(key,value) | |
self[key] = value | |
end | |
end | |
module PropertyPlugin | |
def self.included(base) | |
base.extend(ClassMethods) | |
end | |
module ClassMethods | |
def has_property_plugin | |
include InstanceMethods | |
end | |
end | |
module InstanceMethods | |
attr_accessor :dstore | |
def dstore | |
@dstore ||= DataStore.new | |
end | |
def property(key,value) | |
self[key] = value | |
end | |
def []=(key,value) | |
dstore.store(key,value) | |
end | |
end | |
end | |
class A | |
include PropertyPlugin | |
def write_property_greeting (subject) | |
self[:hello] = subject if self.respond_to? :property | |
end | |
end | |
class B < A | |
has_property_plugin | |
end | |
class C < A | |
end | |
[ B , C ].each do | c | | |
puts c | |
m = c.new | |
m.write_property_greeting "world" | |
if m.respond_to? :dstore | |
puts "DataStore:\n---------------------------------------------------\n" | |
m.dstore.each do |key, value| | |
puts "#{key} => #{value}" | |
end | |
puts "---------------------------------------------------\n" | |
else | |
puts " #{m.class} does not have a dStore" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment