~michaelforrest/use-case-mapper/trunk

« back to all changes in this revision

Viewing changes to vendor/rails/activesupport/lib/active_support/core_ext/class/delegating_attributes.rb

  • Committer: Michael Forrest
  • Date: 2010-10-15 16:28:50 UTC
  • Revision ID: michael.forrest@canonical.com-20101015162850-tj2vchanv0kr0dun
refrozeĀ gems

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# These class attributes behave something like the class
 
2
# inheritable accessors.  But instead of copying the hash over at
 
3
# the time the subclass is first defined,  the accessors simply
 
4
# delegate to their superclass unless they have been given a 
 
5
# specific value.  This stops the strange situation where values 
 
6
# set after class definition don't get applied to subclasses.
 
7
class Class
 
8
  def superclass_delegating_reader(*names)
 
9
    class_name_to_stop_searching_on = self.superclass.name.blank? ? "Object" : self.superclass.name
 
10
    names.each do |name|
 
11
      class_eval <<-EOS
 
12
      def self.#{name}                                            # def self.only_reader
 
13
        if defined?(@#{name})                                     #   if defined?(@only_reader)
 
14
          @#{name}                                                #     @only_reader
 
15
        elsif superclass < #{class_name_to_stop_searching_on} &&  #   elsif superclass < Object &&
 
16
              superclass.respond_to?(:#{name})                    #         superclass.respond_to?(:only_reader)
 
17
          superclass.#{name}                                      #     superclass.only_reader
 
18
        end                                                       #   end
 
19
      end                                                         # end
 
20
      def #{name}                                                 # def only_reader
 
21
        self.class.#{name}                                        #   self.class.only_reader
 
22
      end                                                         # end
 
23
      def self.#{name}?                                           # def self.only_reader?
 
24
        !!#{name}                                                 #   !!only_reader
 
25
      end                                                         # end
 
26
      def #{name}?                                                # def only_reader?
 
27
        !!#{name}                                                 #   !!only_reader
 
28
      end                                                         # end
 
29
      EOS
 
30
    end
 
31
  end
 
32
 
 
33
  def superclass_delegating_writer(*names)
 
34
    names.each do |name|
 
35
      class_eval <<-EOS
 
36
        def self.#{name}=(value)  # def self.only_writer=(value)
 
37
          @#{name} = value        #   @only_writer = value
 
38
        end                       # end
 
39
      EOS
 
40
    end
 
41
  end
 
42
 
 
43
  def superclass_delegating_accessor(*names)
 
44
    superclass_delegating_reader(*names)
 
45
    superclass_delegating_writer(*names)
 
46
  end
 
47
end