Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 116 lines (107 sloc) 3.641 kB
89978f1 @fxn moves Object#singleton_class to Kernel#singleton_class to match Ruby …
fxn authored
1 require 'active_support/core_ext/kernel/singleton_class'
a5587ef @wycats Remove some 1.9 warnings (resulting in some fixed bugs). Remaining AM…
wycats authored
2 require 'active_support/core_ext/module/remove_method'
e6f15f0 @josevalim Ugh, the class_attribute pull request broke the build. fix it.
josevalim authored
3 require 'active_support/core_ext/array/extract_options'
8ae25a8 @jeremy Introduce class_attribute to declare inheritable class attributes. Wr…
jeremy authored
4
5 class Class
e7920a3 @neerajdotname clarifying description for class_attribute method
neerajdotname authored
6 # Declare a class-level attribute whose value is inheritable by subclasses.
7 # Subclasses can change their own value and it will not impact parent class.
8ae25a8 @jeremy Introduce class_attribute to declare inheritable class attributes. Wr…
jeremy authored
8 #
9 # class Base
10 # class_attribute :setting
11 # end
12 #
13 # class Subclass < Base
14 # end
15 #
16 # Base.setting = true
17 # Subclass.setting # => true
18 # Subclass.setting = false
19 # Subclass.setting # => false
20 # Base.setting # => true
21 #
7c3d479 @josevalim More documentation to class_attribute.
josevalim authored
22 # In the above case as long as Subclass does not assign a value to setting
23 # by performing <tt>Subclass.setting = _something_ </tt>, <tt>Subclass.setting</tt>
24 # would read value assigned to parent class. Once Subclass assigns a value then
25 # the value assigned by Subclass would be returned.
f3b50b1 @neerajdotname updating description of how class_attribute works
neerajdotname authored
26 #
8ae25a8 @jeremy Introduce class_attribute to declare inheritable class attributes. Wr…
jeremy authored
27 # This matches normal Ruby method inheritance: think of writing an attribute
7c3d479 @josevalim More documentation to class_attribute.
josevalim authored
28 # on a subclass as overriding the reader method. However, you need to be aware
29 # when using +class_attribute+ with mutable structures as +Array+ or +Hash+.
30 # In such cases, you don't want to do changes in places but use setters:
31 #
32 # Base.setting = []
8a2b69b @pacoguzman applied guidelines to "# =>"
pacoguzman authored
33 # Base.setting # => []
34 # Subclass.setting # => []
7c3d479 @josevalim More documentation to class_attribute.
josevalim authored
35 #
36 # # Appending in child changes both parent and child because it is the same object:
37 # Subclass.setting << :foo
8a2b69b @pacoguzman applied guidelines to "# =>"
pacoguzman authored
38 # Base.setting # => [:foo]
39 # Subclass.setting # => [:foo]
7c3d479 @josevalim More documentation to class_attribute.
josevalim authored
40 #
41 # # Use setters to not propagate changes:
42 # Base.setting = []
43 # Subclass.setting += [:foo]
8a2b69b @pacoguzman applied guidelines to "# =>"
pacoguzman authored
44 # Base.setting # => []
45 # Subclass.setting # => [:foo]
8ae25a8 @jeremy Introduce class_attribute to declare inheritable class attributes. Wr…
jeremy authored
46 #
47 # For convenience, a query method is defined as well:
48 #
7c3d479 @josevalim More documentation to class_attribute.
josevalim authored
49 # Subclass.setting? # => false
60bbf16 @jeremy class_attribute gets instance methods which delegate to but may overr…
jeremy authored
50 #
51 # Instances may overwrite the class value in the same way:
52 #
53 # Base.setting = true
54 # object = Base.new
55 # object.setting # => true
56 # object.setting = false
57 # object.setting # => false
58 # Base.setting # => true
59 #
0ce5fb8 @bensie Add option to omit creating an instance reader method on class_attribute
bensie authored
60 # To opt out of the instance reader method, pass :instance_reader => false.
61 #
62 # object.setting # => NoMethodError
63 # object.setting? # => NoMethodError
64 #
60bbf16 @jeremy class_attribute gets instance methods which delegate to but may overr…
jeremy authored
65 # To opt out of the instance writer method, pass :instance_writer => false.
66 #
7c3d479 @josevalim More documentation to class_attribute.
josevalim authored
67 # object.setting = false # => NoMethodError
8ae25a8 @jeremy Introduce class_attribute to declare inheritable class attributes. Wr…
jeremy authored
68 def class_attribute(*attrs)
e6f15f0 @josevalim Ugh, the class_attribute pull request broke the build. fix it.
josevalim authored
69 options = attrs.extract_options!
70 instance_reader = options.fetch(:instance_reader, true)
71 instance_writer = options.fetch(:instance_writer, true)
60bbf16 @jeremy class_attribute gets instance methods which delegate to but may overr…
jeremy authored
72
76e0a9e @wycats Not using class_eval wasn't adding clarity here
wycats authored
73 attrs.each do |name|
74 class_eval <<-RUBY, __FILE__, __LINE__ + 1
75 def self.#{name}() nil end
76 def self.#{name}?() !!#{name} end
77
78 def self.#{name}=(val)
79 singleton_class.class_eval do
80 remove_possible_method(:#{name})
81 define_method(:#{name}) { val }
82 end
320382c @thedarkone Creating singleton class for every object whenever the instance-level…
thedarkone authored
83
bbafe73 @jonleighton Revert "Further simplify singleton_class checking in class_attribute"
jonleighton authored
84 if singleton_class?
85 class_eval do
86 remove_possible_method(:#{name})
87 def #{name}
88 defined?(@#{name}) ? @#{name} : singleton_class.#{name}
89 end
90 end
91 end
ad53b68 @wagenet Class Attribute setter returns set value
wagenet authored
92 val
76e0a9e @wycats Not using class_eval wasn't adding clarity here
wycats authored
93 end
94
0ce5fb8 @bensie Add option to omit creating an instance reader method on class_attribute
bensie authored
95 if instance_reader
96 remove_possible_method :#{name}
97 def #{name}
bbafe73 @jonleighton Revert "Further simplify singleton_class checking in class_attribute"
jonleighton authored
98 defined?(@#{name}) ? @#{name} : self.class.#{name}
0ce5fb8 @bensie Add option to omit creating an instance reader method on class_attribute
bensie authored
99 end
60bbf16 @jeremy class_attribute gets instance methods which delegate to but may overr…
jeremy authored
100
0ce5fb8 @bensie Add option to omit creating an instance reader method on class_attribute
bensie authored
101 def #{name}?
102 !!#{name}
103 end
76e0a9e @wycats Not using class_eval wasn't adding clarity here
wycats authored
104 end
105 RUBY
106
aacf258 @spastorino refactor evals and adds some __FILE__ and __LINE__
spastorino authored
107 attr_writer name if instance_writer
8ae25a8 @jeremy Introduce class_attribute to declare inheritable class attributes. Wr…
jeremy authored
108 end
109 end
bbafe73 @jonleighton Revert "Further simplify singleton_class checking in class_attribute"
jonleighton authored
110
111 private
112 def singleton_class?
c4a29de @tenderlove Merge pull request #4283 from lest/fix-singleton-checking
tenderlove authored
113 ancestors.first != self
bbafe73 @jonleighton Revert "Further simplify singleton_class checking in class_attribute"
jonleighton authored
114 end
8ae25a8 @jeremy Introduce class_attribute to declare inheritable class attributes. Wr…
jeremy authored
115 end
Something went wrong with that request. Please try again.