Class: Prosperity::Metric

Inherits:
Object
  • Object
show all
Defined in:
lib/prosperity/metric.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.aggregate(&block) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/prosperity/metric.rb', line 56

def self.aggregate(&block)
  if block_given?
    @aggregate = ::Prosperity::Aggregate::AggregateBuilder.new(&block).build
  else
    @aggregate || ::Prosperity::Aggregate::Count.new
  end
end

.extractorsObject



64
65
66
67
68
69
# File 'lib/prosperity/metric.rb', line 64

def self.extractors
  [Extractors::Interval, Extractors::Total, Extractors::Change].inject({}) do |h, ext|
    h[ext.key] = ext
    h
  end
end

.group_by(column = nil) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/prosperity/metric.rb', line 48

def self.group_by(column = nil)
  if column
    @group_by = column
  else
    @group_by || :created_at
  end
end

.option(name, &block) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/prosperity/metric.rb', line 26

def self.option(name, &block)
  raise SqlMetricCannotHaveOption.new unless @sql.nil?
  @options ||= default_options
  if block_given?
    @options[name] = Metrics::Option.new(name, &block)
  else
    raise MissingScope.new
  end
end

.optionsObject



44
45
46
# File 'lib/prosperity/metric.rb', line 44

def self.options
  @options ||= default_options
end

.ruby?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/prosperity/metric.rb', line 75

def self.ruby?
  @value_at.present?
end

.scope(&block) ⇒ Object



3
4
5
6
7
8
9
10
# File 'lib/prosperity/metric.rb', line 3

def self.scope(&block)
  if block_given?
    @scope = block.call
  else
    raise MissingScope.new if @scope.nil?
    @scope
  end
end

.sql(fragment = nil, &block) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/prosperity/metric.rb', line 12

def self.sql(fragment = nil, &block)
  if fragment && block_given?
    raise ArgumentError, "Must pass string or block but not both"
  elsif fragment
    @sql = fragment
  elsif block_given?
    @sql = block.call
  elsif @sql.nil?
    raise MissingSql.new
  else
    @sql
  end
end

.sql?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/prosperity/metric.rb', line 71

def self.sql?
  @sql.present?
end

.value_at(&block) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/prosperity/metric.rb', line 36

def self.value_at(&block)
  if block_given?
    @value_at = block
  else
    @value_at or raise MissingValueAt
  end
end

Instance Method Details

#aggregateObject



103
104
105
# File 'lib/prosperity/metric.rb', line 103

def aggregate
  self.class.aggregate
end

#extractorsObject



79
80
81
# File 'lib/prosperity/metric.rb', line 79

def extractors
  self.class.extractors.values
end

#group_byObject



83
84
85
# File 'lib/prosperity/metric.rb', line 83

def group_by
  self.class.group_by
end

#idObject



111
112
113
# File 'lib/prosperity/metric.rb', line 111

def id
  self.class.name
end

#optionsObject



99
100
101
# File 'lib/prosperity/metric.rb', line 99

def options
  self.class.options
end

#ruby?Boolean

Returns:

  • (Boolean)


127
128
129
# File 'lib/prosperity/metric.rb', line 127

def ruby?
  self.class.ruby?
end

#scopeObject



87
88
89
# File 'lib/prosperity/metric.rb', line 87

def scope
  self.class.scope
end

#sqlObject



91
92
93
# File 'lib/prosperity/metric.rb', line 91

def sql
  self.class.sql
end

#sql?Boolean

Returns:

  • (Boolean)


123
124
125
# File 'lib/prosperity/metric.rb', line 123

def sql?
  self.class.sql?
end

#titleObject



107
108
109
# File 'lib/prosperity/metric.rb', line 107

def title
  self.class.to_s.gsub(/Metric$/, "").titleize
end

#to_paramObject



115
116
117
# File 'lib/prosperity/metric.rb', line 115

def to_param
  id
end

#to_sObject



119
120
121
# File 'lib/prosperity/metric.rb', line 119

def to_s
  title
end

#value_atObject



95
96
97
# File 'lib/prosperity/metric.rb', line 95

def value_at
  self.class.value_at
end