Class: Spider::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/spiderfw/config/configuration.rb

Constant Summary collapse

@@options =
{}
@@lang_aliases =
{}

Instance Attribute Summary collapse

Instance Method Summary collapse

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_setObject

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_keyObject

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_filesObject (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

#optionsObject

Returns the value of attribute options.



7
8
9
# File 'lib/spiderfw/config/configuration.rb', line 7

def options
  @options
end

Instance Method Details

#[](key) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/spiderfw/config/configuration.rb', line 139

def [](key)
    key = translate_key(key)
    val = @values[key]
    
    if (val.nil? && @options[key] && @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



241
242
243
244
# File 'lib/spiderfw/config/configuration.rb', line 241

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 -:choiches an array of allowed values -:type parameter type; can be one of int, string, bool



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/spiderfw/config/configuration.rb', line 190

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



253
254
255
256
257
258
259
# File 'lib/spiderfw/config/configuration.rb', line 253

def configure_set(name, values)
    s = (@sets[name] ||= Configuration.new(@prefix))
    s.options = @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



246
247
248
249
250
251
# File 'lib/spiderfw/config/configuration.rb', line 246

def create_prefix(name)
    first, rest = name.split('.', 2)
    @options[first] ||= {}
    v = sub_conf(first)
    v.create_prefix(rest) if rest
end

#eachObject



153
154
155
156
157
158
159
160
161
162
163
# File 'lib/spiderfw/config/configuration.rb', line 153

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



222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/spiderfw/config/configuration.rb', line 222

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_editorObject



306
307
308
309
310
311
312
313
# File 'lib/spiderfw/config/configuration.rb', line 306

def get_editor
    require 'spiderfw/config/configuration_editor'
    editor = ConfigurationEditor.new
    @loaded_files.each do |f|
        editor.load(f)
    end
    editor
end

#global_optionsObject



33
34
35
# File 'lib/spiderfw/config/configuration.rb', line 33

def global_options
    @@options
end

#include_set(name) ⇒ Object



261
262
263
264
265
266
267
# File 'lib/spiderfw/config/configuration.rb', line 261

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



167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/spiderfw/config/configuration.rb', line 167

def iterate_options(src, prefix, dst)
    src.each do |key, val|
        full_key = prefix ? "#{prefix}.#{key}" : key
        if val[:params]
            if val[:params][:type] == :conf
                iterate_options(src[key]["x"], full_key+'.x', dst)
            else
                dst << full_key
            end
        else
            iterate_options(src[key], full_key, dst)
        end
    end
end

#keysObject



134
135
136
# File 'lib/spiderfw/config/configuration.rb', line 134

def keys
    @values.keys
end

#load_yaml(file) ⇒ Object

Raises:



274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/spiderfw/config/configuration.rb', line 274

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



210
211
212
213
214
215
216
217
218
# File 'lib/spiderfw/config/configuration.rb', line 210

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

#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

Returns:

  • (Boolean)


269
270
271
# File 'lib/spiderfw/config/configuration.rb', line 269

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_hashObject



292
293
294
295
296
297
298
299
300
301
302
303
304
# File 'lib/spiderfw/config/configuration.rb', line 292

def to_hash
    h = {}
    self.options.each do |k|
        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_yamlObject



288
289
290
# File 'lib/spiderfw/config/configuration.rb', line 288

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])
        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