hospital 0.4.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: 4fe2ccd9fe0117efeaf99d430e269bee6e11e27463161d7038f4550ad79dcabb
4
- data.tar.gz: 045b72881224032291189022ede2c9eec3aacc8edb52a688d05570e0ad54d871
3
+ metadata.gz: 3dda421b0e01757d0328884312ef0eb7f3e0f56c93a50ae928b64d2621796578
4
+ data.tar.gz: f56bb16f35eef2b3211a47fefc2c514b17e0b894f9c6e483fcb0819e914d0b76
5
5
  SHA512:
6
- metadata.gz: 7ead12b8b24f8aaae387ac81c400950f02d682260a136cecb5f97007e0607a89d55900ccd5e112f82d1137484295990042dfe0f98e91a570755b354ac08e2181
7
- data.tar.gz: a269f71f8736c77172da1798f6f78623dccbcb887167193b7b0c998611786df554f5a035e66f2acf2dd6b292b6647caf7d0c822d2fed6301d68c85788c4322c6
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,14 +1,19 @@
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
- @name = name.to_s
9
+ @name = name.to_s
6
10
  reset
7
11
  end
8
12
 
9
13
  def reset
10
14
  @infos = []
11
15
  @warnings = []
16
+ @skips = []
12
17
  @errors = []
13
18
  @results = []
14
19
  end
@@ -27,6 +32,12 @@ class Hospital::Diagnosis
27
32
  "[#{vars.map{|v| "'#{v}'"}.join(', ')}]"
28
33
  end
29
34
 
35
+ def hide_value value
36
+ "#{value}"
37
+ .gsub(/(?<=.{1}).(?=.{2})/, '*')
38
+ .gsub(/\*{10,}/, '*****...*****')
39
+ end
40
+
30
41
  class Result
31
42
  attr_reader :message, :prefix
32
43
 
@@ -38,8 +49,8 @@ class Hospital::Diagnosis
38
49
  "#{prefix} #{message.gsub(/\n\z/, '').gsub(/\n/, prefix ? "\n " : "\n")}"
39
50
  end
40
51
 
41
- def put
42
- puts output
52
+ def put
53
+ puts output.indented
43
54
  end
44
55
  end
45
56
 
@@ -48,11 +59,15 @@ class Hospital::Diagnosis
48
59
  end
49
60
 
50
61
  class Warning < Result
51
- def prefix; '🟠'; end
62
+ def prefix; '🟠' end
63
+ end
64
+
65
+ class Skip < Result
66
+ def prefix; '🟠' end
52
67
  end
53
68
 
54
69
  class Error < Result
55
- def prefix; '🔴'; end
70
+ def prefix; '🔴' end
56
71
  end
57
72
 
58
73
  def add_info message
@@ -67,6 +82,12 @@ class Hospital::Diagnosis
67
82
  @results << warning
68
83
  end
69
84
 
85
+ def add_skip message
86
+ skip = Skip.new message
87
+ @skips << skip
88
+ @results << skip
89
+ end
90
+
70
91
  def add_error message
71
92
  error = Error.new message
72
93
  @errors << error
@@ -74,15 +95,16 @@ class Hospital::Diagnosis
74
95
  end
75
96
 
76
97
  def put_results
77
- put_header "Checking #{name}:"
78
98
  results.each &:put
79
99
  end
80
100
 
81
- private
82
-
83
- def put_header message
84
- puts ''
85
- puts "### #{message}"
101
+ def success?
102
+ errors.count == 0 && skips.count == 0
86
103
  end
87
104
 
105
+ def on_success_message message
106
+ if success?
107
+ add_info message
108
+ end
109
+ end
88
110
  end
@@ -0,0 +1,31 @@
1
+ module Formatter
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
+
11
+ def h1
12
+ "\n#{self}".underline.bold
13
+ end
14
+
15
+ def h2
16
+ "\n#{self}".underline
17
+ end
18
+
19
+ def red
20
+ "\e[31m#{self}\e[0m"
21
+ end
22
+
23
+ def yellow
24
+ "\e[33m#{self}\e[0m"
25
+ end
26
+
27
+ def indented
28
+ "#{self}"
29
+ end
30
+ end
31
+ end
@@ -3,13 +3,15 @@
3
3
  require_relative '../../hospital'
