Class: Prosperity::Metric
- Inherits:
-
Object
- Object
- Prosperity::Metric
- Defined in:
- lib/prosperity/metric.rb
Class Method Summary collapse
- .aggregate(&block) ⇒ Object
- .extractors ⇒ Object
- .group_by(column = nil) ⇒ Object
- .option(name, &block) ⇒ Object
- .options ⇒ Object
- .ruby? ⇒ Boolean
- .scope(&block) ⇒ Object
- .sql(fragment = nil, &block) ⇒ Object
- .sql? ⇒ Boolean
- .value_at(&block) ⇒ Object
Instance Method Summary collapse
- #aggregate ⇒ Object
- #extractors ⇒ Object
- #group_by ⇒ Object
- #id ⇒ Object
- #options ⇒ Object
- #ruby? ⇒ Boolean
- #scope ⇒ Object
- #sql ⇒ Object
- #sql? ⇒ Boolean
- #title ⇒ Object
- #to_param ⇒ Object
- #to_s ⇒ Object
- #value_at ⇒ Object
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 |
.extractors ⇒ Object
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 ||= if block_given? @options[name] = Metrics::Option.new(name, &block) else raise MissingScope.new end end |
.options ⇒ Object
44 45 46 |
# File 'lib/prosperity/metric.rb', line 44 def self. @options ||= end |
.ruby? ⇒ 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
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
#aggregate ⇒ Object
103 104 105 |
# File 'lib/prosperity/metric.rb', line 103 def aggregate self.class.aggregate end |
#extractors ⇒ Object
79 80 81 |
# File 'lib/prosperity/metric.rb', line 79 def extractors self.class.extractors.values end |
#group_by ⇒ Object
83 84 85 |
# File 'lib/prosperity/metric.rb', line 83 def group_by self.class.group_by end |
#id ⇒ Object
111 112 113 |
# File 'lib/prosperity/metric.rb', line 111 def id self.class.name end |
#options ⇒ Object
99 100 101 |
# File 'lib/prosperity/metric.rb', line 99 def self.class. end |
#ruby? ⇒ Boolean
127 128 129 |
# File 'lib/prosperity/metric.rb', line 127 def ruby? self.class.ruby? end |
#scope ⇒ Object
87 88 89 |
# File 'lib/prosperity/metric.rb', line 87 def scope self.class.scope end |
#sql ⇒ Object
91 92 93 |
# File 'lib/prosperity/metric.rb', line 91 def sql self.class.sql end |
#sql? ⇒ Boolean
123 124 125 |
# File 'lib/prosperity/metric.rb', line 123 def sql? self.class.sql? end |
#title ⇒ Object
107 108 109 |
# File 'lib/prosperity/metric.rb', line 107 def title self.class.to_s.gsub(/Metric$/, "").titleize end |
#to_param ⇒ Object
115 116 117 |
# File 'lib/prosperity/metric.rb', line 115 def to_param id end |
#to_s ⇒ Object
119 120 121 |
# File 'lib/prosperity/metric.rb', line 119 def to_s title end |
#value_at ⇒ Object
95 96 97 |
# File 'lib/prosperity/metric.rb', line 95 def value_at self.class.value_at end |