hospital 0.5.0 → 0.6.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: 5c2d250697eb93225f8674b4374369688af80a6d32efcbfc70406ae2130d0bba
4
- data.tar.gz: bb30e0f93629c922d9723c943061e9dddad28fcab7406ab24496cd5f893a5b6d
3
+ metadata.gz: 3dda421b0e01757d0328884312ef0eb7f3e0f56c93a50ae928b64d2621796578
4
+ data.tar.gz: f56bb16f35eef2b3211a47fefc2c514b17e0b894f9c6e483fcb0819e914d0b76
5
5
  SHA512:
6
- metadata.gz: 6ca63659e83bd3a662612b41b2a645776ca06132241fd5033c169f8fb4be1547445fb34e2160731adced86f7ba6a8995f92a6d1fa573751002ce6d2c85869c70
7
- data.tar.gz: 91f8b05eddcbfc0fbbc1cb44b7d9a0c340312207267bd4589237480f9fd95a5473af493d0c274d4231c6aaa914fb2820b7e3985039b1ad089eb523764d5ce89c
6
+ metadata.gz: 97048efd58fa3d4a758bffca77386b3dd5b94569b2eb953b5087410f5bee68b3fdef7b4cf70b5045e5e094e82e0833100825d88f363846c3b38229df39408fa0
7
+ data.tar.gz: 4ce02585d62f4fa93d2a2cb03a22abc1be5aa545cbd3157226afcb20dd4ac390a813ad136c98e3608f33dc564d17fcc15fab3266240196da21956c98ac740727
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Hospital
4
+ class Checkup
5
+ attr_reader :code, :condition, :diagnosis, :group, :skipped, :klass, :precondition
6
+
7
+ def initialize klass, code, group: :general, title: nil, condition: -> { true }, precondition: false
8
+ @klass = klass
9
+ @code = code
10
+ @group = group
11
+ @condition = condition
12
+ @diagnosis = Hospital::Diagnosis.new([klass.to_s, title].compact.join(' - '))
13
+ @precondition = precondition
14
+ end
15
+
16
+ def reset_diagnosis
17
+ diagnosis.reset
18
+ end
19
+
20
+ def check verbose: false
21
+ diagnosis.reset
22
+
23
+ if condition.nil? || condition.call
24
+ @skipped = false
25
+ code.call(diagnosis)
26
+ diagnosis
27
+ else
28
+ @skipped = true
29
+ nil
30
+ end
31
+ rescue StandardError => e
32
+ diagnosis.add_error "Unrescued exception in #{klass}.checkup:\n#{e.full_message}.\nThis is a bug inside the checkup method that should be fixed!"
33
+ end
34
+
35
+ def success?
36
+ diagnosis.success?
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,53 @@
1
+ require_relative "formatter"
2
+
3
+ using Formatter
4
+
5
+ module Hospital
6
+ class CheckupGroup
7
+ attr_reader :name, :checkups, :precondition_checkups, :skipped
8
+
9
+ def initialize name
10
+ @name = name
11
+ @precondition_checkups = []
12
+ @checkups = []
13
+ @skipped = false
14
+ end
15
+
16
+ def all_checkups
17
+ @precondition_checkups + @checkups
18
+ end
19
+
20
+ def header
21
+ "\n### #{name.to_s.capitalize.gsub(/_/, ' ')} checks".h1
22
+ end
23
+
24
+ def add_checkup checkup
25
+ if checkup.precondition
26
+ @precondition_checkups << checkup
27
+ else
28
+ @checkups << checkup
29
+ end
30
+ end
31
+
32
+ def run_checkups verbose: false
33
+ run_precondition_checkups verbose: verbose
34
+
35
+ unless @skipped
36
+ run_dependent_checkups verbose: verbose
37
+ end
38
+ end
39
+
40
+ def run_precondition_checkups verbose: false
41
+ @precondition_checkups.each do |checkup|
42
+ checkup.check verbose: verbose
43
+ @skipped = true unless checkup.success?
44
+ end
45
+ end
46
+
47
+ def run_dependent_checkups verbose: false
48
+ @checkups.each do |checkup|
49
+ checkup.check verbose: verbose
50
+ end
51
+ end
52
+ end
53
+ end
@@ -1,5 +1,9 @@
1
- class Hospital::Diagnosis
2
- attr_reader :infos, :warnings, :errors, :name, :results
1
+ require_relative "formatter"
2
+
3
+ using Formatter
4
+
5
+ class Hospital::Diagnosis
6
+ attr_reader :infos, :warnings, :skips, :errors, :name, :results
3
7
 
