Class: CounterObject
- Inherits:
-
Object
- Object
- CounterObject
- Defined in:
- lib/helpers/counter-helpers.rb
Instance Attribute Summary collapse
-
#start_time ⇒ Object
readonly
Returns the value of attribute start_time.
-
#total ⇒ Object
readonly
Returns the value of attribute total.
Instance Method Summary collapse
- #[](thing) ⇒ Object
- #count(thing_to_count) ⇒ Object
- #each {|"*Total*", "*#{@total}*"| ... } ⇒ Object
- #empty? ⇒ Boolean
-
#initialize(show_top = 20) ⇒ CounterObject
constructor
A new instance of CounterObject.
Constructor Details
#initialize(show_top = 20) ⇒ CounterObject
Returns a new instance of CounterObject.
5 6 7 8 9 |
# File 'lib/helpers/counter-helpers.rb', line 5 def initialize( show_top = 20 ) @show_top = show_top @counts = {} @total, @start_time = 0, Time.now end |
Instance Attribute Details
#start_time ⇒ Object (readonly)
Returns the value of attribute start_time.
3 4 5 |
# File 'lib/helpers/counter-helpers.rb', line 3 def start_time @start_time end |
#total ⇒ Object (readonly)
Returns the value of attribute total.
3 4 5 |
# File 'lib/helpers/counter-helpers.rb', line 3 def total @total end |
Instance Method Details
#[](thing) ⇒ Object
30 31 32 |
# File 'lib/helpers/counter-helpers.rb', line 30 def [](thing) @counts[thing] end |
#count(thing_to_count) ⇒ Object
11 12 13 14 |
# File 'lib/helpers/counter-helpers.rb', line 11 def count( thing_to_count ) @counts[thing_to_count] = ( @counts[thing_to_count] || 0 ) + 1 @total += 1 end |
#each {|"*Total*", "*#{@total}*"| ... } ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/helpers/counter-helpers.rb', line 16 def each running_total, displayed = 0, 0 @counts.sort_by { |thing,count| count }.reverse_each do |thing,count| break if @show_top && ( displayed >= @show_top ) displayed += 1 running_total += count yield thing, count end if @show_top && (@show_top < @counts.size) yield "#{@counts.size - @show_top} others", @total-running_total end yield "*Total*", "*#{@total}*" end |
#empty? ⇒ Boolean
34 35 36 |
# File 'lib/helpers/counter-helpers.rb', line 34 def empty? @counts.empty? end |