Class: Hospital::Diagnosis

Inherits:
Object
  • Object
show all
Defined in:
lib/hospital/diagnosis.rb

Defined Under Namespace

Classes: Error, Info, Result, Skip, Warning

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Diagnosis

Returns a new instance of Diagnosis.



8
9
10
11
# File 'lib/hospital/diagnosis.rb', line 8

def initialize name
  @name = name.to_s
  reset
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



6
7
8
# File 'lib/hospital/diagnosis.rb', line 6

def errors
  @errors
end

#infosObject (readonly)

Returns the value of attribute infos.



6
7
8
# File 'lib/hospital/diagnosis.rb', line 6

def infos
  @infos
end

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/hospital/diagnosis.rb', line 6

def name
  @name
end

#resultsObject (readonly)

Returns the value of attribute results.



6
7
8
# File 'lib/hospital/diagnosis.rb', line 6

def results
  @results
end

#skipsObject (readonly)

Returns the value of attribute skips.



6
7
8
# File 'lib/hospital/diagnosis.rb', line 6

def skips
  @skips
end

#warningsObject (readonly)

Returns the value of attribute warnings.



6
7
8
# File 'lib/hospital/diagnosis.rb', line 6

def warnings
  @warnings
end

Instance Method Details

#add_error(message) ⇒ Object



103
104
105
106
107
# File 'lib/hospital/diagnosis.rb', line 103

def add_error message
  error = Error.new message
  @errors   << error
  @results  << error
end

#add_info(message) ⇒ Object



85
86
87
88
89
# File 'lib/hospital/diagnosis.rb', line 85

def add_info message
  info = Info.new message
  @infos    << info
  @results  << info
end

#add_skip(message) ⇒ Object



97
98
99
100
101
# File 'lib/hospital/diagnosis.rb', line 97

def add_skip message
  skip = Skip.new message
  @skips    << skip
  @results  << skip
end

#add_warning(message) ⇒ Object



91
92
93
94
95
# File 'lib/hospital/diagnosis.rb', line 91

def add_warning message
  warning = Warning.new message
  @warnings << warning
  @results  << warning
end

#dump_required_env_varsObject



35
36
37
38
39
40
41
# File 'lib/hospital/diagnosis.rb', line 35

def dump_required_env_vars
  @required_env_vars.map do |name|
    value = ENV[name]
    value = hide_value value if name =~ /(secret|password|key)/i
    "'#{name}': #{value}"
  end.join(",\n")
end

#hide_value(value) ⇒ Object



47
48
49
50
51
# File 'lib/hospital/diagnosis.rb', line 47

def hide_value value
  "#{value}"
    .gsub(/(?<=.{1}).(?=.{2})/, '*')
    .gsub(/\*{10,}/, '*****...*****')
end

#on_success_message(message) ⇒ Object



119
120
121
122
123
# File 'lib/hospital/diagnosis.rb', line 119

def on_success_message message
  if success?
    add_info message
  end
end

#put_results(out) ⇒ Object



109
110
111
112
113
# File 'lib/hospital/diagnosis.rb', line 109

def put_results out
  results.each do |result|
    result.put out
  end
end

#require_env_vars(env_vars = []) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/hospital/diagnosis.rb', line 23

def require_env_vars env_vars=[]
  success = true
  @required_env_vars = env_vars

  if (missing_vars = env_vars.select{|var| ENV[var].nil? || ENV[var].empty? }).any?
    add_error("These necessary ENV vars are not set: #{variable_list(missing_vars)}.")
    success = false
  else
    add_info("All necessary ENV vars are set.")
  end
end

#resetObject



13
14
15
16
17
18
19
20
21
# File 'lib/hospital/diagnosis.rb', line 13

def reset
  @infos              = []
  @warnings           = []
  @skips              = []
  @errors             = []
  @results            = []

  @required_env_vars  = []
end

#success?Boolean

Returns:

  • (Boolean)


115
116
117
# File 'lib/hospital/diagnosis.rb', line 115

def success?
  errors.count == 0 && skips.count == 0
end

#variable_list(vars) ⇒ Object



43
44
45
# File 'lib/hospital/diagnosis.rb', line 43

def variable_list vars
  "[#{vars.map{|v| "'#{v}'"}.join(', ')}]"
end