Module: Rbbt::Config
- Defined in:
- lib/rbbt/util/config.rb
Constant Summary collapse
- CACHE =
IndiferentHash.setup({})
Class Method Summary collapse
- .add_entry(key, value, tokens) ⇒ Object
- .get(key, *tokens) ⇒ Object
- .load_config ⇒ Object
- .load_file(file) ⇒ Object
- .match(entries, token) ⇒ Object
- .set(values, *tokens) ⇒ Object
- .token_priority(token) ⇒ Object
Class Method Details
.add_entry(key, value, tokens) ⇒ Object
8 9 10 11 |
# File 'lib/rbbt/util/config.rb', line 8 def self.add_entry(key, value, tokens) CACHE[key.to_s] ||= [] CACHE[key.to_s] << [tokens, value] end |
.get(key, *tokens) ⇒ Object
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 |
# File 'lib/rbbt/util/config.rb', line 82 def self.get(key, *tokens) = tokens.pop if Hash === tokens.last default = .nil? ? nil : [:default] tokens = tokens.flatten file, _sep, line = caller.reject{|l| l =~ /rbbt\/(?:resource\.rb|workflow\.rb)/ or l =~ /rbbt\/resource\/path\.rb/ or l =~ /rbbt\/util\/misc\.rb/ or l =~ /accessor\.rb/ or l =~ /progress-monitor\.rb/ }.first.partition(":") File.(file) tokens << ("file:" << file) tokens << ("line:" << file << ":" << line.sub(/:in \`.*/,'')) entries = CACHE[key.to_s] priorities = {} tokens = tokens + ["key:" << key.to_s] tokens.each do |token| token_prio = match entries, token.to_s token_prio.each do |prio, values| priorities[prio] ||= [] priorities[prio].concat(values) end end value = priorities.empty? ? default : priorities.sort_by{|p,v| p}.first.last.first value = false if value == 'false' Log.debug "Value #{value.inspect} for config key '#{ key }': #{tokens * ", "}" value end |
.load_config ⇒ Object
24 25 26 27 28 |
# File 'lib/rbbt/util/config.rb', line 24 def self.load_config Rbbt.etc.config.find_all.each do |file| self.load_file(file) end end |
.load_file(file) ⇒ Object
13 14 15 16 17 18 19 20 21 22 |
# File 'lib/rbbt/util/config.rb', line 13 def self.load_file(file) Log.debug "Loading file: #{ file }" TSV.traverse file, :type => :array do |line| next if line =~ /^#/ key, value, *tokens = line.split(/\s/) tokens << "key:#{key}" self.add_entry(key, value, tokens) if key end end |
.match(entries, token) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/rbbt/util/config.rb', line 66 def self.match(entries, token) priorities = {} entries.each do |tokens, value| best_prio = nil tokens.each do |tok| tok, prio = token_priority tok best_prio = prio if best_prio.nil? or best_prio > prio next if prio > best_prio next unless tok == token priorities[prio] ||= [] priorities[prio] << value end end if entries priorities end |
.set(values, *tokens) ⇒ Object
30 31 32 33 34 35 36 37 38 |
# File 'lib/rbbt/util/config.rb', line 30 def self.set(values, *tokens) if not Hash === values values = {values => tokens.shift} end values.each do |key,value| add_entry key, value, tokens end end |
.token_priority(token) ⇒ Object
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 |
# File 'lib/rbbt/util/config.rb', line 40 def self.token_priority(token) token, _sep, priority = token.to_s.partition("::") if priority.nil? || priority.empty? type, _sep, rest = token.partition(":") priority = case type when "workflow" 4 when "task" 3 when "file" 2 when "line" 1 when "key" 20 else 10 end else priority = priority.to_i end [token, priority] end |