Class: CMDB::Interface
- Inherits:
-
Object
- Object
- CMDB::Interface
- Defined in:
- lib/cmdb/interface.rb
Instance Method Summary collapse
-
#each_pair {|key, value| ... } ⇒ Interface
Enumerate all of the keys in the CMDB.
-
#get(key) ⇒ Object?
Retrieve the value of a CMDB key, searching all sources in the order they were initialized.
-
#get!(key) ⇒ Object?
Retrieve the value of a CMDB key; raise an exception if the key is not found.
-
#initialize(*sources) ⇒ Interface
constructor
Create a new instance of the CMDB interface with the specified sources.
-
#search(query) ⇒ Hash
All keys/values that match query.
-
#set(key, value) ⇒ Source?
Set the value of a CMDB key.
-
#to_env ⇒ Object
Transform the entire CMDB into a flat Hash that can be merged into ENV.
Constructor Details
#initialize(*sources) ⇒ Interface
Create a new instance of the CMDB interface with the specified sources.
9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/cmdb/interface.rb', line 9 def initialize(*sources) # ensure no two sources share a prefix prefixes = {} sources.each do |s| next if s.prefix.nil? prefixes[s.prefix] ||= [] prefixes[s.prefix] << s end check_overlap(prefixes) @sources = sources.dup end |
Instance Method Details
#each_pair {|key, value| ... } ⇒ Interface
Enumerate all of the keys in the CMDB.
74 75 76 77 78 79 80 |
# File 'lib/cmdb/interface.rb', line 74 def each_pair(&block) @sources.each do |s| s.each_pair(&block) end self end |
#get(key) ⇒ Object?
Retrieve the value of a CMDB key, searching all sources in the order they were initialized.
27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/cmdb/interface.rb', line 27 def get(key) raise BadKey.new(key) unless key =~ VALID_KEY value = nil @sources.each do |s| value = s.get(key) break unless value.nil? end value end |
#get!(key) ⇒ Object?
Retrieve the value of a CMDB key; raise an exception if the key is not found.
45 46 47 |
# File 'lib/cmdb/interface.rb', line 45 def get!(key) get(key) || raise(MissingKey.new(key)) end |
#search(query) ⇒ Hash
Returns all keys/values that match query.
84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/cmdb/interface.rb', line 84 def search(query) query = Regexp.new('^' + Regexp.escape(query)) unless query.is_a?(Regexp) result = {} @sources.each do |s| s.each_pair do |k, v| result[k] = v if k =~ query end end result end |
#set(key, value) ⇒ Source?
Set the value of a CMDB key.
53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/cmdb/interface.rb', line 53 def set(key, value) raise BadKey.new(key) unless key =~ VALID_KEY wrote = nil @sources.each do |s| if s.respond_to?(:set) if s.set(key, value) wrote = s break end end end wrote end |
#to_env ⇒ Object
Transform the entire CMDB into a flat Hash that can be merged into ENV. Key names are transformed into underscore-separated, uppercase strings; all runs of non-alphanumeric, non-underscore characters are tranformed into a single underscore.
The transformation rules make it possible for key names to conflict, e.g. “apple.orange.pear” and “apple.orange_pear” cannot exist in the same flat hash. This method checks for such conflicts and raises rather than returning bad data.
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/cmdb/interface.rb', line 108 def to_env values = {} originals = {} @sources.each do |s| s.each_pair do |k, v| env = key_to_env(k, s) if (orig = originals[env]) raise NameConflict.new(env, [orig, k]) else values[env] = value_to_env(v) originals[env] = k end end end values end |