Class: Ossert::Stats::Base

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

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBase

Returns a new instance of Base.



72
73
74
75
76
# File 'lib/ossert/stats/base.rb', line 72

def initialize
  self.class.attributes.each do |var, type|
    instance_variable_set("@#{var}", Kernel.const_get(type).new) if type
  end
end

Class Attribute Details

.sectionObject

Returns the value of attribute section.



6
7
8
# File 'lib/ossert/stats/base.rb', line 6

def section
  @section
end

.section_typeObject

Returns the value of attribute section_type.



6
7
8
# File 'lib/ossert/stats/base.rb', line 6

def section_type
  @section_type
end

Class Method Details

.absolute_attributesObject



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

def absolute_attributes
  @absolute_attributes ||= config['absolute_attributes'].to_a
end

.attributesObject



12
13
14
# File 'lib/ossert/stats/base.rb', line 12

def attributes
  @attributes ||= config['attributes']
end

.attributes_namesObject



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

def attributes_names
  @attributes_names ||= attributes.keys
end

.configObject



8
9
10
# File 'lib/ossert/stats/base.rb', line 8

def config
  @config ||= ::Settings['stats'][section][section_type]
end

.create_attributes_accessorsObject



32
33
34
# File 'lib/ossert/stats/base.rb', line 32

def create_attributes_accessors
  attr_accessor(*attributes_names)
end

.define_counts(*attributes) ⇒ Object



42
43
44
45
46
# File 'lib/ossert/stats/base.rb', line 42

def define_counts(*attributes)
  Array.wrap(attributes).each do |metric|
    define_method("#{metric}_count") { public_send(metric).count }
  end
end

.define_ints(*attributes) ⇒ Object



36
37
38
39
40
# File 'lib/ossert/stats/base.rb', line 36

def define_ints(*attributes)
  Array.wrap(attributes).each do |metric|
    define_method("#{metric}_int") { public_send(metric).to_i }
  end
end

.define_percent(attributes, default_value: 0) ⇒ Object



48
49
50
51
52
53
54
55
56
57
# File 'lib/ossert/stats/base.rb', line 48

def define_percent(attributes, default_value: 0)
  attributes.to_h.each do |metric, total|
    define_method("#{metric}_percent") do
      total_count = public_send(total).count
      return default_value if total_count.zero?

      ((public_send(metric).count.to_d / total_count.to_d) * 100).round(2)
    end
  end
end

.metricsObject



28
29
30
# File 'lib/ossert/stats/base.rb', line 28

def metrics
  @metrics ||= config['metrics']
end

.uniq_attributesObject



16
17
18
# File 'lib/ossert/stats/base.rb', line 16

def uniq_attributes
  @uniq_attributes ||= config['uniq_attributes'].to_a
end

Instance Method Details

#<<(other_stats) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/ossert/stats/base.rb', line 60

def <<(other_stats)
  self.class.attributes_names.each do |attr|
    next unless other_value = other_stats.instance_variable_get("@#{attr}")
    new_value = other_value
    new_value += instance_variable_get("@#{attr}") unless self.class.absolute_attributes.include?(attr)
    new_value.uniq! if self.class.uniq_attributes.include?(attr)

    instance_variable_set("@#{attr}", new_value)
  end
  self
end

#median(values, default_value: 0) ⇒ Object



78
79
80
81
82
83
84
85
86
# File 'lib/ossert/stats/base.rb', line 78

def median(values, default_value: 0)
  values = Array(values).sort
  return default_value if (count = values.count).zero?

  middle_idx = values.count / 2
  return values[middle_idx] if count.odd?

  (values[middle_idx - 1] + values[middle_idx]) / 2
end

#metric_valuesObject



88
89
90
# File 'lib/ossert/stats/base.rb', line 88

def metric_values
  self.class.metrics.map { |metric| public_send(metric).to_f }
end

#metrics_to_hashObject



92
93
94
95
96
97
98
# File 'lib/ossert/stats/base.rb', line 92

def metrics_to_hash
  self.class.metrics.each_with_object({}) do |var, result|
    value = send(var)
    value.uniq! if self.class.uniq_attributes.include?(var)
    result[var] = value
  end
end

#to_hashObject



100
101
102
103
104
105
106
# File 'lib/ossert/stats/base.rb', line 100

def to_hash
  self.class.attributes_names.each_with_object({}) do |var, result|
    value = send(var)
    value.uniq! if self.class.uniq_attributes.include?(var)
    result[var] = value
  end
end

#to_jsonObject



108
109
110
# File 'lib/ossert/stats/base.rb', line 108

def to_json
  MultiJson.dump(self)
end