aboutsummaryrefslogtreecommitdiffstats
path: root/railties/guides/source/active_support_overview.textile
blob: 4f1d93559361026bf8c38e99b7d2f7f40f567176 (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
h2. Active Support Overview

Active Support is the Rails component responsible for providing Ruby language extensions, utilities, and other transversal stuff. It offers a richer bottom-line at the language level, targeted both at the development of Rails applications, and at the development of Rails itself.

By referring to this guide you will learn:

* The extensions to the Ruby core modules and classes provided by Rails.
* The rest of fundamental libraries available in Rails.

endprologue.

h3. Extensions to +Kernel+

...

h3. Extensions to +Object+

...

h3. Extensions to +Module+

...

h3. Extensions to +Class+

h4. Class Attribute Accessors

The macros +cattr_reader+, +cattr_writer+, and +cattr_accessor+ are analogous to their +attr_*+ counterparts but for classes. They initialize a class variable to +nil+ unless it already exists, and generate the corresponding class methods to access it:

<ruby>
class MysqlAdapter < AbstractAdapter
  # Generates class methods to access @@emulate_booleans.
  cattr_accessor :emulate_booleans
  self.emulate_booleans = true
end
</ruby>

Instance methods are created as well for convenience. For example given

<ruby>
module ActionController
  class Base
    cattr_accessor :logger
  end
end
</ruby>

we can access +logger+ in actions. The generation of the writer instance method can be prevented setting +:instance_writer+ to +false+ (not any false value, but exactly +false+):

<ruby>
module ActiveRecord
  class Base
    # No pluralize_table_names= instance writer is generated.
    cattr_accessor :pluralize_table_names, :instance_writer => false
  end
end
</ruby>

h4. Class Inheritable Attributes

Class variables are shared down the inheritance tree. Class instance variables are not shared, but they are not inherited either. The macros +class_inheritable_reader+, +class_inheritable_writer+, and +class_inheritable_accessor+ provide accesors for class-level data which is inherited but not shared with children:

<ruby>
module ActionController
  class Base
    # The value of allow_forgery_protection is inherited,
    # but its value in a particular class does not affect
    # the value in the rest of the controllers hierarchy.
    class_inheritable_accessor :allow_forgery_protection
  end
end
</ruby>

They accomplish this with class instance variables and cloning on subclassing, there are no class variables involved. Cloning is performed with +dup+ as long as the value is duplicable.

There are some variants specialised in arrays and hashes:

<ruby>
class_inheritable_array_writer
class_inheritable_array

class_inheritable_hash_writer
class_inheritable_hash
</ruby>

Those writers take any inherited array or hash into account and extend them rather than overwrite them.

As with vanilla class attribute accessors these macros create convenience instance methods for reading and writing. The generation of the writer instance method can be prevented setting +:instance_writer+ to +false+ (not any false value, but exactly +false+):

<ruby>
module ActiveRecord
  class Base
    class_inheritable_accessor :default_scoping, :instance_writer => false
  end
end
</ruby>

h3. Extensions to +NilClass+

...

h3. Extensions to +TrueClass+

...

h3. Extensions to +FalseClass+

...

h3. Extensions to +Symbol+

...

h3. Extensions to +String+

...

h3. Extensions to +Numeric+

...

h3. Extensions to +Integer+

...

h3. Extensions to +Float+

...

h3. Extensions to +BigDecimal+

...

h3. Extensions to +Enumerable+

...

h3. Extensions to +Array+

...

h3. Extensions to +Hash+

...

h3. Extensions to +Range+

...

h3. Extensions to +Proc+

...

h3. Extensions to +Date+

...

h3. Extensions to +DateTime+

...

h3. Extensions to +Time+

...

h3. Extensions to +Process+

...

h3. Extensions to +Pathname+

...

h3. Extensions to +File+

...

h3. Extensions to +Exception+

...

h3. Extensions to +NameError+

...

h3. Extensions to +LoadError+

...

h3. Extensions to +CGI+

...

h3. Extensions to +Benchmark+

...

h3. Changelog

"Lighthouse ticket":https://rails.lighthouseapp.com/projects/16213/tickets/67

* April 18, 2009: Initial version by "Xavier Noria":credits.html#fxn