Class: Class
Overview
Defines class variable
Instance Method Summary collapse
Instance Method Details
#class_attribute(name, default = nil, &block) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/clean-annotations/class_attribute.rb', line 4 def class_attribute name, default=nil, &block raise ArgumentError.new('name must be symbol') unless name.is_a?(Symbol) ivar = "@class_attribute_#{name}" instance_variable_set ivar, block || default define_singleton_method('%s=' % name) { |arg| send(name, arg) } define_singleton_method(name) do |arg=:_undefined, &block| # define and set if argument given if block || arg != :_undefined return instance_variable_set ivar, block || arg end # find value and return ancestors.each do |klass| if klass.instance_variable_defined?(ivar) value = klass.instance_variable_get ivar return value.is_a?(Proc) ? instance_exec(&value) : value end end end end |