Skip to content

Instantly share code, notes, and snippets.

@cyberfox
Created February 10, 2011 09:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save cyberfox/820211 to your computer and use it in GitHub Desktop.
Save cyberfox/820211 to your computer and use it in GitHub Desktop.
# Based off of Rails, by way of http://www.raulparolari.com/Rails/class_inheritable
class Class
def class_inheritable_reader(*syms)
syms.each do |sym|
class_eval <<-EOS
def self.#{sym}
read_inheritable_attr(:#{sym})
end
EOS
end
end
def class_inheritable_writer(*syms)
syms.each do |sym|
class_eval <<-EOS
def self.#{sym}=(obj)
write_inheritable_attr(:#{sym},obj)
end
EOS
end
end
def class_inheritable_accessor(*syms)
class_inheritable_reader(*syms)
class_inheritable_writer(*syms)
end
# accessor for hash
def inheritable_attrs
@inheritable_attrs ||= {}
end
# write variable into hash
def write_inheritable_attr(key, value)
inheritable_attrs[key] = value
end
# read variable from hash
def read_inheritable_attr(key)
inheritable_attrs[key]
end
private
def inherited_with_inheritable_attrs(child)
inherited_without_inheritable_attrs(child) if respond_to?(:inherited_without_inheritable_attrs)
if inheritable_attrs.nil?
new_inheritable_attrs = {}
else
new_inheritable_attrs = inheritable_attrs.inject({}) do |memo,(key, value)|
memo.update(key => (value.dup rescue value))
end
end
child.instance_variable_set('@inheritable_attrs', new_inheritable_attrs)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment