Module: BlackStack::Deployer::NoMatchModule

Included in:
NoMatch
Defined in:
lib/blackstack-deployer.rb

Overview

define attributes and methods of a command’s nomatch

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#error_descriptionObject

Returns the value of attribute error_description.



478
479
480
# File 'lib/blackstack-deployer.rb', line 478

def error_description
  @error_description
end

#nomatchObject

Returns the value of attribute nomatch.



478
479
480
# File 'lib/blackstack-deployer.rb', line 478

def nomatch
  @nomatch
end

Class Method Details

.descriptor_errors(m) ⇒ Object



480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
# File 'lib/blackstack-deployer.rb', line 480

def self.descriptor_errors(m)
  errors = []
  # validate the nomatch is a regular expresion
  errors << "Each nomatch is not a regex nor a hash" unless m.is_a?(Hash) || m.is_a?(Regexp)
  # if m is a hash
  if m.is_a?(Hash)
    # validate: the hash m has a key :nomatch
    errors << "The hash descriptor of the nomatch does not have a key :nomatch" unless m.has_key?(:nomatch)
    # validate: the value of m[:nomatch] is a string
    errors << "The value of the key :nomatch is not a regex" unless m[:nomatch].is_a?(Regexp)
    # validate: the hash m has a key :error_description
    errors << "The hash descriptor of the nomatch does not have a key :error_description" unless m.has_key?(:error_description)
    # validate: the value of m[:error_description] is a string
    errors << "The value of the key :error_description is not a string" unless m[:error_description].is_a?(String)
  end # if m.is_a?(Hash)
  # 
  errors.uniq
end

Instance Method Details

#initialize(h) ⇒ Object

def self.descriptor_error(h)



499
500
501
502
503
504
# File 'lib/blackstack-deployer.rb', line 499

def initialize(h)
  errors = BlackStack::Deployer::NoMatchModule.descriptor_errors(h)
  raise "The node descriptor is not valid: #{errors.uniq.join(".\n")}" if errors.length > 0
  self.nomatch = h[:nomatch]
  self.error_description = h[:error_description]
end

#to_hashObject



506
507
508
509
510
511
# File 'lib/blackstack-deployer.rb', line 506

def to_hash
  h = {}
  h[:nomatch] = self.nomatch
  h[:error_description] = self.error_description
  h
end

#validate(output) ⇒ Object



513
514
515
516
517
518
519
520
521
# File 'lib/blackstack-deployer.rb', line 513

def validate(output)
  errors = []
  if !self.error_description.nil?
    errors << self.error_description if output.match(self.nomatch)
  else
    errors << "The output of the command matches the regular expression #{self.nomatch.inspect}" if output.match(self.nomatch)
  end
  errors
end