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


97
98
99
# File 'lib/ood_core/clusters.rb', line 97

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
# File 'lib/ood_core/clusters.rb', line 17

def load_file(path)
  config = Pathname.new(path.to_s).expand_path

  clusters = []
  if config.file?
    CONFIG_VERSION.any? do |version|
      YAML.safe_load(config.read).fetch(version, {}).each do |k, v|
        clusters << Cluster.new(send("parse_#{version}", id: k, cluster: v))
      end
      !clusters.empty?
    end
  elsif config.directory?
    Pathname.glob(config.join("*.yml")).each do |p|
      CONFIG_VERSION.any? do |version|
        if cluster = YAML.safe_load(p.read).fetch(version, nil)
          clusters << Cluster.new(send("parse_#{version}", id: p.basename(".yml").to_s, cluster: cluster))
          true
        else
          false
        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


104
105
106
# File 'lib/ood_core/clusters.rb', line 104

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


110
111
112
# File 'lib/ood_core/clusters.rb', line 110

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