Class: Ghaki::Stats::Base
- Inherits:
-
Object
- Object
- Ghaki::Stats::Base
- Defined in:
- lib/ghaki/stats/base.rb
Instance Attribute Summary collapse
-
#format ⇒ Object
Get the formatter for the statistical report.
-
#title ⇒ Object
Title of statistics output report.
Instance Method Summary collapse
-
#clear ⇒ Object
Clears statistic counts.
-
#decr(major, minor) ⇒ Object
Decrease stats count by 1.
-
#decr_by(major, minor, value) ⇒ Object
Decrease stats count by a given value.
-
#def_value(major, minor, value) ⇒ Object
Assign default stats count value if not already set.
-
#def_values(major, value, *minors) ⇒ Object
Assign multiple stats counts if not already set.
-
#def_zero(major, minor) ⇒ Object
Assign zeroe to a stats count if its not already set.
-
#def_zeros(major_to_minor) ⇒ Object
Assign zeroes to multiple stats counts if they’re not already set.
-
#dump(out_file, title = @title) ⇒ Object
Writes statistics report to specified output file.
-
#flush(out_file, title = @title) ⇒ Object
Writes statistics report to specified output file, and clears statistic counts.
-
#get(major, minor) ⇒ Object
Get current value of stats count.
-
#get!(major, minor) ⇒ Object
Get current value or throw exception.
-
#get?(major, minor, defval = nil) ⇒ Boolean
Get current value of stats count.
-
#has!(major, minor = nil) ⇒ Object
Throw exception if stat is mising.
-
#has?(major, minor = nil) ⇒ Boolean
Is stat present?.
-
#incr(major, minor) ⇒ Object
Increase stats count by 1.
-
#incr_by(major, minor, value) ⇒ Object
Increase stats count by a given value.
-
#initialize(opts = {}) ⇒ Base
constructor
format
-
Statistics report formatting object.
-
#lacks?(major, minor = nil) ⇒ Boolean
Is stat missing?.
-
#log_dump(logger, title = @title) ⇒ Object
Logger specific dump.
-
#log_flush(logger, title = @title) ⇒ Object
Logger specific flush.
-
#put(major, minor, value) ⇒ Object
Set stats count to a given value.
-
#set_gt(major, minor, value) ⇒ Object
Set stats value if given value is greater than current value.
-
#set_lt(major, minor, value) ⇒ Object
Set stats value if given value is less than current value.
Constructor Details
#initialize(opts = {}) ⇒ Base
format
-
Statistics report formatting object.
title
-
Title of statistics output report.
18 19 20 21 22 |
# File 'lib/ghaki/stats/base.rb', line 18 def initialize opts={} clear @format = opts[:format] @title = opts[:title] || '' end |
Instance Attribute Details
#format ⇒ Object
Get the formatter for the statistical report.
Note: Will generate default formatting object if not already set.
28 29 30 |
# File 'lib/ghaki/stats/base.rb', line 28 def format @format ||= self._format_default end |
#title ⇒ Object
Title of statistics output report.
10 11 12 |
# File 'lib/ghaki/stats/base.rb', line 10 def title @title end |
Instance Method Details
#clear ⇒ Object
Clears statistic counts.
176 177 178 |
# File 'lib/ghaki/stats/base.rb', line 176 def clear @stats = {} end |
#decr(major, minor) ⇒ Object
Decrease stats count by 1.
70 71 72 |
# File 'lib/ghaki/stats/base.rb', line 70 def decr major, minor decr_by major, minor, 1 end |
#decr_by(major, minor, value) ⇒ Object
Decrease stats count by a given value
83 84 85 86 |
# File 'lib/ghaki/stats/base.rb', line 83 def decr_by major, minor, value def_zero major, minor @stats[major][minor] -= value end |
#def_value(major, minor, value) ⇒ Object
Assign default stats count value if not already set.
57 58 59 60 |
# File 'lib/ghaki/stats/base.rb', line 57 def def_value major, minor, value @stats[major] ||= {} @stats[major][minor] = value unless @stats[major].has_key?(minor) end |
#def_values(major, value, *minors) ⇒ Object
Assign multiple stats counts if not already set.
51 52 53 |
# File 'lib/ghaki/stats/base.rb', line 51 def def_values major, value, *minors minors.each { |minor| def_value major, minor, value } end |
#def_zero(major, minor) ⇒ Object
Assign zeroe to a stats count if its not already set.
35 36 37 |
# File 'lib/ghaki/stats/base.rb', line 35 def def_zero major, minor def_value major, minor, 0 end |
#def_zeros(major_to_minor) ⇒ Object
Assign zeroes to multiple stats counts if they’re not already set.
41 42 43 44 45 46 47 |
# File 'lib/ghaki/stats/base.rb', line 41 def def_zeros major_to_minor major_to_minor.each_pair do |major,minor_s| [*minor_s].each do |minor| def_zero major, minor end end end |
#dump(out_file, title = @title) ⇒ Object
Writes statistics report to specified output file.
182 183 184 |
# File 'lib/ghaki/stats/base.rb', line 182 def dump out_file, title=@title format.dump @stats, out_file, title end |
#flush(out_file, title = @title) ⇒ Object
Writes statistics report to specified output file, and clears statistic counts.
194 195 196 197 198 |
# File 'lib/ghaki/stats/base.rb', line 194 def flush out_file, title=@title dump out_file, title ensure clear end |
#get(major, minor) ⇒ Object
Get current value of stats count. Defaults to zero when not present.
128 129 130 131 132 |
# File 'lib/ghaki/stats/base.rb', line 128 def get major, minor return 0 unless @stats.has_key?(major) return 0 unless @stats[major].has_key?(minor) return @stats[major][minor] end |
#get!(major, minor) ⇒ Object
Get current value or throw exception.
136 137 138 |
# File 'lib/ghaki/stats/base.rb', line 136 def get! major, minor has!(major,minor).get(major,minor) end |
#get?(major, minor, defval = nil) ⇒ Boolean
Get current value of stats count. Defaults to specified when not present.
142 143 144 145 146 147 148 |
# File 'lib/ghaki/stats/base.rb', line 142 def get? major, minor, defval=nil if has? major, minor get major, minor else defval end end |
#has!(major, minor = nil) ⇒ Object
Throw exception if stat is mising.
117 118 119 120 121 122 123 124 |
# File 'lib/ghaki/stats/base.rb', line 117 def has! major, minor=nil return self if has? major, minor if minor.nil? raise MissingMajorStatsError.new(major) else raise MissingMinorStatsError.new(major, minor) end end |
#has?(major, minor = nil) ⇒ Boolean
Is stat present?
97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/ghaki/stats/base.rb', line 97 def has? major, minor=nil if @stats.has_key?(major) if minor.nil? true else @stats[major].has_key?(minor) end else false end end |
#incr(major, minor) ⇒ Object
Increase stats count by 1.
64 65 66 |
# File 'lib/ghaki/stats/base.rb', line 64 def incr major, minor incr_by major, minor, 1 end |
#incr_by(major, minor, value) ⇒ Object
Increase stats count by a given value.
76 77 78 79 |
# File 'lib/ghaki/stats/base.rb', line 76 def incr_by major, minor, value def_zero major, minor @stats[major][minor] += value end |
#lacks?(major, minor = nil) ⇒ Boolean
Is stat missing?
111 112 113 |
# File 'lib/ghaki/stats/base.rb', line 111 def lacks? major, minor=nil ! has?(major,minor) end |
#log_dump(logger, title = @title) ⇒ Object
Logger specific dump. (DEPRECATED)
188 189 190 191 |
# File 'lib/ghaki/stats/base.rb', line 188 def log_dump logger, title=@title warn "[DEPRECATED] 'log_dump' is deprecated. Please use 'dump' instead." dump logger, title end |
#log_flush(logger, title = @title) ⇒ Object
Logger specific flush. (DEPRECATED)
202 203 204 205 |
# File 'lib/ghaki/stats/base.rb', line 202 def log_flush logger, title=@title warn "[DEPRECATED] 'log_flush' is deprecated. Please use 'flush' instead." flush logger, title end |
#put(major, minor, value) ⇒ Object
Set stats count to a given value.
90 91 92 93 |
# File 'lib/ghaki/stats/base.rb', line 90 def put major, minor, value @stats[major] ||= {} @stats[major][minor] = value end |
#set_gt(major, minor, value) ⇒ Object
Set stats value if given value is greater than current value.
152 153 154 155 156 157 158 159 160 |
# File 'lib/ghaki/stats/base.rb', line 152 def set_gt major, minor, value if not @stats.has_key?(major) @stats[major] = { minor => value } elsif not @stats[major].has_key?(minor) @stats[major][minor] = value elsif @stats[major][minor] < value @stats[major][minor] = value end end |
#set_lt(major, minor, value) ⇒ Object
Set stats value if given value is less than current value.
164 165 166 167 168 169 170 171 172 |
# File 'lib/ghaki/stats/base.rb', line 164 def set_lt major, minor, value if not @stats.has_key?(major) @stats[major] = { minor => value } elsif not @stats[major].has_key?(minor) @stats[major][minor] = value elsif @stats[major][minor] > value @stats[major][minor] = value end end |