Object
Ubiquo::Config offers a place where store configuration variables.
It stores all the values in a data structure like that:
configuration = {
<context_name> => {
:allowed_options => [
<option_1>,
<option_2>,
...
<option_N>,
]
:default_values => {
<option_1> => <value_1>,
<option_2> => <value_2>,
...
<option_N> => <value_N>,
}
:values => {
<option_1> => <value_1>,
<option_2> => <value_2>,
...
<option_N> => <value_N>,
},
:inherited_values => {
<option_1> => <lambda_1>,
<option_2> => <lambda_2>,
...
<option_N> => <lambda_N>,
}
}
}
See public methods to know how to use.
Adds an option to the current context (default :BASE). Default value is optional. Example:
>> Ubiquo::Config.add(:new_option, 1) >> Ubiquo::Config.get(:new_option) => 1
Can be used with a block. Example:
>> Ubiquo::Config.add do |configurator|
configurator.option_1 = 1
configurator.option_2 = 2
end
>> Ubiquo::Config.get(:option_1)
=> 1
>> Ubiquo::Config.get(:option_2)
=> 2
# File vendor/plugins/ubiquo_core/lib/ubiquo/config.rb, line 58
58: def self.add(name = nil, default_value = nil, &block)
59: if block_given?
60: block_assignment(&block).each do |name, default_value|
61: self.add(name.to_sym, default_value)
62: end
63: else
64: raise InvalidOptionName if !check_valid_name(name)
65: raise AlreadyExistingOption if configuration[self.current_context][:allowed_options].include?(name)
66: name = name.to_sym
67: configuration[self.current_context][:default_values][name] = default_value if !default_value.nil?
68: configuration[self.current_context][:allowed_options] << name
69: end
70: end
(Not documented)
# File vendor/plugins/ubiquo_core/lib/ubiquo/config.rb, line 72
72: def self.add_inheritance(name, inherited_value)
73: raise InvalidOptionName if !check_valid_name(name)
74: raise OptionNotFound if !self.option_exists?(name)
75: name = name.to_sym
76:
77: proc = case inherited_value
78: when Hash
79: lambda{ Ubiquo::Config.context(inherited_value.keys.first).get(inherited_value.values.first)}
80: when String, Symbol
81: lambda{ Ubiquo::Config.context(:BASE).get(inherited_value)}
82: end
83:
84: configuration[self.current_context][:inherited_values][name] = proc
85: end
(Not documented)
# File vendor/plugins/ubiquo_core/lib/ubiquo/config.rb, line 180
180: def self.call(name, run_in, options = {})
181: case option = self.get(name)
182: when Proc
183: method_name = "_ubi_config_call_#{Time.now.to_f*10000}"
184: while(run_in.respond_to?(method_name))
185: method_name = "_" + method_name
186: end
187: run_in.class.send(:define_method, method_name, &option)
188: run_in.send(method_name, options).tap do
189: run_in.class.send(:remove_method, method_name)
190: end
191: when String, Symbol
192: run_in.send option, options
193: end
194: end
Allow to work in the desired context. Can be used inline or as block. Example:
>> Ubiquo::Config.add(:a, 1) # Context :BASE
>> Ubiquo::Config.create_context(:context)
>> Ubiquo::Config.context(:context).add(:a, 2)
>> Ubiquo::Config.context(:context).get(:a)
=> 2
>> value = nil
=> nil
>> Ubiquo::Config.context(:context) do |config|
config.set(:a), 3
value = config.get(:a)
end
>> value
=> 3
# File vendor/plugins/ubiquo_core/lib/ubiquo/config.rb, line 228
228: def self.context(name, &block)
229: raise ContextNotFound if !self.context_exists?(name)
230: if block_given?
231: returning_value = nil
232: begin
233: old_context, @context = @context, name.to_sym
234: returning_value = block.call(self)
235: rescue
236: raise $!
237: ensure
238: @context = old_context
239: end
240: returning_value
241: else
242: # raise BlockNeeded
243: myself = self
244: Proxy.send(:define_method, :my_method_missing){ |method, args, block|
245: return_value = nil
246: myself.context(name){|contexted|
247: return_value = contexted.send(method, *args, &block)
248: }
249: return_value
250: }
251: Proxy.new
252: end
253: end
Returns true only if the context exists
# File vendor/plugins/ubiquo_core/lib/ubiquo/config.rb, line 261
261: def self.context_exists?(name)
262: configuration.include?(name)
263: end
Creates a context. Contexts contains an independent structure which stores options. Example:
>> Ubiquo::Config.add(:a, 1) # Context :BASE >> Ubiquo::Config.create_context(:context) >> Ubiquo::Config.context(:context).add(:a, 2) >> Ubiquo::Config.context(:context).get(:a) => 2
# File vendor/plugins/ubiquo_core/lib/ubiquo/config.rb, line 204
204: def self.create_context(name)
205: raise InvalidContextName if !check_valid_context_name(name)
206: raise AlreadyExistingContext if configuration.include?(name)
207: name = name.to_sym
208: configuration.merge!(self.new_context_options(name))
209: true
210: end
Get the value of a given option name in the current context(default :BASE). Will return the standard value if setted or default value. If no default value or standard value defined, raises Ubiquo::Config::ValueNeverSetted Example:
>> Ubiquo::Config.add(:a, 1) >> Ubiquo::Config.get(:a) => 1 >> Ubiquo::Config.set(:a, 2) >> Ubiquo::Config.get(:a) => 2
# File vendor/plugins/ubiquo_core/lib/ubiquo/config.rb, line 164
164: def self.get(name)
165: raise InvalidOptionName if !check_valid_name(name)
166: raise OptionNotFound.new(name) if !self.option_exists?(name)
167: name = name.to_sym
168:
169: if configuration[self.current_context][:values].include?(name)
170: configuration[self.current_context][:values][name]
171: elsif configuration[self.current_context][:default_values].include?(name)
172: configuration[self.current_context][:default_values][name]
173: elsif configuration[self.current_context][:inherited_values].include?(name)
174: configuration[self.current_context][:inherited_values][name].call
175: else
176: raise ValueNeverSetted
177: end
178: end
(Not documented)
# File vendor/plugins/ubiquo_core/lib/ubiquo/config.rb, line 255
255: def self.option_exists?(name)
256: configuration[self.current_context][:allowed_options].include?(name)
257: end
Set a value to an existent option of the current context( default :BASE). Example:
>> Ubiquo::Config.add(:a) >> Ubiquo::Config.get(:a) => nil >> Ubiquo::Config.set(:a, 1) >> Ubiquo::Config.get(:a) => 1
Can ge used with a block.
>> Ubiquo::Config.add(:a)
>> Ubiquo::Config.add(:b)
>> Ubiquo::Config.add(:c)
>> Ubiquo::Config.set do |configurator|
configurator.a = 1
configurator.b = 2
configurator.c = 3
end
>> Ubiquo::Config.get(:c)
=> 3
# File vendor/plugins/ubiquo_core/lib/ubiquo/config.rb, line 142
142: def self.set(name = nil, value = nil, &block)
143: if block_given?
144: block_assignment(&block).each do |name, default_value|
145: set(name, default_value)
146: end
147: else
148: raise InvalidOptionName if !check_valid_name(name)
149: raise OptionNotFound if !self.option_exists?(name)
150: name = name.to_sym
151: configuration[self.current_context][:values][name] = value
152: end
153: end
Set a default value to an existent option of the current context( default :BASE). Example:
>> Ubiquo::Config.add(:a) >> Ubiquo::Config.get(:a) => nil >> Ubiquo::Config.set_default(:a, 1) >> Ubiquo::Config.get(:a) => 1
Can ge used with a block.
>> Ubiquo::Config.add(:a)
>> Ubiquo::Config.add(:b)
>> Ubiquo::Config.add(:c)
>> Ubiquo::Config.set_default do |configurator|
configurator.a = 1
configurator.b = 2
configurator.c = 3
end
>> Ubiquo::Config.get(:c)
=> 3
# File vendor/plugins/ubiquo_core/lib/ubiquo/config.rb, line 108
108: def self.set_default(name = nil, default_value = nil, &block)
109: if block_given?
110: block_assignment(&block).each do |name, default_value|
111: set_default_value(name, default_value)
112: end
113: else
114: raise InvalidOptionName if !check_valid_name(name)
115: raise OptionNotFound if !self.option_exists?(name)
116: name = name.to_sym
117: configuration[self.current_context][:default_values][name] = default_value
118: end
119: end
Disabled; run with $DEBUG to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.