4
8
  def initialize name
5
9
  @name = name.to_s
@@ -9,6 +13,7 @@ class Hospital::Diagnosis
9
13
  def reset
10
14
  @infos = []
11
15
  @warnings = []
16
+ @skips = []
12
17
  @errors = []
13
18
  @results = []
14
19
  end
@@ -44,8 +49,8 @@ class Hospital::Diagnosis
44
49
  "#{prefix} #{message.gsub(/\n\z/, '').gsub(/\n/, prefix ? "\n " : "\n")}"
45
50
  end
46
51
 
47
- def put
48
- puts output
52
+ def put
53
+ puts output.indented
49
54
  end
50
55
  end
51
56
 
@@ -54,11 +59,15 @@ class Hospital::Diagnosis
54
59
  end
55
60
 
56
61
  class Warning < Result
57
- def prefix; '🟠'; end
62
+ def prefix; '🟠' end
63
+ end
64
+
65
+ class Skip < Result
66
+ def prefix; '🟠' end
58
67
  end
59
68
 
60
69
  class Error < Result
61
- def prefix; '🔴'; end
70
+ def prefix; '🔴' end
62
71
  end
63
72
 
64
73
  def add_info message
@@ -73,6 +82,12 @@ class Hospital::Diagnosis
73
82
  @results << warning
74
83
  end
75
84
 
85
+ def add_skip message
86
+ skip = Skip.new message
87
+ @skips << skip
88
+ @results << skip
89
+ end
90
+
76
91
  def add_error message
77
92
  error = Error.new message
78
93
  @errors << error
@@ -82,4 +97,14 @@ class Hospital::Diagnosis
82
97
  def put_results
83
98
  results.each &:put
84
99
  end
100
+
101
+ def success?
102
+ errors.count == 0 && skips.count == 0
103
+ end
104
+
105
+ def on_success_message message
106
+ if success?
107
+ add_info message
108
+ end
109
+ end
85
110
  end
@@ -1,11 +1,19 @@
1
1
  module Formatter
2
2
  refine String do
3
+ def bold
4
+ "\e[1m#{self}\e[0m"
5
+ end
6
+
7
+ def underline
8
+ "\e[4m#{self}\e[0m"
9
+ end
10
+
3
11
  def h1
4
- "\n\e[4m\e[1m#{self}\e[0m"
12
+ "\n#{self}".underline.bold
5
13
  end
6
14
 
7
15
  def h2
8
- "\n\e[4m#{self}\e[0m"
16
+ "\n#{self}".underline
9
17
  end
10
18
 
11
19
  def red
@@ -15,5 +23,9 @@ module Formatter
15
23
  def yellow
16
24
  "\e[33m#{self}\e[0m"
17
25
  end
26
+
27
+ def indented
28
+ "#{self}"
29
+ end
18
30
  end
19
31
  end
@@ -4,15 +4,6 @@ require_relative '../../hospital'
4
4
 
5
5
  desc 'Check system setup sanity.'
6
6
  task :doctor, [:verbose] => :environment do |t, args|
7
- # at_exit { Rake::Task['doctor:summary'].invoke if $!.nil? }
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
7
  verbose = args[:verbose] == "true"
17
8
 
18
9
  # silence warnings about double constant definitions
@@ -20,7 +11,7 @@ task :doctor, [:verbose] => :environment do |t, args|
20
11
  p "eager load all classes" if verbose
21
12
  Rails.application.eager_load!
22
13
  end
23
-
14
+
24
15
  p "start checkup" if verbose
25
16
  Hospital.do_checkup_all verbose: verbose
26
17
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Hospital
4
- VERSION = "0.5.0"
4
+ VERSION = "0.6.0"
5
5
  end
