aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/core_ext/class/delegating_attributes.rb
blob: 72e0eefb0abcf83499e2f1dbe10d6b1220bfc6e8 (plain) (blame)
1
2
3
4
5
6
7
8
9
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
require 'active_support/core_ext/object/blank'
require 'active_support/core_ext/array/extract_options'

class Class
  def superclass_delegating_reader(*names)
    class_to_stop_searching_on = superclass.name.blank? ? "Object" : superclass.name
    options = names.extract_options!

    names.each do |name|
      # def self.only_reader
      #   if defined?(@only_reader)
      #     @only_reader
      #   elsif superclass < Object && superclass.respond_to?(:only_reader)
      #     superclass.only_reader
      #   end
      # end
      class_eval <<-EOS, __FILE__, __LINE__ + 1
        def self.#{name}
          if defined?(@#{name})
            @#{name}
          elsif superclass < #{class_to_stop_searching_on} && superclass.respond_to?(:#{name})
            superclass.#{name}
          end
        end
      EOS

      unless options[:instance_reader] == false
        class_eval <<-EOS, __FILE__, __LINE__ + 1
          def #{name}                                  # def only_reader
            self.class.#{name}                         #   self.class.only_reader
          end                                          # end
          def self.#{name}?                            # def self.only_reader?
            !!#{name}                                  #   !!only_reader
          end                                          # end
          def #{name}?                                 # def only_reader?
            !!#{name}                                  #   !!only_reader
          end                                          # end
        EOS
      end
    end
  end

  def superclass_delegating_writer(*names, &block)
    options = names.extract_options!

    names.each do |name|
      class_eval <<-EOS, __FILE__, __LINE__ + 1
        def self.#{name}=(value)     # def self.property=(value)
          @#{name} = value           #   @property = value
        end                          # end
      EOS

      self.send(:"#{name}=", yield) if block_given?
    end
  end

  # These class attributes behave something like the class
  # inheritable accessors.  But instead of copying the hash over at
  # the time the subclass is first defined, the accessors simply
  # delegate to their superclass unless they have been given a 
  # specific value.  This stops the strange situation where values 
  # set after class definition don't get applied to subclasses.
  def superclass_delegating_accessor(*names, &block)
    superclass_delegating_reader(*names)
    superclass_delegating_writer(*names, &block)
  end
end