hospital 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4fe2ccd9fe0117efeaf99d430e269bee6e11e27463161d7038f4550ad79dcabb
4
- data.tar.gz: 045b72881224032291189022ede2c9eec3aacc8edb52a688d05570e0ad54d871
3
+ metadata.gz: 5c2d250697eb93225f8674b4374369688af80a6d32efcbfc70406ae2130d0bba
4
+ data.tar.gz: bb30e0f93629c922d9723c943061e9dddad28fcab7406ab24496cd5f893a5b6d
5
5
  SHA512:
6
- metadata.gz: 7ead12b8b24f8aaae387ac81c400950f02d682260a136cecb5f97007e0607a89d55900ccd5e112f82d1137484295990042dfe0f98e91a570755b354ac08e2181
7
- data.tar.gz: a269f71f8736c77172da1798f6f78623dccbcb887167193b7b0c998611786df554f5a035e66f2acf2dd6b292b6647caf7d0c822d2fed6301d68c85788c4322c6
6
+ metadata.gz: 6ca63659e83bd3a662612b41b2a645776ca06132241fd5033c169f8fb4be1547445fb34e2160731adced86f7ba6a8995f92a6d1fa573751002ce6d2c85869c70
7
+ data.tar.gz: 91f8b05eddcbfc0fbbc1cb44b7d9a0c340312207267bd4589237480f9fd95a5473af493d0c274d4231c6aaa914fb2820b7e3985039b1ad089eb523764d5ce89c
@@ -2,7 +2,7 @@ class Hospital::Diagnosis
2
2
  attr_reader :infos, :warnings, :errors, :name, :results
3
3
 
4
4
  def initialize name
5
- @name = name.to_s
5
+ @name = name.to_s
6
6
  reset
7
7
  end
8
8
 
@@ -27,6 +27,12 @@ class Hospital::Diagnosis
27
27
  "[#{vars.map{|v| "'#{v}'"}.join(', ')}]"
28
28
  end
29
29
 
30
+ def hide_value value
31
+ "#{value}"
32
+ .gsub(/(?<=.{1}).(?=.{2})/, '*')
33
+ .gsub(/\*{10,}/, '*****...*****')
34
+ end
35
+
30
36
  class Result
31
37
  attr_reader :message, :prefix
32
38
 
@@ -74,15 +80,6 @@ class Hospital::Diagnosis
74
80
  end
75
81
 
76
82
  def put_results
77
- put_header "Checking #{name}:"
78
83
  results.each &:put
79
84
  end
80
-
81
- private
82
-
83
- def put_header message
84
- puts ''
85
- puts "### #{message}"
86
- end
87
-
88
85
  end
@@ -0,0 +1,19 @@
1
+ module Formatter
2
+ refine String do
3
+ def h1
4
+ "\n\e[4m\e[1m#{self}\e[0m"
5
+ end
6
+
7
+ def h2
8
+ "\n\e[4m#{self}\e[0m"
9
+ end
10
+
11
+ def red
12
+ "\e[31m#{self}\e[0m"
13
+ end
14
+
15
+ def yellow
16
+ "\e[33m#{self}\e[0m"
17
+ end
18
+ end
19
+ end
@@ -3,13 +3,24 @@
3
3
  require_relative '../../hospital'
4
4
 
5
5
  desc 'Check system setup sanity.'
6
- task :doctor => :environment do
6
+ task :doctor, [:verbose] => :environment do |t, args|
7
7
  # at_exit { Rake::Task['doctor:summary'].invoke if $!.nil? }
8
8
 
9
+ ActiveRecord::Base.connection_pool.disconnect!
10
+ ActiveSupport.on_load(:active_record) do
11
+ config = ActiveRecord::Base.configurations[Rails.env]
12
+ config['pool'] = 100
13
+ ActiveRecord::Base.establish_connection(config)
14
+ end
15
+
16
+ verbose = args[:verbose] == "true"
17
+
9
18
  # silence warnings about double constant definitions
10
19
  Kernel::silence_warnings do
20
+ p "eager load all classes" if verbose
11
21
  Rails.application.eager_load!
12
22
  end
13
-
14
- Hospital.checkup_all
23
+
24
+ p "start checkup" if verbose
25
+ Hospital.do_checkup_all verbose: verbose
15
26
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Hospital
4
- VERSION = "0.4.0"
4
+ VERSION = "0.5.0"
5
5
  end
data/lib/hospital.rb CHANGED
@@ -2,49 +2,113 @@
2
2
 
3
3
  require_relative "hospital/version"
4
4
  require_relative "hospital/diagnosis"
5
+ require_relative "hospital/formatter"
6
+
7
+ using Formatter
5
8
 
6
9
  module Hospital
7
10
  require_relative 'railtie' if defined?(Rails)
8
11
 
9
12
  class Error < StandardError; end
10
13
 
11
- @@checkups = {}
12
- @@diagnosises = {}
14
+ class Checkup
15
+ attr_reader :code, :condition, :diagnosis, :group, :skipped, :klass
13
16
 