data/lib/hospital.rb CHANGED
@@ -1,6 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "byebug"
3
4
  require_relative "hospital/version"
5
+ require_relative "hospital/checkup"
6
+ require_relative "hospital/checkup_group"
4
7
  require_relative "hospital/diagnosis"
5
8
  require_relative "hospital/formatter"
6
9
 
@@ -11,38 +14,7 @@ module Hospital
11
14
 
12
15
  class Error < StandardError; end
13
16
 
14
- class Checkup
15
- attr_reader :code, :condition, :diagnosis, :group, :skipped, :klass
16
-
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
24
-
25
- def reset_diagnosis
26
- diagnosis.reset
27
- end
28
-
29
- def check verbose: false
30
- diagnosis.reset
31
-
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
43
- end
44
-
45
- @@checkups = {}
17
+ @@groups = []
46
18
 
47
19
  class << self
48
20
 
@@ -50,40 +22,24 @@ module Hospital
50
22
  raise Hospital::Error.new("Cannot include Hospital, please extend instead.")
51
23
  end
52
24
 
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
25
  def do_checkup_all verbose: false
61
26
  errcount = 0
62
27
  warcount = 0
63
28
 
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
29
+ @@groups.each do |group|
30
+ puts group.header
31
+ group.run_checkups verbose: verbose
72
32
 
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|
33
+ group.all_checkups.each do |checkup|
78
34
  if diagnosis = checkup.diagnosis
79
35
  errcount += diagnosis.errors.count
80
36
  warcount += diagnosis.warnings.count
81
37
 
82
38
  if !checkup.skipped
83
- puts "Checking #{diagnosis.name}:".h2
39
+ puts "Checking #{diagnosis.name}:".h2.indented
84
40
  diagnosis.put_results
85
41
  elsif verbose
86
- puts "Skipped #{diagnosis.name}.".h2
42
+ puts "Skipped #{diagnosis.name}.".h2.indented
87
43
  end
88
44
  end
89
45
  end
@@ -98,17 +54,33 @@ module Hospital
98
54
  end
99
55
 
100
56
  # used to call the checkup for a specific class directly (in specs)
101
- def do_checkup(klass)
102
- @@checkups[klass].check
57
+ def do_checkup(klass, verbose: false)
58
+ @@groups.map(&:all_checkups).flatten.select{|cu| cu.klass == klass }.map do |cu|
59
+ cu.check verbose: verbose
60
+ end
103
61
  end
104
62
 
105
- def group_header group
106
- "### #{group.to_s.capitalize.gsub(/_/, ' ')} checks".h1
63
+ def find_or_create_checkup_group name
64
+ unless checkup_group = @@groups.detect{|g| g.name == name }
65
+ checkup_group = CheckupGroup.new name
66
+ @@groups << checkup_group
67
+ end
68
+ checkup_group
107
69
  end
108
70
  end
109
71
 
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')
72
+ def checkup if: -> { true }, group: :general, title: nil, precondition: false, &code
73
+ checkup_group = Hospital.find_or_create_checkup_group group
74
+ checkup = Checkup.new(
75
+ self,
76
+ code,
77
+ group: group,
78
+ title: title,
79
+ condition: binding.local_variable_get('if'),
80
+ precondition: precondition
81
+ )
82
+
83
+ # p "adding #{checkup.inspect} to #{group}"
84
+ checkup_group.add_checkup checkup
112
85
  end
113
-
114
86
  end
metadata CHANGED
@@ -1,15 +1,43 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hospital
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.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-26 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2024-01-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: byebug
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: require_all
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
13
41
  description: Imagine a team of little doctors creating diagnoses and creating a useful
14
42
  report.
15
43
  email:
@@ -21,6 +49,8 @@ files:
21
49
  - LICENSE.txt
22
50
  - README.md
23
51
  - lib/hospital.rb
52
+ - lib/hospital/checkup.rb
53
+ - lib/hospital/checkup_group.rb
24
54
  - lib/hospital/diagnosis.rb
25
55
  - lib/hospital/formatter.rb
26
56
  - lib/hospital/tasks/checkup.rake