class Prosperity::Metric

Public Class Methods

aggregate(&block) click to toggle source
# 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
extractors() click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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
options() click to toggle source
# File lib/prosperity/metric.rb, line 44
def self.options
  @options ||= default_options
end
ruby?() click to toggle source
# File lib/prosperity/metric.rb, line 75
def self.ruby?
  @value_at.present?
end
scope(&block) click to toggle source
# 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) click to toggle source
# 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?() click to toggle source
# File lib/prosperity/metric.rb, line 71
def self.sql?
  @sql.present?
end
value_at(&block) click to toggle source
# 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

Private Class Methods

default_options() click to toggle source
# File lib/prosperity/metric.rb, line 133
def self.default_options
  o = Metrics::Option.new("default") do |scope|
    scope
  end
  {"default" => o}
end

Public Instance Methods

aggregate() click to toggle source
# File lib/prosperity/metric.rb, line 103
def aggregate
  self.class.aggregate
end
extractors() click to toggle source
# File lib/prosperity/metric.rb, line 79
def extractors
  self.class.extractors.values
end
group_by() click to toggle source
# File lib/prosperity/metric.rb, line 83
def group_by
  self.class.group_by
end
id() click to toggle source
# File lib/prosperity/metric.rb, line 111
def id
  self.class.name
end
options() click to toggle source
# File lib/prosperity/metric.rb, line 99
def options
  self.class.options
end
ruby?() click to toggle source
# File lib/prosperity/metric.rb, line 127
def ruby?
  self.class.ruby?
end
scope() click to toggle source
# File lib/prosperity/metric.rb, line 87
def scope
  self.class.scope
end
sql() click to toggle source
# File lib/prosperity/metric.rb, line 91
def sql
  self.class.sql
end
sql?() click to toggle source
# File lib/prosperity/metric.rb, line 123
def sql?
  self.class.sql?
end
title() click to toggle source
# File lib/prosperity/metric.rb, line 107
def title
  self.class.to_s.gsub(/Metric$/, "").titleize
end
to_param() click to toggle source
# File lib/prosperity/metric.rb, line 115
def to_param
  id
end
to_s() click to toggle source
# File lib/prosperity/metric.rb, line 119
def to_s
  title
end
value_at() click to toggle source
# File lib/prosperity/metric.rb, line 95
def value_at
  self.class.value_at
end