diagnostics 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- diagnostics (0.0.1)
4
+ diagnostics (0.0.2)
5
5
 
6
6
  GEM
7
7
  remote: http://rubygems.org/
@@ -12,12 +12,16 @@ Feature: Diagnostic checks
12
12
  $:.unshift(File.dirname(__FILE__) + '/../../lib')
13
13
  require 'diagnostics'
14
14
 
15
- check = diagnostic_check :system do |c|
16
- c.passed { true }
17
- c.warning { false }
18
- c.failed { false }
15
+ class StatusMethods
16
+ include DiagnosticsCheck
17
+
18
+ def passed; true end
19
+ def warning; false end
20
+ def failed; false end
19
21
  end
20
22
 
23
+ check = diagnostic_check(:StatusMethods)
24
+
21
25
  puts check.passed?
22
26
  puts check.warning?
23
27
  puts check.failed?
@@ -39,13 +43,19 @@ Feature: Diagnostic checks
39
43
  $:.unshift(File.dirname(__FILE__) + '/../../lib')
40
44
  require 'diagnostics'
41
45
 
42
- check = diagnostic_check :system do |c|
43
- c.data do |d|
44
- d['Master'] = 'Up'
45
- d['Slave'] = 'Down'
46
+ class AttributesOnCheck
47
+ include DiagnosticsCheck
48
+
49
+ def data
50
+ lambda do |d|
51
+ d['Master'] = 'Up'
52
+ d['Slave'] = 'Down'
53
+ end
46
54
  end
47
55
  end
48
56
 
57
+ check = diagnostic_check(:AttributesOnCheck)
58
+
49
59
  puts check.data.attributes(:plain)
50
60
  puts
51
61
  puts check.data.attributes(:html)
@@ -74,13 +84,19 @@ Feature: Diagnostic checks
74
84
  $:.unshift(File.dirname(__FILE__) + '/../../lib')
75
85
  require 'diagnostics'
76
86
 
77
- check = diagnostic_check :system do |c|
78
- c.data do |d|
79
- d << ['a', 'b', 'c']
80
- d << [1, 2, 3]
87
+ class ListsOnCheck
88
+ include DiagnosticsCheck
89
+
90
+ def data
91
+ lambda do |d|
92
+ d << ['a', 'b', 'c']
93
+ d << [1, 2, 3]
94
+ end
81
95
  end
82
96
  end
83
97
 
98
+ check = diagnostic_check(:ListsOnCheck)
99
+
84
100
  puts check.data.lists(:plain)
85
101
  puts
86
102
  puts check.data.lists(:html)
@@ -106,13 +122,19 @@ Feature: Diagnostic checks
106
122
  $:.unshift(File.dirname(__FILE__) + '/../../lib')
107
123
  require 'diagnostics'
108
124
 
109
- check = diagnostic_check :system do |c|
110
- c.data do |d|
111
- d << 'a'
112
- d << 'b'
125
+ class TextsOnCheck
126
+ include DiagnosticsCheck
127
+
128
+ def data
129
+ lambda do |d|
130
+ d << 'a'
131
+ d << 'b'
132
+ end
113
133
  end
114
134
  end
115
135
 
136
+ check = diagnostic_check(:TextsOnCheck)
137
+
116
138
  puts check.data.texts(:plain)
117
139
  puts
118
140
  puts check.data.texts(:html)
@@ -134,13 +156,19 @@ Feature: Diagnostic checks
134
156
  $:.unshift(File.dirname(__FILE__) + '/../../lib')
135
157
  require 'diagnostics'
136
158
 
137
- check = diagnostic_check :system do |c|
138
- c.data do |d|
139
- d << { 'heading1' => [1,2,3],
140
- 'heading2' => [4,5,6] }
159
+ class TablesOnCheck
160
+ include DiagnosticsCheck
161
+
162
+ def data
163
+ lambda do |d|
164
+ d << { 'heading1' => [1,2,3],
165
+ 'heading2' => [4,5,6] }
166
+ end
141
167
  end
