class Enc::Node

Attributes

environment[R]
node[R]
role[R]

Public Class Methods

lookup(name, nodes) click to toggle source

Search through the files in the `nodes` array until we find one that matches the node `name`. Set `@found` to the node hash returned by `.search_nodes`.

@api public @param name [String] the name of the node to look for. @param nodes [Array] an array of node files to search.

# File lib/enc/node.rb, line 23
def self.lookup(name, nodes)
  found = nil
  nodes.each do |c|
    found = search(name, YAML.load_file(c))
    break if found
  end
  Node.new(found) if found
end
new(node) click to toggle source
# File lib/enc/node.rb, line 7
def initialize(node)
  @node = node
  @role = validate('role', String)
  @environment = validate('environment', String)
  @classes = validate('classes', Array)
  @parameters = validate('parameters', Hash)
  @node_hash = to_hash
end

Private Class Methods

Public Instance Methods

classes() click to toggle source

Return the classes array. Checks if a role class has been delcared via role. If there's a role add it to the classes array.

@api public @return [Array, nil] the array of classes, or nil

# File lib/enc/node.rb, line 45
def classes
  if role
    return [role] unless @classes
    @classes << role unless @classes.include?(role)
  end

  @classes
end
parameters() click to toggle source

Return the parameters hash. Checks if a role class has been delcared via role. If there's a role, format it correctly (drop the 'roles' class,

change the class separator

to / for filesystem use) add it to the

parameters hash as 'role' overriding anything else set there.

@api public @return [Hash, nil] the Hash of parameters, or nil

# File lib/enc/node.rb, line 61
def parameters
  if role
    role_parameter = role.gsub(/^roles::/, '').gsub(/::/, '/')
    @parameters = { 'role' => role_parameter } unless @parameters
    @parameters['role'] = role_parameter
  end

  @parameters
end
to_yaml() click to toggle source

Return the hash version of the node statement in YAML

@api public @return [String] the YAML representation of the node statement

# File lib/enc/node.rb, line 36
def to_yaml
  to_hash.to_yaml unless to_hash.empty?
end

Private Instance Methods

to_hash() click to toggle source

A hash representation of the node statement

@api private @return [Hash] the node statment in Hash form

# File lib/enc/node.rb, line 106
def to_hash
  node_hash = {}
  node_hash['environment'] = environment if environment
  node_hash['classes'] = classes if classes
  node_hash['parameters'] = parameters if parameters
  node_hash
end
validate(key, type) click to toggle source

Takes a key name, and data type and ensures that the data in nodes is the correct data type. If it's not the correct data type, we'll fail and print a message. If nodes is the correct data type and is not empty, return it, otherwise we return nil.

@api private @param key [String] a key name to search the nodes hash for @param type [Object] a data type to check for @return [Object, nil] returns the data of 'type' or nil if the value is empty

# File lib/enc/node.rb, line 96
def validate(key, type)
  return nil unless node && node.key?(key)
  fail "#{key.capitalize} must be a #{type}!" unless node[key].is_a?(type)
  return node[key] unless node[key].empty?
end