Module: ActiveReporter::Report::Validation

Included in:
ActiveReporter::Report
Defined in:
lib/active_reporter/report/validation.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#errorsObject

Returns the value of attribute errors.



7
8
9
# File 'lib/active_reporter/report/validation.rb', line 7

def errors
  @errors
end

Instance Method Details

#validate_aggregators!Object



29
30
31
32
33
# File 'lib/active_reporter/report/validation.rb', line 29

def validate_aggregators!
  (aggregators.keys - self.class.aggregators.keys).each do |aggregator|
    add_invalid_param_error(:aggregator, ":#{aggregator} is not a valid aggregator (should be in #{self.class.aggregators.keys})")
  end
end

#validate_calculators!Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/active_reporter/report/validation.rb', line 35

def validate_calculators!
  (calculators.keys - self.class.calculators.keys).each do |calculator|
    add_invalid_param_error(:calculator, ":#{calculator} is not a valid calculator (should be in #{self.class.calculators.keys})")
  end

  calculators.values.each do |calculator|
    case
    when calculator.aggregator.nil?
      add_invalid_param_error(:calculator, ":#{calculator.name} must define an aggregator (should be in #{self.class.aggregator.keys})")
    when self.class.aggregators.exclude?(calculator.aggregator)
      add_invalid_param_error(:calculator, ":#{calculator.name} defines an invalid aggregator :#{calculator.aggregator} (should be in #{self.class.aggregators.keys})")
    when params.include?(:aggregators) && aggregators.exclude?(calculator.aggregator)
      params[:aggregators].push(calculator.aggregator)
    end
  end
end

#validate_configuration!Object



21
22
23
24
25
26
27
# File 'lib/active_reporter/report/validation.rb', line 21

def validate_configuration!
  incomplete_message = ['You must declare at least one aggregator or tracker, and at lease one dimension to initialize a report', 'See the README for more details']

  raise ActiveReporter::InvalidParamsError, ["#{self.class.name} does not declare any aggregators or trackers"].concat(incomplete_message).join(". ") if aggregators.empty?
  raise ActiveReporter::InvalidParamsError, ["#{self.class.name} does not declare any dimensions"].concat(incomplete_message).join(". ") if dimensions.except(:totals).empty?
  raise ActiveReporter::InvalidParamsError, 'parent_report must be included in order to process calculations' if calculators.any? && parent_report.nil?
end

#validate_groupers!Object



78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/active_reporter/report/validation.rb', line 78

def validate_groupers!
  unless groupers.all?(&:present?)
    invalid_groupers = grouper_names.zip(groupers).collect { |k,v| k if v.nil? }.compact
    invalid_groupers_message = [
      [
        invalid_groupers.to_sentence,
        (invalid_groupers.one? ? 'is not a' : 'are not'), 'valid', 'dimension'.pluralize(invalid_groupers.count, :_gem_active_reporter)
      ].join(' '),
      "declared dimension include #{dimensions.keys.to_sentence}"
    ].join(". ")
    add_invalid_param_error(:groupers, invalid_groupers_message)
  end
end

#validate_params!Object



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/active_reporter/report/validation.rb', line 9

def validate_params!
  validate_configuration!
  validate_aggregators!
  validate_groupers!
  validate_calculators!
  validate_trackers!
  validate_parent_report!
  validate_total_report!

  raise_invalid_params_error! if errors.present? && errors.any?
end

#validate_parent_report!Object



92
93
94
# File 'lib/active_reporter/report/validation.rb', line 92

def validate_parent_report!
  add_invalid_param_error(:parent_report, 'must be an instance of ActiveReporter::Report') unless parent_report.nil? || parent_report.kind_of?(ActiveReporter::Report)
end

#validate_total_report!Object



96
97
98
# File 'lib/active_reporter/report/validation.rb', line 96

def validate_total_report!
  add_invalid_param_error(:total_report, 'must be an instance of ActiveReporter::Report') unless @total_report.nil? || @total_report.kind_of?(ActiveReporter::Report)
end

#validate_trackers!Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/active_reporter/report/validation.rb', line 52

def validate_trackers!
  (trackers.keys - self.class.trackers.keys).each do |tracker|
    add_invalid_param_error(:tracker, ":#{tracker} is not a valid tracker (should be in #{self.class.trackers.keys})")
  end

  trackers.values.each do |tracker|
    case
    when tracker.aggregator.nil?
      add_invalid_param_error(:tracker, ":#{tracker.name} must define an aggregator (should be in #{self.class.aggregator.keys})")
    when self.class.aggregators.exclude?(tracker.aggregator)
      add_invalid_param_error(:tracker, ":#{tracker.name} defines an invalid aggregator :#{tracker.aggregator} (should be in #{self.class.aggregators.keys})")
    when params.include?(:aggregators) && aggregators.exclude?(tracker.aggregator)
      params[:aggregators].push(tracker.aggregator)
    end

    if tracker.opts.include?(:prior_aggregator)
      case
      when self.class.aggregators.exclude?(tracker.prior_aggregator)
        add_invalid_param_error(:tracker, ":#{tracker.name} defines an invalid prior aggregator :#{tracker.prior_aggregator} (should be in #{self.class.aggregators.keys})")
      when params.include?(:aggregators) && aggregators.exclude?(tracker.prior_aggregator)
        params[:aggregators].push(tracker.prior_aggregator)
      end
    end
  end
end