Class: OodCore::Clusters
- Inherits:
-
Object
- Object
- OodCore::Clusters
- Includes:
- Enumerable
- Defined in:
- lib/ood_core/clusters.rb
Overview
An enumerable that contains a list of Cluster objects
Constant Summary collapse
- CONFIG_VERSION =
The format version of the configuration file
['v2', 'v1']
Class Method Summary collapse
-
.load_file(path) ⇒ Clusters
Parse a configuration file or a set of configuration files in a directory.
Instance Method Summary collapse
-
#[](id) ⇒ Cluster?
Find cluster in list using the id of the cluster.
-
#each {|cluster| ... } ⇒ Object
For a block {|cluster| …}.
-
#initialize(clusters = []) ⇒ Clusters
constructor
A new instance of Clusters.
Constructor Details
#initialize(clusters = []) ⇒ Clusters
Returns a new instance of Clusters.
109 110 111 |
# File 'lib/ood_core/clusters.rb', line 109 def initialize(clusters = []) @clusters = clusters end |
Class Method Details
.load_file(path) ⇒ Clusters
Parse a configuration file or a set of configuration files in a directory
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/ood_core/clusters.rb', line 17 def load_file(path) config = Pathname.new(path.to_s). clusters = [] if config.file? if config.readable? CONFIG_VERSION.any? do |version| begin YAML.safe_load(config.read)&.fetch(version, {}).each do |k, v| clusters << Cluster.new(send("parse_#{version}", id: k, cluster: v)) end rescue Psych::SyntaxError => e clusters << InvalidCluster.new( id: config.basename(config.extname).to_s, errors: [ e..to_s ] ) end end end elsif config.directory? Pathname.glob([config.join("*.yml"), config.join("*.yaml")]).select(&:file?).select(&:readable?).each do |p| CONFIG_VERSION.any? do |version| begin if cluster = YAML.safe_load(p.read)&.fetch(version, nil) clusters << Cluster.new(send("parse_#{version}", id: p.basename(p.extname()).to_s, cluster: cluster)) end rescue Psych::SyntaxError => e clusters << InvalidCluster.new( id: p.basename(p.extname).to_s, errors: [ e..to_s ] ) end end end else raise ConfigurationNotFound, "configuration file '#{config}' does not exist" end new clusters end |
Instance Method Details
#[](id) ⇒ Cluster?
Find cluster in list using the id of the cluster
116 117 118 |
# File 'lib/ood_core/clusters.rb', line 116 def [](id) @clusters.detect { |cluster| cluster == id } end |
#each {|cluster| ... } ⇒ Object
For a block {|cluster| …}
122 123 124 |
# File 'lib/ood_core/clusters.rb', line 122 def each(&block) @clusters.each(&block) end |