Class: Spider::Configuration
- Defined in:
- lib/spiderfw/config/configuration.rb
Constant Summary collapse
- @@options =
{}
- @@lang_aliases =
{}
Instance Attribute Summary collapse
-
#current_set ⇒ Object
Returns the value of attribute current_set.
-
#hash_key ⇒ Object
Returns the value of attribute hash_key.
-
#loaded_files ⇒ Object
readonly
Returns the value of attribute loaded_files.
-
#options ⇒ Object
Returns the value of attribute options.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, val) ⇒ Object
- #conf_alias(name, aliases = nil) ⇒ Object
-
#config(key = nil) ⇒ Object
FIXME: temporarely allows old behaviour.
-
#config_option(name, description = nil, params = {}, &proc) ⇒ Object
Sets an allowed configuration option Possible params are: -:default the default value for the option; if it is a proc, it will be called -:fallback like default, but use another config key instead (an array may be specified) -:choices an array of allowed values -:type parameter type; can be one of int, string, bool.
- #configure_set(name, values) ⇒ Object
- #convert_val(type, val) ⇒ Object
- #create_prefix(name) ⇒ Object
- #each ⇒ Object
- #get(key) ⇒ Object
- #get_editor ⇒ Object
- #global_options ⇒ Object
- #include_set(name) ⇒ Object
-
#initialize(prefix = '') ⇒ Configuration
constructor
A new instance of Configuration.
- #iterate_options(src, prefix, dst) ⇒ Object
- #keys ⇒ Object
- #load_yaml(file) ⇒ Object
- #option(name) ⇒ Object
- #options_list ⇒ Object
- #set(key, val) ⇒ Object (also: #configure)
- #set_included?(name) ⇒ Boolean
- #sub_conf(name) ⇒ Object
- #to_hash ⇒ Object
- #to_yaml ⇒ Object
- #translate_key(key) ⇒ Object
Constructor Details
#initialize(prefix = '') ⇒ Configuration
Returns a new instance of Configuration.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/spiderfw/config/configuration.rb', line 12 def initialize(prefix='') prefix = prefix[1..prefix.length-1] if (prefix[0..0] == '.') @prefix = prefix @options = @@options if prefix != '' cur = '' @options = prefix.split('.').inject(@options) do |o, part| cur += '.' unless cur.empty?; cur += part config_option(cur, '__auto__') unless o[part] #raise ConfigurationException.new(:invalid_option), _("%s is not a configuration option") % cur unless o[part] o[part] end end @sets = {} @current_set = 'default' @sets['default'] = self @values = {} @hash_key = nil @loaded_files = [] end |
Instance Attribute Details
#current_set ⇒ Object
Returns the value of attribute current_set.
7 8 9 |
# File 'lib/spiderfw/config/configuration.rb', line 7 def current_set @current_set end |
#hash_key ⇒ Object
Returns the value of attribute hash_key.
7 8 9 |
# File 'lib/spiderfw/config/configuration.rb', line 7 def hash_key @hash_key end |
#loaded_files ⇒ Object (readonly)
Returns the value of attribute loaded_files.
8 9 10 |
# File 'lib/spiderfw/config/configuration.rb', line 8 def loaded_files @loaded_files end |
#options ⇒ Object
Returns the value of attribute options.
7 8 9 |
# File 'lib/spiderfw/config/configuration.rb', line 7 def @options end |
Instance Method Details
#[](key) ⇒ Object
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/spiderfw/config/configuration.rb', line 139 def [](key) key = translate_key(key) val = @values[key] return val unless @options[key] if val.nil? && fallback = @options[key][:params][:fallback] fallback = [fallback] unless fallback.is_a?(Array) fallback.each do |fb| val = Spider.conf.get(fb) break if val end end if val.nil? && @options[key][:params][:default] default = @options[key][:params][:default] val = default if (default.class == Proc) val = @hash_key ? default.call(@hash_key) : default.call end end return val end |
#[]=(key, val) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/spiderfw/config/configuration.rb', line 54 def []=(key, val) key = translate_key(key) config_option(key, "__auto__") unless @options[key] #raise ConfigurationException.new(:invalid_option), _("%s is not a configuration option") % key unless @options && @options[key] process = @options[key][:params][:process] val = process.call(val) if (process) first, rest = key.split('.', 2) @values[key] = val action = @options[key][:params][:action] action.call(val) if (action) end |
#conf_alias(name, aliases = nil) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/spiderfw/config/configuration.rb', line 37 def conf_alias(name, aliases=nil) if (aliases) aliases.each do |locale, translated| @@lang_aliases[locale] ||= {} @@lang_aliases[locale][translated] = name end elsif (name.is_a?(Hash)) name.each do |locale, aliases| @@lang_aliases[locale] ||= {} aliases.each do |name, translated| @@lang_aliases[locale][translated] = name end end end end |
#config(key = nil) ⇒ Object
FIXME: temporarely allows old behaviour
250 251 252 253 |
# File 'lib/spiderfw/config/configuration.rb', line 250 def config(key=nil) return self unless key get(key) end |
#config_option(name, description = nil, params = {}, &proc) ⇒ Object
Sets an allowed configuration option Possible params are: -:default the default value for the option; if it is a proc, it will be called -:fallback like default, but use another config key instead (an array may be specified) -:choices an array of allowed values -:type parameter type; can be one of int, string, bool
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/spiderfw/config/configuration.rb', line 199 def config_option(name, description=nil, params={}, &proc) name = name.to_s if (params.empty? && description.is_a?(Hash)) params = description description = '' end o = @options params[:action] ||= proc if proc params[:type] ||= String first, rest = name.split('.', 2) while (rest) o = (o[first] ||= {}) first, rest = rest.split('.', 2) end if params && description o[first] = {:description => description, :params => params} end o[first] end |
#configure_set(name, values) ⇒ Object
262 263 264 265 266 267 268 |
# File 'lib/spiderfw/config/configuration.rb', line 262 def configure_set(name, values) s = (@sets[name] ||= Configuration.new(@prefix)) s. = @options values.each do |k, v| s.configure(k, v) end end |
#convert_val(type, val) ⇒ Object
120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/spiderfw/config/configuration.rb', line 120 def convert_val(type, val) case type.name when 'String' val = val.to_s when 'Symbol' val = val.to_sym when 'Fixnum' val = val.to_i when 'Float' val = val.to_f end return val end |
#create_prefix(name) ⇒ Object
255 256 257 258 259 260 |
# File 'lib/spiderfw/config/configuration.rb', line 255 def create_prefix(name) first, rest = name.split('.', 2) @options[first] ||= {} v = sub_conf(first) v.create_prefix(rest) if rest end |
#each ⇒ Object
161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/spiderfw/config/configuration.rb', line 161 def each @values.each do |key, val| if (val.class == Configuration) val.each do |k, v| yield key+'.'+k, v end else yield key, val end end end |
#get(key) ⇒ Object
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 |
# File 'lib/spiderfw/config/configuration.rb', line 231 def get(key) key = key.to_s first, rest = key.split('.', 2) if rest first = translate_key(first) v = sub_conf(first) if (@values[first].is_a?(Configuration)) return @values[first].config(rest) elsif (@values[first].is_a?(Hash) || @values[first].is_a?(Array)) return @values[first][rest] end else key = translate_key(key) self[key] end end |
#get_editor ⇒ Object
316 317 318 319 320 321 322 323 324 325 326 327 |
# File 'lib/spiderfw/config/configuration.rb', line 316 def get_editor require 'spiderfw/config/configuration_editor' require 'pathname' editor = ConfigurationEditor.new config_path = Pathname.new(Spider.paths[:config]).realpath.to_s @loaded_files.each do |f| f = Pathname.new(f).realpath.to_s next unless f.index(config_path) == 0 editor.load(f) end editor end |
#global_options ⇒ Object
33 34 35 |
# File 'lib/spiderfw/config/configuration.rb', line 33 def @@options end |
#include_set(name) ⇒ Object
270 271 272 273 274 275 276 |
# File 'lib/spiderfw/config/configuration.rb', line 270 def include_set(name) return if (self == @sets[name]) ( @sets[name] ||= Configuration.new(@prefix) ).each do |key, val| configure(key, val) end @sets[name] = self end |
#iterate_options(src, prefix, dst) ⇒ Object
175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/spiderfw/config/configuration.rb', line 175 def (src, prefix, dst) src.each do |key, val| full_key = prefix ? "#{prefix}.#{key}" : key if val[:params] if val[:params][:type] == :conf (src[key]["x"], full_key+'.x', dst) else dst << full_key end else (src[key], full_key, dst) end end end |
#keys ⇒ Object
134 135 136 |
# File 'lib/spiderfw/config/configuration.rb', line 134 def keys @values.keys end |
#load_yaml(file) ⇒ Object
283 284 285 286 287 288 289 290 291 292 293 294 295 |
# File 'lib/spiderfw/config/configuration.rb', line 283 def load_yaml(file) y = YAML::load_file(file) raise ConfigurationException.new(:yaml), "Can't parse configuration file #{file}" unless y y.each do |key, val| case key when /set (.+)/ configure_set($1, val) else configure(key, val) end end @loaded_files << file end |
#option(name) ⇒ Object
219 220 221 222 223 224 225 226 227 |
# File 'lib/spiderfw/config/configuration.rb', line 219 def option(name) o = @options first, rest = name.split('.', 2) while (rest) o = (o[first] ||= {}) first, rest = rest.split('.', 2) end return o[first] end |
#options_list ⇒ Object
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/spiderfw/config/configuration.rb', line 173 def = [] def (src, prefix, dst) src.each do |key, val| full_key = prefix ? "#{prefix}.#{key}" : key if val[:params] if val[:params][:type] == :conf (src[key]["x"], full_key+'.x', dst) else dst << full_key end else (src[key], full_key, dst) end end end (@options, nil, ) end |
#set(key, val) ⇒ Object Also known as: configure
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 |
# File 'lib/spiderfw/config/configuration.rb', line 80 def set(key, val) first, rest = key.split('.', 2) if rest first = translate_key(first) begin sub_conf(first).configure(rest, val) rescue ConfigurationException # raise only top level exception raise ConfigurationException.new(:invalid_option), _("%s is not a configuration option") % key end else key = translate_key(key) config_option(key, '__auto__') unless @options[key] if val.is_a?(Hash) if (@options[key][:params] && @options[key][:params][:type] == :conf) @values[key] ||= Configuration.new(@prefix+".#{key}") # FIXME: needed? val.each do |h_key, h_val| unless h_val.is_a?(Hash) # Reference to another subconf item self[key][h_key] = self[key][h_val] next end self[key][h_key] = Configuration.new(@prefix+".#{key}.x") self[key][h_key].hash_key = h_key h_val.each { |k, v| self[key][h_key].set(k, v) } end elsif (!@options[key][:params] || @options[key][:params][:type] != Hash) # sub conf @values[key] ||= Configuration.new(@prefix+".#{key}") val.each { |k, v| self[key].set(k.to_s, v) } else self[key] = val end else val = convert_val(@options[key][:params][:type], val) if (@options[key][:params][:type]) @options[key][:params][:do].call(val) if @options[key][:params][:do] self[key] = val end end end |
#set_included?(name) ⇒ Boolean
278 279 280 |
# File 'lib/spiderfw/config/configuration.rb', line 278 def set_included?(name) @sets[name] == self end |
#sub_conf(name) ⇒ Object
66 67 68 |
# File 'lib/spiderfw/config/configuration.rb', line 66 def sub_conf(name) @values[name] ||= Configuration.new(@prefix+".#{name}") end |
#to_hash ⇒ Object
301 302 303 304 305 306 307 308 309 310 311 312 313 314 |
# File 'lib/spiderfw/config/configuration.rb', line 301 def to_hash h = {} self..each do |k| k = k.first v = self[k] if v.is_a?(self.class) v = v.to_hash next if v.empty? end next if v.class == Class h[k] = v end return h end |
#to_yaml ⇒ Object
297 298 299 |
# File 'lib/spiderfw/config/configuration.rb', line 297 def to_yaml to_hash.to_yaml end |
#translate_key(key) ⇒ Object
70 71 72 73 74 75 76 77 78 |
# File 'lib/spiderfw/config/configuration.rb', line 70 def translate_key(key) if (!@options[key] && key != 'locale') locale = Spider.locale locale = $1 if locale =~ /^([^@\.]+)[@\.].+/ a = @@lang_aliases[locale][key] if @@lang_aliases[locale] return a.to_s if a end return key.to_s end |