Class: MethodProfiler::Report
- Inherits:
-
Object
- Object
- MethodProfiler::Report
- Defined in:
- lib/method_profiler/report.rb
Overview
Sorts and displays data collected by a Profiler.
Constant Summary collapse
- HEADERS =
Report headers
{ method: "Method", min: "Min Time", max: "Max Time", average: "Average Time", total_time: "Total Time", total_calls: "Total Calls", }
- FIELDS =
Fields that can be passed to #sort_by.
HEADERS.keys
- DIRECTIONS =
Directions that can be passed to #order.
[:asc, :ascending, :desc, :descending]
Instance Method Summary collapse
-
#initialize(data, name) ⇒ Report
constructor
Initializes a new Report.
-
#order(direction) ⇒ Report
Changes the direction of the sort.
-
#sort_by(field) ⇒ Report
Sorts the report by the given field.
-
#to_a ⇒ Array
Sorts the data by the currently set criteria and returns an array of profiling results.
-
#to_s ⇒ String
Sorts the data by the currently set criteria and returns a pretty printed table as a string.
Constructor Details
#initialize(data, name) ⇒ Report
Initializes a new MethodProfiler::Report. Used to sort and display data collected by a Profiler.
29 30 31 32 33 34 |
# File 'lib/method_profiler/report.rb', line 29 def initialize(data, name) @data = data @name = name @sort_by = :average @order = :descending end |
Instance Method Details
#order(direction) ⇒ Report
Changes the direction of the sort. Defaults to ‘:descending`. Chainable with #sort_by.
53 54 55 56 57 58 59 60 |
# File 'lib/method_profiler/report.rb', line 53 def order(direction) direction = direction.to_sym direction = :descending unless DIRECTIONS.include?(direction) direction = :descending if direction == :desc direction = :ascending if direction == :asc @order = direction self end |
#sort_by(field) ⇒ Report
Sorts the report by the given field. Defaults to ‘:average`. Chainable with #order.
41 42 43 44 45 46 |
# File 'lib/method_profiler/report.rb', line 41 def sort_by(field) field = field.to_sym field = :average unless FIELDS.include?(field) @sort_by = field self end |
#to_a ⇒ Array
Sorts the data by the currently set criteria and returns an array of profiling results.
66 67 68 69 70 71 72 |
# File 'lib/method_profiler/report.rb', line 66 def to_a if @order == :ascending @data.sort { |a, b| a[@sort_by] <=> b[@sort_by] } else @data.sort { |a, b| b[@sort_by] <=> a[@sort_by] } end end |
#to_s ⇒ String
Sorts the data by the currently set criteria and returns a pretty printed table as a string.
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/method_profiler/report.rb', line 78 def to_s [ "MethodProfiler results for: #{@name}", Hirb::Helpers::Table.render( to_a, headers: HEADERS.dup, fields: FIELDS.dup, filters: { min: :to_milliseconds, max: :to_milliseconds, average: :to_milliseconds, total_time: :to_milliseconds, }, description: false ) ].join("\n") end |