hospital 0.6.0 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3dda421b0e01757d0328884312ef0eb7f3e0f56c93a50ae928b64d2621796578
4
- data.tar.gz: f56bb16f35eef2b3211a47fefc2c514b17e0b894f9c6e483fcb0819e914d0b76
3
+ metadata.gz: c474d0a653f97f57a2e281a594c0e9b45d3e03bd17bd0b4ea80140e79b8d6572
4
+ data.tar.gz: 24c2232ea33222d4c470c2c736a6bf93745049d332fa07c44970c4f654bcc5eb
5
5
  SHA512:
6
- metadata.gz: 97048efd58fa3d4a758bffca77386b3dd5b94569b2eb953b5087410f5bee68b3fdef7b4cf70b5045e5e094e82e0833100825d88f363846c3b38229df39408fa0
7
- data.tar.gz: 4ce02585d62f4fa93d2a2cb03a22abc1be5aa545cbd3157226afcb20dd4ac390a813ad136c98e3608f33dc564d17fcc15fab3266240196da21956c98ac740727
6
+ metadata.gz: 05540eb19dbdd3277ef4d76bca4f8692e69c6eba340e0a8f91a8c554dd81bf654fa478c7f9396925eed5e2deca3ac28d75b872cec652b38128b84291a65d3b02
7
+ data.tar.gz: 0eb85a41d00fe122217b07eef2d6a69f5d8f6dadbbfcd95b21991d0e055c2be35e2aeec4c9d4f4a86d133d50b960f7697746a26cb08e7fb584389905491d5fb3
@@ -4,13 +4,13 @@ module Hospital
4
4
  class Checkup
5
5
  attr_reader :code, :condition, :diagnosis, :group, :skipped, :klass, :precondition
6
6
 
7
- def initialize klass, code, group: :general, title: nil, condition: -> { true }, precondition: false
7
+ def initialize klass, code, title: nil, condition: -> { true }, precondition: false
8
8
  @klass = klass
9
9
  @code = code
10
- @group = group
11
10
  @condition = condition
12
11
  @diagnosis = Hospital::Diagnosis.new([klass.to_s, title].compact.join(' - '))
13
12
  @precondition = precondition
13
+ @group = nil
14
14
  end
15
15
 
16
16
  def reset_diagnosis
@@ -35,5 +35,9 @@ module Hospital
35
35
  def success?
36
36
  diagnosis.success?
37
37
  end
38
+
39
+ def set_group group
40
+ @group = group
41
+ end
38
42
  end
39
43
  end
@@ -1,6 +1,6 @@
1
- require_relative "formatter"
1
+ require_relative "string_formatter"
2
2
 
3
- using Formatter
3
+ using StringFormatter
4
4
 
5
5
  module Hospital
6
6
  class CheckupGroup
@@ -18,10 +18,12 @@ module Hospital
18
18
  end
19
19
 
20
20
  def header
21
- "\n### #{name.to_s.capitalize.gsub(/_/, ' ')} checks".h1
21
+ "#{name.to_s.capitalize.gsub(/_/, ' ')} checks"
22
22
  end
23
23
 
24
24
  def add_checkup checkup
25
+ checkup.set_group self
26
+
25
27
  if checkup.precondition
26
28
  @precondition_checkups << checkup
27
29
  else
@@ -1,6 +1,6 @@
1
- require_relative "formatter"
1
+ require_relative "string_formatter"
2
2
 
3
- using Formatter
3
+ using StringFormatter
4
4
 
5
5
  class Hospital::Diagnosis
6
6
  attr_reader :infos, :warnings, :skips, :errors, :name, :results
@@ -49,8 +49,8 @@ class Hospital::Diagnosis
49
49
  "#{prefix} #{message.gsub(/\n\z/, '').gsub(/\n/, prefix ? "\n " : "\n")}"
50
50
  end
51
51
 
