Sha256: 7bd78358d1aef41f7808d2bc1865ee24e3a1742928f890304bf1138c8e5445b5

Contents?: true

Size: 789 Bytes

Versions: 8

Compression:

Stored size: 789 Bytes

Contents

module LabTech
class Summary

  class Count
    attr_reader :name, :n, :total, :label

    def initialize(name, n, total, label = nil)
      @name  = name
      @n     = n
      @total = total
      @label = label || name.to_s
    end

    def zero?
      n.zero?
    end

    def to_s
      "%s of %s (%s) %s" % [
        humanize( n ),
        humanize( total ),
        rate( n ),
        label
      ]
    end

    private

    def humanize(n)
      width = number_helper.number_with_delimiter( n ).length
      "%#{width}s" % number_helper.number_with_delimiter( n )
    end

    def number_helper
      @_number_helper ||= Object.new.tap {|o| o.send :extend, ActionView::Helpers::NumberHelper }
    end

    def rate(n)
      "%2.2f%%" % ( 100.0 * n / total )
    end
  end

end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
lab_tech-0.1.9 app/models/lab_tech/summary/count.rb
lab_tech-0.1.8 app/models/lab_tech/summary/count.rb
lab_tech-0.1.7 app/models/lab_tech/summary/count.rb
lab_tech-0.1.6 app/models/lab_tech/summary/count.rb
lab_tech-0.1.5 app/models/lab_tech/summary/count.rb
lab_tech-0.1.4 app/models/lab_tech/summary/count.rb
lab_tech-0.1.3 app/models/lab_tech/summary/count.rb
lab_tech-0.1.2 app/models/lab_tech/summary/count.rb