Class: ReportFormatter
- Inherits:
-
Object
- Object
- ReportFormatter
- Defined in:
- app/facades/report_formatter.rb
Constant Summary collapse
- STAT_FUNCTIONS =
{ sum: ->(arr){arr.sum}, min: ->(arr){arr.min}, max: ->(arr){arr.max}, average: ->(arr){arr.average} }
- STAT_FORMATS =
{ number: "%g", date: "%m-%d-%y", time: "%I:%M:%S %P", datetime: "%m-%d-%y %I:%M:%S %P" }
Instance Method Summary collapse
- #calculate_stats(stat_function) ⇒ Object
- #format_stats(stat) ⇒ Object
- #format_time_stat(stat) ⇒ Object
-
#initialize(question, responses) ⇒ ReportFormatter
constructor
A new instance of ReportFormatter.
- #stats(stat_function) ⇒ Object
Constructor Details
#initialize(question, responses) ⇒ ReportFormatter
Returns a new instance of ReportFormatter.
17 18 19 20 |
# File 'app/facades/report_formatter.rb', line 17 def initialize(question, responses) @question = question @responses = responses end |
Instance Method Details
#calculate_stats(stat_function) ⇒ Object
27 28 29 30 |
# File 'app/facades/report_formatter.rb', line 27 def calculate_stats(stat_function) arr = @responses.where(:question_id => @question.id).map{|r| r.response_value.to_f} STAT_FUNCTIONS[stat_function].call(arr) end |
#format_stats(stat) ⇒ Object
32 33 34 35 36 37 38 39 40 |
# File 'app/facades/report_formatter.rb', line 32 def format_stats(stat) if @question.question_type_id == :number STAT_FORMATS[@question.question_type_id] % stat.to_f elsif [:date, :datetime, :time].include? @question.question_type_id format_time_stat(stat.to_f) else stat end end |
#format_time_stat(stat) ⇒ Object
42 43 44 45 |
# File 'app/facades/report_formatter.rb', line 42 def format_time_stat(stat) stat = Time.zone.at(stat) stat.strftime(STAT_FORMATS[@question.question_type_id]) end |
#stats(stat_function) ⇒ Object
22 23 24 25 |
# File 'app/facades/report_formatter.rb', line 22 def stats(stat_function) stat = calculate_stats(stat_function) format_stats(stat) end |