142
168
  end
143
169
 
170
+ check = diagnostic_check(:TablesOnCheck)
171
+
144
172
  print check.data.tables(:html)
145
173
  """
146
174
  When I run "ruby tables.rb"
@@ -1,44 +1,33 @@
1
1
  module Diagnostics
2
- class Check
2
+ class Check < Struct.new(:cls)
3
3
 
4
- def self.add(name, &block)
5
- Diagnostics.checks << check = new(name, &block)
4
+ def self.add(cls)
5
+ Diagnostics.checks << check = new(cls)
6
6
  check
7
7
  end
8
8
 
9
9
  def self.find(name)
10
- Diagnostics.checks.select {|c| c.name == name }[0]
10
+ Diagnostics.checks.select {|c| c.name == name.to_s }[0]
11
11
  end
12
12
 
13
- attr_reader :name
14
-
15
- def initialize(name, &block)
16
- @name = name
17
- instance_eval(&block) if block
18
- end
19
-
20
- def passed(&block)
21
- @passed_block = block
22
- end
23
-
24
- def warning(&block)
25
- @warning_block = block
13
+ def name
14
+ cls.name
26
15
  end
27
16
 
28
- def failed(&block)
29
- @failed_block = block
17
+ def instance
18
+ @instance ||= cls.new
30
19
  end
31
20
 
32
21
  def passed?
33
- @passed_block_result ||= call_block(@passed_block)
22
+ @passed_method_result ||= call_method(:passed)
34
23
  end
35
24
 
36
25
  def warning?
37
- @warning_block_result ||= call_block(@warning_block)
26
+ @warning_method_result ||= call_method(:warning)
38
27
  end
39
28
 
40
29
  def failed?
41
- @failed_block_result ||= call_block(@failed_block)
30
+ @failed_method_result ||= call_method(:failed)
42
31
  end
43
32
 
44
33
  def status
@@ -49,7 +38,11 @@ module Diagnostics
49
38
  end
50
39
 
51
40
  def data
52
- block_given? ? yield(data_group) : data_group
41
+ @data_method_result ||= if instance.respond_to?(:data)
42
+ instance.data.call(data_group) && data_group
43
+ else
44
+ data_group
45
+ end
53
46
  end
54
47
 
55
48
  private
@@ -58,8 +51,8 @@ module Diagnostics
58
51
  @data_group ||= DataGroup.new
59
52
  end
60
53
 
61
- def call_block(block)
62
- block ? block.call : false
54
+ def call_method(meth)
55
+ instance.respond_to?(meth) && instance.send(meth)
63
56
  end
64
57
 
65
58
  end
@@ -12,8 +12,8 @@ module Diagnostics
12
12
  Diagnostics.checks
13
13
  end
14
14
 
15
- def diagnostic_check(name, &block)
16
- block ? Check.add(name, &block) : Check.find(name)
15
+ def diagnostic_check(name)
16
+ Check.find(name)
17
17
  end
18
18
 
19
19
  def diagnostic_message
@@ -1,3 +1,3 @@
1
1
  module Diagnostics
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
data/lib/diagnostics.rb CHANGED
@@ -15,4 +15,12 @@ module Diagnostics
15
15
  @@checks = []
16
16
  def self.checks; @@checks end
17
17
 
18
+ end
19
+
20
+ module DiagnosticsCheck
21
+
22
+ def self.included(cls)
23
+ Diagnostics::Check.add(cls)
24
+ end
25
+
18
26
  end
@@ -3,27 +3,43 @@ require 'spec_helper'
3
3
  module Diagnostics
4
4
  describe Check do
5
5
 
6
+ before { class MyClass; end }
7
+
6
8
  describe '.add' do
7
9
  it 'adds a check' do
8
- described_class.add :foo
10
+ described_class.add MyClass
9
11
  Diagnostics.should have(1).checks
10
12
  end
11
13
  end
12
14
 
13
15
  describe '.find' do
14
- let!(:check) { described_class.add :foo }
16
+ let!(:check) { described_class.add MyClass }
15
17
 
16
18
  it 'finds the check by name' do
17
- described_class.find(:foo).should eq(check)
19
+ described_class.find('Diagnostics::MyClass').should eq(check)
20
+ end
21
+ end
22
+
23
+ describe '#name' do
24
+ it 'returns the diagnostic class name' do
25
+ described_class.new(MyClass).name.should eq('Diagnostics::MyClass')
26
+ end
27
+ end
28
+
29
+ describe '#instance' do
30
+ it 'returns an instance of the diagnostic class' do
31
+ described_class.new(MyClass).instance.should be_an_instance_of(MyClass)
18
32
  end
19
33
  end
20
34
 
21
35
  describe 'status predicate methods' do
22
- let(:check) do
23
- described_class.new(:foo) do |c|
24
- c.passed { true }
25
- c.warning { false }
26
- c.failed { false }
36
+ let(:check) { described_class.new(MyClass) }
37
+
38
+ before do
39
+ check.cls.class_eval do
40
+ def passed; true end
41
+ def warning; false end
42
+ def failed; false end
27
43
  end
28
44
  end
29
45
 
@@ -45,32 +61,23 @@ module Diagnostics
45
61
  context "with #{status}" do
46
62
  let(:check) { create_check(status) }
47
63
  specify { check.status.should eq(status) }
64
+ after { check.cls.send(:remove_method, status) }
48
65
  end
49
66
  end
50
67
 
51
- def create_check(status = nil)
52
- described_class.new(:foo) do |c|
53
- c.passed { status == :passed }
54
- c.warning { status == :warning }
55
- c.failed { status == :failed }
68
+ def create_check(status)
69
+ described_class.new(MyClass).tap do |check|
70
+ check.cls.class_eval do
71
+ define_method(status) { true }
72
+ end
56
73
  end
57
74
  end
58
75
  end
59
76
 
60
77
  describe '#data' do
61
- context 'given a block' do
62
- it 'yields a DataGroup instance' do
63
- described_class.new(:foo).data do |d|
64
- d.should be_an_instance_of(Diagnostics::DataGroup)
65
- end
66
- end
67
- end
68
-
69
- context 'given no block' do
70
- it 'returns a DataGroup instance' do
71
- data = described_class.new(:foo).data
72
- data.should be_an_instance_of(Diagnostics::DataGroup)
73
- end
78
+ it 'returns a DataGroup instance' do
79
+ data = described_class.new(MyClass).data
80
+ data.should be_an_instance_of(Diagnostics::DataGroup)
74
81
  end
75
82
  end
76
83
 
@@ -8,19 +8,9 @@ end
8
8
 
9
9
  describe '#diagnostic_check' do
10
10
 
11
- context 'with block given' do
12
- it 'adds a check' do
13
- diagnostic_check(:foo) { }
14
- diagnostic_checks.should have(1).check
15
- diagnostic_checks.first.name.should eq(:foo)
16
- end
17
- end
18
-
19
- context 'without block given' do
20
- it 'finds the check' do
21
- check = diagnostic_check(:foo) { }
22
- diagnostic_check(:foo).should eq(check)
23
- end
11
+ it 'finds the check' do
12
+ class MyClass; include(DiagnosticsCheck) end
13
+ diagnostic_check(:MyClass).cls.should eq(MyClass)
24
14
  end
25
15
 
26
16
  end
@@ -73,11 +63,13 @@ describe '#diagnostic_status' do
73
63
 
74
64
  context 'with all checks passing' do
75
65
  before do
76
- diagnostic_check(:foo) do |c|
77
- c.passed { true }
66
+ class MyPassingClass
67
+ include DiagnosticsCheck
68
+ def passed; true end
78
69
  end
79
- diagnostic_check(:bar) do |c|
80
- c.passed { true }
70
+ class MySecondPassingClass
71
+ include DiagnosticsCheck
72
+ def passed; true end
81
73
  end
82
74
  end
83
75
 
@@ -88,14 +80,17 @@ describe '#diagnostic_status' do
88
80
 
89
81
  context 'with a check that failed' do
90
82
  before do
91
- diagnostic_check(:foo) do |c|
92
- c.passed { true }
83
+ class MyPassingClass
84
+ include DiagnosticsCheck
85
+ def passed; true end
93
86
  end
94
- diagnostic_check(:zoo) do |c|
95
- c.warning { true }
87
+ class MyWarningClass
88
+ include DiagnosticsCheck
89
+ def warning; true end
96
90
  end
97
- diagnostic_check(:bar) do |c|
98
- c.failed { true }
91
+ class MyFailedClass
92
+ include DiagnosticsCheck
93
+ def failed; true end
99
94
  end
100
95
  end
101
96
 
@@ -106,11 +101,13 @@ describe '#diagnostic_status' do
106
101
 
107
102
  context 'with a warning check' do
108
103
  before do
109
- diagnostic_check(:foo) do |c|
110
- c.passed { true }
104
+ class MyPassingClass
105
+ include DiagnosticsCheck
106
+ def passed; true end
111
107
  end
112
- diagnostic_check(:bar) do |c|
113
- c.warning { true }
108
+ class MyWarningClass
109
+ include DiagnosticsCheck
110
+ def warning; true end
114
111
  end
115
112
  end
116
113
 
@@ -8,4 +8,17 @@ describe Diagnostics do
8
8
  end
9
9
  end
10
10
 
11
+ end
12
+
13
+ describe DiagnosticsCheck do
14
+
15
+ describe '.included' do
16
+ before { diagnostic_checks.should be_empty }
17
+
18
+ it 'adds the class to checks' do
19
+ class MyCheck; include DiagnosticsCheck end
20
+ diagnostic_checks.should have(1).check
21
+ end
22
+ end
23
+
11
24
  end
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ../..
3
3
  specs:
4
- diagnostics (0.0.1)
4
+ diagnostics (0.0.2)
5
5
 
6
6
  GEM
7
7
  remote: http://rubygems.org/
@@ -8,26 +8,26 @@ Feature: Diagnostics page
8
8
  Then I should be on the diagnostics page
9
9
 
10
10
  Scenario: A passing diagnostic check with data attributes
11
- Given a diagnostic check named "mysql" with the following statuses:
11
+ Given a diagnostic check named "MySQLCheck" with the following statuses:
12
12
  | passed | warning | failed |
13
13
  | true | false | false |
14
- And diagnostic check "mysql" has the following data attributes:
14
+ And diagnostic check "MySQLCheck" has the following data attributes:
15
15
  | a | b | c |
16
16
  | 1 | 2 | 3 |
17
17
  When I go to the diagnostics page
18
- Then I should see a passed diagnostic check named "mysql"
18
+ Then I should see a passed diagnostic check named "MySQLCheck"
19
19
  And should see "a: 1" within diagnostic data
20
20
  And should see "b: 2" within diagnostic data
21
21
  And should see "c: 3" within diagnostic data
22
22
  And should see "All Systems Operational" as the title
23
23
 
24
24
  Scenario: A failed diagnostic check with a data list
25
- Given a diagnostic check named "mysql" with the following statuses:
25
+ Given a diagnostic check named "MySQLCheck" with the following statuses:
26
26
  | passed | warning | failed |
27
27
  | false | false | true |
28
- And diagnostic check "mysql" has a data list: "a,b,c"
28
+ And diagnostic check "MySQLCheck" has a data list: "a,b,c"
29
29
  When I go to the diagnostics page
30
- Then I should see a failed diagnostic check named "mysql"
30
+ Then I should see a failed diagnostic check named "MySQLCheck"
31
31
  And should see "a" within diagnostic data
32
32
  And should see "b" within diagnostic data
33
33
  And should see "c" within diagnostic data
@@ -11,33 +11,33 @@ Feature: Status page
11
11
  Then I should be on the status page
12
12
 
13
13
  Scenario: A simple passing diagnostic check
14
- Given a diagnostic check named "mysql" with the following statuses:
14
+ Given a diagnostic check named "MySQLCheck" with the following statuses:
15
15
  | passed | warning | failed |
16
16
  | true | false | false |
17
17
  When I go to the status page
18
- Then I should see a passed diagnostic check named "mysql"
18
+ Then I should see a passed diagnostic check named "MySQLCheck"
19
19
  And should see "All Systems Operational" as the title
20
20
 
21
21
  Scenario: Diagnostic checks that contain one warning check
22
- Given a diagnostic check named "mysql" with the following statuses:
22
+ Given a diagnostic check named "MySQLCheck" with the following statuses:
23
23
  | passed | warning | failed |
24
24
  | true | false | false |
25
- Given a diagnostic check named "postgresql" with the following statuses:
25
+ Given a diagnostic check named "PostgreSQLCheck" with the following statuses:
26
26
  | passed | warning | failed |
27
27
  | false | true | false |
28
28
  When I go to the status page
29
- Then I should see a passed diagnostic check named "mysql"
30
- And should see a warning diagnostic check named "postgresql"
29
+ Then I should see a passed diagnostic check named "MySQLCheck"
30
+ And should see a warning diagnostic check named "PostgreSQLCheck"
31
31
  And should see "Experiencing Issues" as the title
32
32
 
33
33
  Scenario: Diagnostic checks that contain one failing check
34
- Given a diagnostic check named "mysql" with the following statuses:
34
+ Given a diagnostic check named "MySQLCheck" with the following statuses:
35
35
  | passed | warning | failed |
36
36
  | true | false | false |
37
- Given a diagnostic check named "postgresql" with the following statuses:
37
+ Given a diagnostic check named "PostgreSQLCheck" with the following statuses:
38
38
  | passed | warning | failed |
39
39
  | false | false | true |
40
40
  When I go to the status page
41
- Then I should see a passed diagnostic check named "mysql"
42
- And should see a failed diagnostic check named "postgresql"
41
+ Then I should see a passed diagnostic check named "MySQLCheck"
42
+ And should see a failed diagnostic check named "PostgreSQLCheck"
43
43
  And should see "System Failure" as the title
@@ -1,11 +1,15 @@
1
1
  Given /^a diagnostic check named "([^"]*)" with the following statuses:$/ do |name, table|
2
2
  statuses = table.hashes[0]
3
3
 
4
- diagnostic_check name do |c|
5
- c.passed { eval(statuses[:passed]) }
6
- c.warning { eval(statuses[:warning]) }
7
- c.failed { eval(statuses[:failed]) }
4
+ body = Class.new do
5
+ include DiagnosticsCheck
6
+ define_method(:passed) { eval(statuses[:passed]) }
7
+ define_method(:warning) { eval(statuses[:warning]) }
8
+ define_method(:failed) { eval(statuses[:failed]) }
8
9
  end
10
+
11
+ Object.send(:remove_const, name) if Object.const_defined?(name)
12
+ Object.const_set(name, body)
9
13
  end
10
14
 
11
15
  Given /^diagnostic check "([^"]*)" has the following data attributes:$/ do |name, table|
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: diagnostics
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 25
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 2
10
- version: 0.0.2
9
+ - 3
10
+ version: 0.0.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Justin Ko
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-01-13 00:00:00 -07:00
18
+ date: 2011-01-23 00:00:00 -07:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -216,7 +216,7 @@ rubyforge_project: diagnostics
216
216
  rubygems_version: 1.4.2
217
217
  signing_key:
218
218
  specification_version: 3
219
- summary: diagnostics-0.0.2
219
+ summary: diagnostics-0.0.3
220
220
  test_files:
221
221
  - features/diagnostic_checks.feature
222
222
  - features/step_definitions/aruba.rb