4
4
 
5
5
  desc 'Check system setup sanity.'
6
- task :doctor => :environment do
7
- # at_exit { Rake::Task['doctor:summary'].invoke if $!.nil? }
6
+ task :doctor, [:verbose] => :environment do |t, args|
7
+ verbose = args[:verbose] == "true"
8
8
 
9
9
  # silence warnings about double constant definitions
10
10
  Kernel::silence_warnings do
11
+ p "eager load all classes" if verbose
11
12
  Rails.application.eager_load!
12
13
  end
13
14
 
14
- Hospital.checkup_all
15
+ p "start checkup" if verbose
16
+ Hospital.do_checkup_all verbose: verbose
15
17
  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.6.0"
5
5
  end
data/lib/hospital.rb CHANGED
@@ -1,50 +1,86 @@
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"
8
+ require_relative "hospital/formatter"
9
+
10
+ using Formatter
5
11
 
6
12
  module Hospital
7
13
  require_relative 'railtie' if defined?(Rails)
8
14
 
9
15
  class Error < StandardError; end
10
16
 
11
- @@checkups = {}
12
- @@diagnosises = {}
17
+ @@groups = []
13
18
 
14
- def self.included(base)
15
- raise Hospital::Error.new("Cannot include Hospital, please extend instead.")
16
- end
19
+ class << self
17
20
 
18
- def self.extended(base)
19
- @@checkups[base] = -> (diagnosis) do
20
- diagnosis.add_warning("#{base}: No checks defined! Please call checkup with a lambda.")
21
+ def included(klass)
22
+ raise Hospital::Error.new("Cannot include Hospital, please extend instead.")
21
23
  end
22
- @@diagnosises[base] = Hospital::Diagnosis.new(base)
23
- end
24
24
 
25
- def checkup &code
26
- @@checkups[self] = code
27
- end
25
+ def do_checkup_all verbose: false
26
+ errcount = 0
27
+ warcount = 0
28
28
 
29
- def self.checkup klass
30
- @@diagnosises[klass].reset
31
- @@checkups[klass].call(@@diagnosises[klass])
32
- @@diagnosises[klass]
33
- end
29
+ @@groups.each do |group|
30
+ puts group.header
31
+ group.run_checkups verbose: verbose
32
+
33
+ group.all_checkups.each do |checkup|
34
+ if diagnosis = checkup.diagnosis
35
+ errcount += diagnosis.errors.count
36
+ warcount += diagnosis.warnings.count
37
+
38
+ if !checkup.skipped
39
+ puts "Checking #{diagnosis.name}:".h2.indented
40
+ diagnosis.put_results
41
+ elsif verbose
42
+ puts "Skipped #{diagnosis.name}.".h2.indented
43
+ end
44
+ end
45
+ end
46
+ end
34
47
 
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
48
+ puts <<~END
49
+
50
+ #{"Summary:".h1}
51
+ #{"Errors: #{errcount}".red}
52
+ #{"Warnings: #{warcount}".yellow}
53
+ END
54
+ end
55
+
56
+ # used to call the checkup for a specific class directly (in specs)
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
42
61
  end
43
62
 
44
- puts <<~END
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
69
+ end
70
+ end
71
+
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
+ )
45
82
 
46
- Summary:
47
- Errors: #{errcount}
48
- END
83
+ # p "adding #{checkup.inspect} to #{group}"
84
+ checkup_group.add_checkup checkup
49
85
  end
50
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.4.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-21 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,7 +49,10 @@ 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
55
+ - lib/hospital/formatter.rb
25
56
  - lib/hospital/tasks/checkup.rake
26
57
  - lib/hospital/version.rb
27
58
  - lib/railtie.rb