Class: CMDB::Source
- Inherits:
-
Object
- Object
- CMDB::Source
- Defined in:
- lib/cmdb/source.rb
Defined Under Namespace
Classes: Consul, File, Memory, Network
Instance Attribute Summary collapse
-
#prefix ⇒ nil, String
readonly
The dot-notation prefix of all keys provided by this source.
-
#uri ⇒ URI
readonly
Logical description of this source.
Class Method Summary collapse
-
.create(uri) ⇒ Object
Construct a source given a URI that identifies both the type of source (consul, file or environment) and its location if applicable.
-
.detect ⇒ Array
Test for the presence of some default sources and return any that exist.
Instance Method Summary collapse
-
#initialize(uri, prefix) ⇒ Source
constructor
Construct a new Source.
Constructor Details
#initialize(uri, prefix) ⇒ Source
Construct a new Source.
86 87 88 89 90 |
# File 'lib/cmdb/source.rb', line 86 def initialize(uri, prefix) uri = URI.parse(uri) if uri.is_a?(String) @uri = uri @prefix = prefix end |
Instance Attribute Details
#prefix ⇒ nil, String (readonly)
The dot-notation prefix of all keys provided by this source. No two sources can share a prefix (other than nil) and no source’s prefix can be a prefix of any other source’s prefix.
Some sources have no prefix, in which case this reader returns nil.
13 14 15 |
# File 'lib/cmdb/source.rb', line 13 def prefix @prefix end |
#uri ⇒ URI (readonly)
Returns logical description of this source.
4 5 6 |
# File 'lib/cmdb/source.rb', line 4 def uri @uri end |
Class Method Details
.create(uri) ⇒ Object
Construct a source given a URI that identifies both the type of source (consul, file or environment) and its location if applicable. Choose a suitable prefix for the source based on the URI contents.
This method accepts a special URI notation that is specific to the cmdb gem; in this notation, the scheme of the URI specifies the type of source (consul, file, etc) and the other components describe how to locate the source.
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/cmdb/source.rb', line 48 def self.create(uri) uri = URI.parse(uri) if uri.is_a?(String) if !uri.path.nil? && !uri.path.empty? prefix = ::File.basename(uri.path, '.*') else prefix = nil end case uri.scheme when 'consul' Source::Consul.new(uri, prefix) when 'file' Source::File.new(uri, prefix) when 'memory' Source::Memory.new({},prefix) else raise ArgumentError, "Unrecognized URL scheme '#{uri.scheme}'" end end |
.detect ⇒ Array
Test for the presence of some default sources and return any that exist.
72 73 74 75 76 77 78 79 |
# File 'lib/cmdb/source.rb', line 72 def self.detect sources = [] consul = create('consul://localhost') sources << consul if consul.ping sources end |