Class: OodCore::Clusters

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(clusters = []) ⇒ Clusters

Returns a new instance of Clusters.

Parameters:

  • clusters (Array<Cluster>) (defaults to: [])

    list of cluster objects



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

Parameters:

  • path (#to_s)

    configuration file or directory path

Returns:

  • (Clusters)

    the clusters parsed from config

Raises:



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).expand_path

  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.message.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.message.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

Parameters:

  • id (Object)

    id of cluster object

Returns:

  • (Cluster, nil)

    cluster object if found



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| …}

Yields:

  • (cluster)

    Gives the next cluster object in the list



122
123
124
# File 'lib/ood_core/clusters.rb', line 122

def each(&block)
  @clusters.each(&block)
end