Class: StatModule::Stat

Inherits:
JSONable show all
Defined in:
lib/stat.rb

Constant Summary

Constants inherited from JSONable

JSONable::FORMATTING_BALL, JSONable::FORMATTING_CHECKMARK, JSONable::FORMATTING_STAR, JSONable::FORMATTING_WARNING

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from JSONable

from_json!

Constructor Details

#initialize(process, hash = nil) ⇒ Stat

Returns a new instance of Stat.

Raises:



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/stat.rb', line 18

def initialize(process, hash = nil)
  @finding_print_index = 0
  @findings = []

  if hash.is_a? Hash
    super(hash)
    return
  end

  raise TypeException unless process.is_a?(StatModule::Process)
  @statVersion = '1.0.0'
  @process = process
end

Instance Attribute Details

#statVersionObject (readonly)

Returns the value of attribute statVersion.



16
17
18
# File 'lib/stat.rb', line 16

def statVersion
  @statVersion
end

Instance Method Details

#findingsObject



41
42
43
# File 'lib/stat.rb', line 41

def findings
  @findings
end

#findings=(findings) ⇒ Object

Raises:



32
33
34
35
36
37
38
39
# File 'lib/stat.rb', line 32

def findings=(findings)
  raise TypeException unless findings.is_a?(Array)
  findings.each { |item|
    raise TypeException unless item.is_a?(StatModule::Finding)
    raise DuplicateElementException if @findings.include?(item)
    @findings.push(item)
  }
end


67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/stat.rb', line 67

def print_finding
  if @finding_print_index < @findings.length
    result = @findings[@finding_print_index].to_json
    result += ',' unless @finding_print_index >= @findings.length - 1
    puts(result)
    puts
    $stdout.flush
    @finding_print_index += 1
  else
    raise IndexOutOfBoundException
  end
end


80
81
82
83
84
85
# File 'lib/stat.rb', line 80

def print_footer
  @finding_print_index = 0
  puts ']}'
  puts
  $stdout.flush
end


54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/stat.rb', line 54

def print_header
  @finding_print_index = 0
  hash = {}
  hash['statVersion'] = @statVersion
  hash['process'] = @process.to_json
  hash['findings'] = []
  result = hash.to_json.to_s
  result = result[0..result.length - 3]
  puts(result)
  puts
  $stdout.flush
end

#processObject



50
51
52
# File 'lib/stat.rb', line 50

def process
  @process
end

#process=(process) ⇒ Object

Raises:



45
46
47
48
# File 'lib/stat.rb', line 45

def process=(process)
  raise TypeException unless process.is_a?(StatModule::Process)
  @process = process
end

#summary_print(formatted = false) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/stat.rb', line 91

def summary_print(formatted = false)
  errors = 0
  warnings = 0
  findings.each { |finding|
    if finding.failure
      errors += 1
    else
      warnings += 1
    end
  }
  if errors == 0 && warnings == 0
    result = "#{FORMATTING_CHECKMARK} PASSED with no warning".colorize(:green)
  elsif errors == 0
    result = "#{FORMATTING_WARNING} PASSED with #{warnings} warning".colorize(:yellow)
  elsif warnings == 0
    result = "#{FORMATTING_BALL} FAILED with #{errors} error".colorize(:red)
  else
    result = "#{FORMATTING_BALL} FAILED with #{errors} error and #{warnings} warning".colorize(:red)
  end
  if formatted
    result
  else
    result[result.index(' ') + 1..result.length]
  end
end

#to_json(options = {}) ⇒ Object



87
88
89
# File 'lib/stat.rb', line 87

def to_json(options = {})
  super(['finding_print_index'])
end