52
- def put
53
- puts output.indented
52
+ def put out
53
+ out.put_diagnosis_result output
54
54
  end
55
55
  end
56
56
 
@@ -94,8 +94,10 @@ class Hospital::Diagnosis
94
94
  @results << error
95
95
  end
96
96
 
97
- def put_results
98
- results.each &:put
97
+ def put_results out
98
+ results.each do |result|
99
+ result.put out
100
+ end
99
101
  end
100
102
 
101
103
  def success?
@@ -0,0 +1,11 @@
1
+ module Hospital
2
+ module Formatter
3
+ class Base
4
+ attr_reader :buffer
5
+
6
+ def initialize
7
+ @buffer = ""
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,31 @@
1
+ require_relative "base"
2
+
3
+ using StringFormatter
4
+
5
+ module Hospital
6
+ module Formatter
7
+ class Pre < Base
8
+ def put_group_header text
9
+ @buffer << "\n\n### #{text}"
10
+ end
11
+
12
+ def put_diagnosis_header text
13
+ @buffer << "\n\n## #{text}"
14
+ end
15
+
16
+ def put_summary errors_count, warnings_count
17
+ @buffer << <<~END
18
+ \n\n
19
+ #### Summary:
20
+ Errors: #{errors_count}
21
+ Warnings: #{warnings_count}
22
+ END
23
+ end
24
+
25
+ def put_diagnosis_result text
26
+ @buffer << "\n#{text}"
27
+ end
28
+
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,31 @@
1
+ require_relative "base"
2
+
3
+ using StringFormatter
4
+
5
+ module Hospital
6
+ module Formatter
7
+ class Shell < Base
8
+ def put_group_header text
9
+ @buffer << "\n### #{text}".h1
10
+ end
11
+
12
+ def put_diagnosis_header text
13
+ @buffer << "\n#{text.h2.indented}"
14
+ end
15
+
16
+ def put_summary errors_count, warnings_count
17
+ @buffer << <<~END
18
+
19
+ #{"Summary:".h1}
20
+ #{"Errors: #{errors_count}".red}
21
+ #{"Warnings: #{warnings_count}".yellow}
22
+ END
23
+ end
24
+
25
+ def put_diagnosis_result text
26
+ @buffer << "\n#{text.indented}"
27
+ end
28
+
29
+ end
30
+ end
31
+ end
@@ -1,4 +1,4 @@
1
- module Formatter
1
+ module StringFormatter
2
2
  refine String do
3
3
  def bold
4
4
  "\e[1m#{self}\e[0m"
@@ -2,6 +2,10 @@
2
2
 
3
3
  require_relative '../../hospital'
4
4
 
5
+ # usage:
6
+ # rake doctor
7
+ # rake doctor[true]
8
+
5
9
  desc 'Check system setup sanity.'
6
10
  task :doctor, [:verbose] => :environment do |t, args|
7
11
  verbose = args[:verbose] == "true"
@@ -13,5 +17,5 @@ task :doctor, [:verbose] => :environment do |t, args|
13
17
  end
14
18
 
15
19
  p "start checkup" if verbose
16
- Hospital.do_checkup_all verbose: verbose
20
+ puts Hospital::Runner.new(verbose: verbose).do_checkup_all
17
21
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Hospital
4
- VERSION = "0.6.0"
4
+ VERSION = "0.7.0"
5
5
  end
data/lib/hospital.rb CHANGED
@@ -1,13 +1,16 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "byebug"
4
+ require 'require_all'
4
5
  require_relative "hospital/version"
5
6
  require_relative "hospital/checkup"
6
7
  require_relative "hospital/checkup_group"
7
8
  require_relative "hospital/diagnosis"
8
- require_relative "hospital/formatter"
9
+ require_relative "hospital/string_formatter"
10
+ require_relative "hospital/formatter/shell"
11
+ require_relative "hospital/formatter/pre"
9
12
 
