Module: ClassX::ClassAttributes
- Defined in:
- lib/classx/class_attributes.rb
Overview
for easy defining inheritable class accessor using classx attribute interface.
require 'classx'
class YourClass
extend ClassX::ClassAttributes
class_has :table_name, :writable => true, :default => proc {|klass| klass.to_s.downcase }
end
YourClass.table_name #=> 'yourclass'
YourClass.table_name = 'test'
YourClass.table_name #=> 'test'
class SubClass < YourClass
end
SubClass.table_name #=> 'subclass' # it's not "test"!!
# you can also write following:
SubClass.table_name('test2')
SubClass.table_name #=> 'test2'
SEE ALSO
dsl_accessor:
It's have also similar functions. It's implemented simply using activesupport's inheritable_accessor.
classx is complex but more extensible to define class attribute.
Constant Summary collapse
- CLASS_ATTRIBUTE_REGEX =
/\Aclass_attribute_of:(\w+)\z/
Instance Method Summary collapse
Instance Method Details
#class_attribute_of ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/classx/class_attributes.rb', line 36 def class_attribute_of unless instance_variable_defined?('@__class_attribute_of') && @__class_attribute_of @__class_attribute_of = {} private_methods.select {|meth| meth.to_s =~ CLASS_ATTRIBUTE_REGEX }.each do |meth| key = meth.to_s.sub(CLASS_ATTRIBUTE_REGEX) { $1 } @__class_attribute_of[key] = __send__("class_attribute_of:#{key}").new(self) end end @__class_attribute_of end |