Class: Rapid::Check

Inherits:
Object
  • Object
show all
Defined in:
lib/rapid/check.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(code, options = {}) ⇒ Check

Returns a new instance of Check.



8
9
10
11
12
13
14
15
# File 'lib/rapid/check.rb', line 8

def initialize code, options = {}
  @code = code
  @console = options[:console]
  @ok_count = 0
  @warning_count = 0
  @error_count = 0
  @sections = []
end

Class Attribute Details

.currentObject

Returns the value of attribute current.



135
136
137
# File 'lib/rapid/check.rb', line 135

def current
  @current
end

.fileObject

Returns the value of attribute file.



135
136
137
# File 'lib/rapid/check.rb', line 135

def file
  @file
end

Instance Attribute Details

#codeObject (readonly)

Returns the value of attribute code.



5
6
7
# File 'lib/rapid/check.rb', line 5

def code
  @code
end

#consoleObject

Returns the value of attribute console.



6
7
8
# File 'lib/rapid/check.rb', line 6

def console
  @console
end

#error_countObject (readonly)

Returns the value of attribute error_count.



5
6
7
# File 'lib/rapid/check.rb', line 5

def error_count
  @error_count
end

#ok_countObject (readonly)

Returns the value of attribute ok_count.



5
6
7
# File 'lib/rapid/check.rb', line 5

def ok_count
  @ok_count
end

#warning_countObject (readonly)

Returns the value of attribute warning_count.



5
6
7
# File 'lib/rapid/check.rb', line 5

def warning_count
  @warning_count
end

Class Method Details

.new_with_detection(*args) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/rapid/check.rb', line 145

def new_with_detection *args
  filename = file
  
  if filename.nil?
    possible_files.each do |name|
      if File.exists?(name) && !File.directory?(name)
        filename ||= name
      end
    end
  end
  
  unless filename
    raise FileNotFoundCheckError.new("could not locate your rapid.rb check file [#{Rapid::Check.possible_files.join(', ')}]. " +
            "Please create it or set the location using Rapid::Check.file = '/config/checks.rb'.")
  end
  
  code = File.open(filename) {|f| f.read }
  new code, *args
end

.possible_filesObject



137
138
139
140
141
142
143
# File 'lib/rapid/check.rb', line 137

def possible_files
  [
    '.rapid',
    'rapid.rb',
    File.join('config', 'rapid.rb')
  ]
end

Instance Method Details

#as_currentObject



87
88
89
90
91
92
93
# File 'lib/rapid/check.rb', line 87

def as_current
  self.class.current = self
  yield
  
ensure
  self.class.current = nil
end

#check_countObject



17
18
19
# File 'lib/rapid/check.rb', line 17

def check_count
  ok_count + warning_count + error_count
end

#encounters_error?Boolean

Returns:

  • (Boolean)


95
96
97
98
99
# File 'lib/rapid/check.rb', line 95

def encounters_error?
  old_count = self.error_count
  yield
  old_count != self.error_count
end

#error(filename, message) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/rapid/check.rb', line 41

def error filename, message
  @error_count += 1
  @valid = false
  log :error, "", ""
  log :error, filename, message
  log :error, "", ""
end

#exception(filename, e) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/rapid/check.rb', line 49

def exception filename, e
  @error_count += 1
  @valid = false
  
  log :error, "", ""
  log :error, filename, ""
  log :error, "", ""
  log :error, "  problem: ", "content doesn't match"
  log :error, "  after:   ", pretty_code(e.previous_content, "START OF FILE")
  log :error, "  expected:", pretty_code(e.expected_content, "END OF FILE")
  log :error, "  received:", pretty_code(e.content, "END OF FILE")
  log :error, "  current: ", clean_hash(e.result)
  log :error, "", ""
end

#log_value(log_type, key, value) ⇒ Object



64
65
66
67
68
69
70
71
72
73
# File 'lib/rapid/check.rb', line 64

def log_value log_type, key, value
  return unless @console
  
  # don't print out very large strings
  if value.is_a?(String) && value.length > 50
    value = value[0..23] + "..." + value[value.length - 23..-1]
  end
  
  log log_type, "  #{key}:", value.inspect
end

#ok(filename, message) ⇒ Object



31
32
33
34
# File 'lib/rapid/check.rb', line 31

def ok filename, message
  @ok_count += 1
  log :ok, filename, message
end

#valid?Boolean

Returns:

  • (Boolean)


21
22
23
24
25
26
27
28
29
# File 'lib/rapid/check.rb', line 21

def valid?
  @valid = true
  @ok_count = 0
  @warning_count = 0
  @error_count = 0
  
  as_current { eval code }
  @valid
end

#warning(filename, message) ⇒ Object



36
37
38
39
# File 'lib/rapid/check.rb', line 36

def warning filename, message
  @warning_count += 1
  log :warning, filename, message
end

#with_section(name) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/rapid/check.rb', line 75

def with_section name
  puts "" if @sections.empty? && @console
  
  indention = "  " * @sections.length
  puts "#{indention}#{name}" if @console
  
  @sections.push name
  yield
ensure
  @sections.pop
end