10
- using Formatter
13
+ using StringFormatter
11
14
 
12
15
  module Hospital
13
16
  require_relative 'railtie' if defined?(Rails)
@@ -22,37 +25,6 @@ module Hospital
22
25
  raise Hospital::Error.new("Cannot include Hospital, please extend instead.")
23
26
  end
24
27
 
25
- def do_checkup_all verbose: false
26
- errcount = 0
27
- warcount = 0
28
-
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
47
-
48
- puts <<~END
49
-
50
- #{"Summary:".h1}
51
- #{"Errors: #{errcount}".red}
52
- #{"Warnings: #{warcount}".yellow}
53
- END
54
- end
55
-
56
28
  # used to call the checkup for a specific class directly (in specs)
57
29
  def do_checkup(klass, verbose: false)
58
30
  @@groups.map(&:all_checkups).flatten.select{|cu| cu.klass == klass }.map do |cu|
@@ -67,6 +39,10 @@ module Hospital
67
39
  end
68
40
  checkup_group
69
41
  end
42
+
43
+ def groups
44
+ @@groups
45
+ end
70
46
  end
71
47
 
72
48
  def checkup if: -> { true }, group: :general, title: nil, precondition: false, &code
@@ -74,13 +50,55 @@ module Hospital
74
50
  checkup = Checkup.new(
75
51
  self,
76
52
  code,
77
- group: group,
78
53
  title: title,
79
54
  condition: binding.local_variable_get('if'),
80
55
  precondition: precondition
81
56
  )
82
57
 
83
- # p "adding #{checkup.inspect} to #{group}"
58
+ # p "adding #{checkup.inspect} to #{group_name}"
84
59
  checkup_group.add_checkup checkup
85
60
  end
61
+
62
+ class Runner
63
+ attr_reader :verbose
64
+
65
+ def initialize verbose: false, formatter: :shell
66
+ @verbose = verbose
67
+ @formatter = case formatter
68
+ when :pre then Formatter::Pre
69
+ else Formatter::Shell
70
+ end
71
+
72
+ @out = @formatter.new
73
+ # @out.put_group_header "using formatter #{@formatter}"
74
+ end
75
+
76
+ def do_checkup_all
77
+ errcount = 0
78
+ warcount = 0
79
+
80
+ Hospital.groups.each do |group|
81
+ @out.put_group_header group.header
82
+ group.run_checkups verbose: verbose
83
+
84
+ group.all_checkups.each do |checkup|
85
+ if diagnosis = checkup.diagnosis
86
+ errcount += diagnosis.errors.count
87
+ warcount += diagnosis.warnings.count
88
+
89
+ if !checkup.skipped && (!checkup.group.skipped || checkup.precondition)
90
+ @out.put_diagnosis_header "Checking #{diagnosis.name}:"
91
+ diagnosis.put_results @out
92
+ elsif verbose
93
+ @out.put_diagnosis_header "Skipped #{diagnosis.name}."
94
+ end
95
+ end
96
+ end
97
+ end
98
+
99
+ @out.put_summary errcount, warcount
100
+
101
+ @out.buffer
102
+ end
103
+ end
86
104
  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.6.0
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexander
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-01-28 00:00:00.000000000 Z
11
+ date: 2024-03-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: byebug
@@ -52,7 +52,10 @@ files:
52
52
  - lib/hospital/checkup.rb
53
53
  - lib/hospital/checkup_group.rb
54
54
  - lib/hospital/diagnosis.rb
55
- - lib/hospital/formatter.rb
55
+ - lib/hospital/formatter/base.rb
56
+ - lib/hospital/formatter/pre.rb
57
+ - lib/hospital/formatter/shell.rb
58
+ - lib/hospital/string_formatter.rb
56
59
  - lib/hospital/tasks/checkup.rake
57
60
  - lib/hospital/version.rb
58
61
  - lib/railtie.rb