Class: Stats::Report

Inherits:
Object
  • Object
show all
Defined in:
lib/stats/report.rb

Direct Known Subclasses

EventReport, PercentWatchedReport

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Report

Returns a new instance of Report.



2
3
4
5
6
7
8
9
10
11
# File 'lib/stats/report.rb', line 2

def initialize options = {}
  options.assert_valid_keys self.class.attributes

  self.class.attributes.each { |a| instance_variable_set "@#{a}", nil }
  options.each { |k, v| instance_variable_set "@#{k}", v }
  self.class.numeric_attributes.each do |a|
    attribute_name = "@#{a}"        
    instance_variable_set(attribute_name, 0) if instance_variable_get(attribute_name).blank?
  end
end

Class Method Details

.array_to_csv(report_array, options = {}) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/stats/report.rb', line 28

def self.array_to_csv(report_array, options = {})
  buf = ""

  unless report_array.first.nil?
    label          = report_array.first.label
    new_options    = options.merge( { :label => label } )
    the_csv_labels = options[:images] ? Stats::ImageReport.csv_labels : report_array[0].class.csv_labels
    Stats::EventReport::SUPPRESSED_LABELS.each do |key, labels|
      the_csv_labels = strip_label(key, the_csv_labels) if labels.include?(label)
    end
    

    buf << the_csv_labels + "\n" unless report_array.empty?

    report_array.each do |report|
      buf << report.to_csv(new_options) + "\n"
    end
  end

  buf
end

.array_to_xml(report_array, options = {}) ⇒ Object



24
25
26
# File 'lib/stats/report.rb', line 24

def self.array_to_xml(report_array, options = {})
  report_array.to_xml options.merge({:root => 'statistics-reports'})
end

.csv_keysObject



55
56
57
# File 'lib/stats/report.rb', line 55

def self.csv_keys
  []
end

.csv_labelsObject



64
65
66
# File 'lib/stats/report.rb', line 64

def self.csv_labels
  csv_keys.map { |k| format_csv_value(k.to_s) }.join(',')
end

.format_csv_value(value) ⇒ Object



68
69
70
71
# File 'lib/stats/report.rb', line 68

def self.format_csv_value(value)
  return "\"#{value}\"" if value =~ /,/
  value
end

.pad_reports(reports, options) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/stats/report.rb', line 80

def self.pad_reports(reports, options)
  limit = options["limit"].to_i
  return reports if limit < 1 || reports.length < 1

  # make a copy of the first report
  clone = reports[0].clone
  # blank out all text fields
  clone.class.send(:attributes).each { |key| clone.send("#{key}=", "") }
  # zero out all numeric fields
  clone.class.send(:numeric_attributes).each { |key| clone.send("#{key}=", 0) }
  # add empty reports at the end
  (limit - reports.length).times { reports << clone }
  reports
end

.set_attributes(attrs) ⇒ Object

class instance variables (@attributes and @numeric_attributes) are used to make validation, initialization, assignment, comparison, and accessor generation generic



97
98
99
100
101
# File 'lib/stats/report.rb', line 97

def self.set_attributes attrs
  attr_accessor *attrs
  @attributes ||= []
  @attributes.concat attrs
end

.set_numeric_attributes(attrs) ⇒ Object



103
104
105
106
107
# File 'lib/stats/report.rb', line 103

def self.set_numeric_attributes attrs
  @numeric_attributes ||= []
  @numeric_attributes.concat attrs
  set_attributes attrs
end

.strip_label(label_sym, labels_as_str) ⇒ Object



50
51
52
53
# File 'lib/stats/report.rb', line 50

def self.strip_label(label_sym, labels_as_str)
  labels = labels_as_str.split(/,/)
  (labels - [ label_sym.to_s ]).join(',')
end

.to_amcharts(reports, *options) ⇒ Object



73
74
75
76
77
78
# File 'lib/stats/report.rb', line 73

def self.to_amcharts reports, *options
  return reports.first.class.to_amcharts(reports, *options) unless reports.empty?
  xml = Builder::XmlMarkup.new(:indent => 2)
  xml.instruct! :xml, :version=>"1.0", :encoding=>"UTF-8"
  xml.chart
end

Instance Method Details

#==(other) ⇒ Object



13
14
15
16
17
18
# File 'lib/stats/report.rb', line 13

def == other
  self.class.attributes.all? do |e|
    attribute_name = "@#{e}"        
    instance_variable_get(attribute_name) == other.instance_variable_get(attribute_name)
  end
end

#[](method_name) ⇒ Object



20
21
22
# File 'lib/stats/report.rb', line 20

def [] method_name
  send method_name
end

#to_csv(options = {}) ⇒ Object



59
60
61
62
# File 'lib/stats/report.rb', line 59

def to_csv(options = {})
  klass_keys = options[:images] ? Stats::ImageReport.csv_keys : self.class.csv_keys 
  klass_keys.map { |k| self.class.format_csv_value(send(k)) }.join(',')
end