Class: OodCluster::Cluster

Inherits:
Object
  • Object
show all
Includes:
JsonSerializer
Defined in:
lib/ood_cluster/cluster.rb

Overview

An object that describes a given cluster of nodes used by an HPC center

Instance Method Summary collapse

Methods included from JsonSerializer

included, #to_json

Constructor Details

#initialize(servers: {}, hpc_cluster: true, **_) ⇒ Cluster

Returns a new instance of Cluster.

Parameters:

  • servers (Hash{#to_sym=>Server) (defaults to: {})

    ] hash of servers

  • hpc_cluster (Boolean) (defaults to: true)

    whether this is an hpc cluster



11
12
13
14
# File 'lib/ood_cluster/cluster.rb', line 11

def initialize(servers: {}, hpc_cluster: true, **_)
  @servers = servers.each_with_object({}) { |(k, v), h| h[k.to_sym] = v }
  @hpc_cluster = hpc_cluster
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *arguments, &block) ⇒ Object

Grab object from @servers hash or check if it exists

Parameters:

  • method_name

    the method name called

  • arguments

    the arguments to the call

  • block

    an optional block for the call



35
36
37
38
39
40
41
42
43
# File 'lib/ood_cluster/cluster.rb', line 35

def method_missing(method_name, *arguments, &block)
  if /^(.+)_server$/ =~ method_name.to_s
    @servers.fetch($1.to_sym, nil)
  elsif /^(.+)_server\?$/ =~ method_name.to_s
    @servers.has_key?($1.to_sym)
  else
    super
  end
end

Instance Method Details

#==(other) ⇒ Boolean

The comparison operator

Parameters:

  • other (#to_h)

    object to compare against

Returns:

  • (Boolean)

    whether objects are equivalent



55
56
57
# File 'lib/ood_cluster/cluster.rb', line 55

def ==(other)
  to_h == other.to_h
end

#eql?(other) ⇒ Boolean

Check whether objects are identical to each other

Parameters:

  • other (#to_h)

    object to compare against

Returns:

  • (Boolean)

    whether objects are identical



62
63
64
# File 'lib/ood_cluster/cluster.rb', line 62

def eql?(other)
  self.class == other.class && self == other
end

#hashFixnum

Generate a hash value for this object

Returns:

  • (Fixnum)

    hash value of object



68
69
70
# File 'lib/ood_cluster/cluster.rb', line 68

def hash
  [self.class, to_h].hash
end

#hpc_cluster?Boolean

Whether this is an hpc-style cluster (i.e., meant for heavy computation)

Returns:

  • (Boolean)

    whether this an hpc-style cluster



27
28
29
# File 'lib/ood_cluster/cluster.rb', line 27

def hpc_cluster?
  @hpc_cluster
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Check if method ends with custom *_server or *_server?

Parameters:

  • method_name

    the method name to check

Returns:

  • (Boolean)


48
49
50
# File 'lib/ood_cluster/cluster.rb', line 48

def respond_to_missing?(method_name, include_private = false)
  method_name.to_s.end_with?('_server', '_server?') || super
end

#to_hHash

Convert object to hash

Returns:

  • (Hash)

    the hash describing this object



18
19
20
21
22
23
# File 'lib/ood_cluster/cluster.rb', line 18

def to_h
  {
    servers: @servers,
    hpc_cluster: @hpc_cluster
  }
end