Class: Class

Inherits:
Object
  • Object
show all
Defined in:
lib/famili/class_attribute.rb

Instance Method Summary collapse

Instance Method Details

#class_attribute(*attrs) ⇒ Object



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
# File 'lib/famili/class_attribute.rb', line 10

def class_attribute(*attrs)
  attrs.each do |name|
    class_eval <<-RUBY, __FILE__, __LINE__ + 1
      def self.#{name}() nil end
      def self.#{name}?() !!#{name} end

      def self.#{name}=(val)
        singleton_class.class_eval do
          remove_possible_method(:#{name})
          define_method(:#{name}) { val }
        end

        if singleton_class?
          class_eval do
            remove_possible_method(:#{name})
            def #{name}
              defined?(@#{name}) ? @#{name} : singleton_class.#{name}
            end
          end
        end
        val
      end
    RUBY
  end
end