14
- def self.included(base)
15
- raise Hospital::Error.new("Cannot include Hospital, please extend instead.")
16
- end
17
+ def initialize klass, code, group: :general, title: nil, condition: -> { true }
18
+ @klass = klass
19
+ @code = code
20
+ @group = group
21
+ @condition = condition
22
+ @diagnosis = Hospital::Diagnosis.new([klass.to_s, title].compact.join(' - '))
23
+ end
17
24
 
18
- def self.extended(base)
19
- @@checkups[base] = -> (diagnosis) do
20
- diagnosis.add_warning("#{base}: No checks defined! Please call checkup with a lambda.")
25
+ def reset_diagnosis
26
+ diagnosis.reset
21
27
  end
22
- @@diagnosises[base] = Hospital::Diagnosis.new(base)
23
- end
24
28
 
25
- def checkup &code
26
- @@checkups[self] = code
27
- end
29
+ def check verbose: false
30
+ diagnosis.reset
28
31
 
29
- def self.checkup klass
30
- @@diagnosises[klass].reset
31
- @@checkups[klass].call(@@diagnosises[klass])
32
- @@diagnosises[klass]
32
+ if condition.nil? || condition.call
33
+ @skipped = false
34
+ code.call(diagnosis)
35
+ diagnosis
36
+ else
37
+ @skipped = true
38
+ nil
39
+ end
40
+ rescue StandardError => e
41
+ diagnosis.add_error "Unrescued exception in #{klass}.checkup:\n#{e.inspect}.\nThis is a bug inside the checkup method that should be fixed!"
42
+ end
33
43
  end
34
44
 
35
- def self.checkup_all
36
- errcount = 0
37
- @@checkups.keys.each do |klass|
38
- checkup(klass)
39
- diagnosis = @@diagnosises[klass]
40
- diagnosis.put_results
41
- errcount += diagnosis.errors.count
45
+ @@checkups = {}
46
+
47
+ class << self
48
+
49
+ def included(klass)
50
+ raise Hospital::Error.new("Cannot include Hospital, please extend instead.")
51
+ end
52
+
53
+ def extended(klass)
54
+ # only relevant if the class does not yet define a real checkup method
55
+ @@checkups[klass] = Checkup.new klass, -> (diagnosis) do
56
+ diagnosis.add_warning("#{klass}: No checks defined! Please call checkup with a lambda.")
57
+ end, group: :general
58
+ end
59
+
60
+ def do_checkup_all verbose: false
61
+ errcount = 0
62
+ warcount = 0
63
+
64
+ threads = @@checkups.map do |klass, checkup|
65
+ Thread.new do
66
+ Thread.current.report_on_exception = false
67
+ checkup.check(verbose: verbose)
68
+ end
69
+ end
70
+
71
+ threads.each &:join
72
+
73
+ @@checkups.group_by{|klass, checkup| checkup.group}.map do |group, checkups|
74
+ puts group_header(group)
75
+ first = false
76
+
77
+ checkups.each do |klass, checkup|
78
+ if diagnosis = checkup.diagnosis
79
+ errcount += diagnosis.errors.count
80
+ warcount += diagnosis.warnings.count
81
+
82
+ if !checkup.skipped
83
+ puts "Checking #{diagnosis.name}:".h2
84
+ diagnosis.put_results
85
+ elsif verbose
86
+ puts "Skipped #{diagnosis.name}.".h2
87
+ end
88
+ end
89
+ end
90
+ end
91
+
92
+ puts <<~END
93
+
94
+ #{"Summary:".h1}
95
+ #{"Errors: #{errcount}".red}
96
+ #{"Warnings: #{warcount}".yellow}
97
+ END
98
+ end
99
+
100
+ # used to call the checkup for a specific class directly (in specs)
101
+ def do_checkup(klass)
102
+ @@checkups[klass].check
42
103
  end
43
104
 
44
- puts <<~END
105
+ def group_header group
106
+ "### #{group.to_s.capitalize.gsub(/_/, ' ')} checks".h1
107
+ end
108
+ end
45
109
 
46
- Summary:
47
- Errors: #{errcount}
48
- END
110
+ def checkup if: -> { true }, group: :general, title: nil, &code
111
+ @@checkups[self] = Checkup.new self, code, group: group, title: title, condition: binding.local_variable_get('if')
49
112
  end
113
+
50
114
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hospital
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexander
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-12-21 00:00:00.000000000 Z
11
+ date: 2023-12-26 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Imagine a team of little doctors creating diagnoses and creating a useful
14
14
  report.
@@ -22,6 +22,7 @@ files:
22
22
  - README.md
23
23
  - lib/hospital.rb
24
24
  - lib/hospital/diagnosis.rb
25
+ - lib/hospital/formatter.rb
25
26
  - lib/hospital/tasks/checkup.rake
26
27
  - lib/hospital/version.rb
27
28
  - lib/railtie.rb