Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 148 lines (135 sloc) 4.882 kB
4163cce @josevalim Clean up the config object in ActionPack. Create config_accessor whic…
josevalim authored
1 require 'active_support/concern'
2 require 'active_support/ordered_options'
894bdbd @josevalim Move variables to underscore format, update protected instance variab…
josevalim authored
3 require 'active_support/core_ext/array/extract_options'
a41c6c3 @wycats Start adding configuration to ActionView instead of using constants.
wycats authored
4
5 module ActiveSupport
c7adc96 @txus Documented active_support/configurable
txus authored
6 # Configurable provides a <tt>config</tt> method to store and retrieve
7 # configuration options as an <tt>OrderedHash</tt>.
a41c6c3 @wycats Start adding configuration to ActionView instead of using constants.
wycats authored
8 module Configurable
9 extend ActiveSupport::Concern
10
918dc27 @thedarkone Compile ActionController::Base.config's methods to avoid method_missi…
thedarkone authored
11 class Configuration < ActiveSupport::InheritableOptions
6067d16 @josevalim Call it compile_methods! and do the same on AM.
josevalim authored
12 def compile_methods!
d677097 @josevalim Fix configurable cristalization and tests.
josevalim authored
13 self.class.compile_methods!(keys)
918dc27 @thedarkone Compile ActionController::Base.config's methods to avoid method_missi…
thedarkone authored
14 end
15
d71d5ba update AS docs [ci skip]
Francesco Rodriguez authored
16 # Compiles reader methods so we don't have to go through method_missing.
6067d16 @josevalim Call it compile_methods! and do the same on AM.
josevalim authored
17 def self.compile_methods!(keys)
d677097 @josevalim Fix configurable cristalization and tests.
josevalim authored
18 keys.reject { |m| method_defined?(m) }.each do |key|
918dc27 @thedarkone Compile ActionController::Base.config's methods to avoid method_missi…
thedarkone authored
19 class_eval <<-RUBY, __FILE__, __LINE__ + 1
5a51848 @thedarkone Try to use Hash's native #[] for speed.
thedarkone authored
20 def #{key}; _get(#{key.inspect}); end
918dc27 @thedarkone Compile ActionController::Base.config's methods to avoid method_missi…
thedarkone authored
21 RUBY
22 end
23 end
24 end
25
a41c6c3 @wycats Start adding configuration to ActionView instead of using constants.
wycats authored
26 module ClassMethods
27 def config
f42562f Make ActiveSupport::Configurable work with modules
Oriol Gual and Josep M. Bach authored
28 @_config ||= if respond_to?(:superclass) && superclass.respond_to?(:config)
918dc27 @thedarkone Compile ActionController::Base.config's methods to avoid method_missi…
thedarkone authored
29 superclass.config.inheritable_copy
30 else
31 # create a new "anonymous" class that will host the compiled reader methods
8cda132 @thedarkone Make InheritableOptions's constructor more flexible.
thedarkone authored
32 Class.new(Configuration).new
918dc27 @thedarkone Compile ActionController::Base.config's methods to avoid method_missi…
thedarkone authored
33 end
a41c6c3 @wycats Start adding configuration to ActionView instead of using constants.
wycats authored
34 end
35
4163cce @josevalim Clean up the config object in ActionPack. Create config_accessor whic…
josevalim authored
36 def configure
37 yield config
38 end
39
5e1d40f add instance_accessor option to ActiveSupport::Configurable#config_ac…
Francesco Rodriguez authored
40 # Allows you to add shortcut so that you don't have to refer to attribute
019df98 @claudiob Replace comments' non-breaking spaces with spaces
claudiob authored
41 # through config. Also look at the example for config to contrast.
1efe30e @larrylv set up config_accessor with a default value by block
larrylv authored
42 #
5e1d40f add instance_accessor option to ActiveSupport::Configurable#config_ac…
Francesco Rodriguez authored
43 # Defines both class and instance config accessors.
aa1ac1c @neerajdotname Add comment for config_accessor method
neerajdotname authored
44 #
45 # class User
46 # include ActiveSupport::Configurable
47 # config_accessor :allowed_access
48 # end
49 #
1efe30e @larrylv set up config_accessor with a default value by block
larrylv authored
50 # User.allowed_access # => nil
5e1d40f add instance_accessor option to ActiveSupport::Configurable#config_ac…
Francesco Rodriguez authored
51 # User.allowed_access = false
1efe30e @larrylv set up config_accessor with a default value by block
larrylv authored
52 # User.allowed_access # => false
53 #
aa1ac1c @neerajdotname Add comment for config_accessor method
neerajdotname authored
54 # user = User.new
5e1d40f add instance_accessor option to ActiveSupport::Configurable#config_ac…
Francesco Rodriguez authored
55 # user.allowed_access # => false
aa1ac1c @neerajdotname Add comment for config_accessor method
neerajdotname authored
56 # user.allowed_access = true
57 # user.allowed_access # => true
58 #
1efe30e @larrylv set up config_accessor with a default value by block
larrylv authored
59 # User.allowed_access # => false
5e1d40f add instance_accessor option to ActiveSupport::Configurable#config_ac…
Francesco Rodriguez authored
60 #
61 # The attribute name must be a valid method name in Ruby.
62 #
63 # class User
64 # include ActiveSupport::Configurable
65 # config_accessor :"1_Badname"
66 # end
67 # # => NameError: invalid config attribute name
68 #
69 # To opt out of the instance writer method, pass <tt>instance_writer: false</tt>.
70 # To opt out of the instance reader method, pass <tt>instance_reader: false</tt>.
71 #
72 # class User
73 # include ActiveSupport::Configurable
74 # config_accessor :allowed_access, instance_reader: false, instance_writer: false
75 # end
76 #
77 # User.allowed_access = false
019df98 @claudiob Replace comments' non-breaking spaces with spaces
claudiob authored
78 # User.allowed_access # => false
5e1d40f add instance_accessor option to ActiveSupport::Configurable#config_ac…
Francesco Rodriguez authored
79 #
80 # User.new.allowed_access = true # => NoMethodError
81 # User.new.allowed_access # => NoMethodError
82 #
83 # Or pass <tt>instance_accessor: false</tt>, to opt out both instance methods.
84 #
85 # class User
86 # include ActiveSupport::Configurable
87 # config_accessor :allowed_access, instance_accessor: false
88 # end
89 #
90 # User.allowed_access = false
019df98 @claudiob Replace comments' non-breaking spaces with spaces
claudiob authored
91 # User.allowed_access # => false
5e1d40f add instance_accessor option to ActiveSupport::Configurable#config_ac…
Francesco Rodriguez authored
92 #
93 # User.new.allowed_access = true # => NoMethodError
1efe30e @larrylv set up config_accessor with a default value by block
larrylv authored
94 # User.new.allowed_access # => NoMethodError
95 #
96 # Also you can pass a block to set up the attribute with a default value.
97 #
98 # class User
99 # include ActiveSupport::Configurable
100 # config_accessor :hair_colors do
101 # [:brown, :black, :blonde, :red]
102 # end
103 # end
104 #
105 # User.hair_colors # => [:brown, :black, :blonde, :red]
4163cce @josevalim Clean up the config object in ActionPack. Create config_accessor whic…
josevalim authored
106 def config_accessor(*names)
894bdbd @josevalim Move variables to underscore format, update protected instance variab…
josevalim authored
107 options = names.extract_options!
108
4163cce @josevalim Clean up the config object in ActionPack. Create config_accessor whic…
josevalim authored
109 names.each do |name|
5e1d40f add instance_accessor option to ActiveSupport::Configurable#config_ac…
Francesco Rodriguez authored
110 raise NameError.new('invalid config attribute name') unless name =~ /^[_A-Za-z]\w*$/
111
008023c @benolee correct line numbers for reader and writer methods
benolee authored
112 reader, reader_line = "def #{name}; config.#{name}; end", __LINE__
113 writer, writer_line = "def #{name}=(value); config.#{name} = value; end", __LINE__
4163cce @josevalim Clean up the config object in ActionPack. Create config_accessor whic…
josevalim authored
114
008023c @benolee correct line numbers for reader and writer methods
benolee authored
115 singleton_class.class_eval reader, __FILE__, reader_line
116 singleton_class.class_eval writer, __FILE__, writer_line
5e1d40f add instance_accessor option to ActiveSupport::Configurable#config_ac…
Francesco Rodriguez authored
117
118 unless options[:instance_accessor] == false
008023c @benolee correct line numbers for reader and writer methods
benolee authored
119 class_eval reader, __FILE__, reader_line unless options[:instance_reader] == false
120 class_eval writer, __FILE__, writer_line unless options[:instance_writer] == false
5e1d40f add instance_accessor option to ActiveSupport::Configurable#config_ac…
Francesco Rodriguez authored
121 end
1efe30e @larrylv set up config_accessor with a default value by block
larrylv authored
122 send("#{name}=", yield) if block_given?
a41c6c3 @wycats Start adding configuration to ActionView instead of using constants.
wycats authored
123 end
124 end
125 end
126
c7adc96 @txus Documented active_support/configurable
txus authored
127 # Reads and writes attributes from a configuration <tt>OrderedHash</tt>.
9b96de6 @suchasurge Some style changes
suchasurge authored
128 #
6ce844a @fxn removes Examples headers introduced in 9b96de6
fxn authored
129 # require 'active_support/configurable'
9b96de6 @suchasurge Some style changes
suchasurge authored
130 #
c7adc96 @txus Documented active_support/configurable
txus authored
131 # class User
132 # include ActiveSupport::Configurable
6ce844a @fxn removes Examples headers introduced in 9b96de6
fxn authored
133 # end
c7adc96 @txus Documented active_support/configurable
txus authored
134 #
135 # user = User.new
6ce844a @fxn removes Examples headers introduced in 9b96de6
fxn authored
136 #
c7adc96 @txus Documented active_support/configurable
txus authored
137 # user.config.allowed_access = true
138 # user.config.level = 1
139 #
140 # user.config.allowed_access # => true
141 # user.config.level # => 1
a41c6c3 @wycats Start adding configuration to ActionView instead of using constants.
wycats authored
142 def config
918dc27 @thedarkone Compile ActionController::Base.config's methods to avoid method_missi…
thedarkone authored
143 @_config ||= self.class.config.inheritable_copy
a41c6c3 @wycats Start adding configuration to ActionView instead of using constants.
wycats authored
144 end
145 end
c7adc96 @txus Documented active_support/configurable
txus authored
146 end
147
Something went wrong with that request. Please try again.