Class: Graphiti::Stats::DSL
Overview
Provides an easier interface to stats scoping.
Used within Resource DSL:
allow_stat total: [:count] do
# ... eval'd in Stats::DSL context! ...
end
This allows us to define arbitrary stats:
allow_stat total: [:count] do
standard_deviation { |scope, attr| ... }
end
And use convenience methods:
allow_stat :rating do
count!
average!
end
Instance Attribute Summary collapse
-
#calculations ⇒ Hash
readonly
procs for various metrics.
-
#name ⇒ Symbol
readonly
the stat, e.g.
Instance Method Summary collapse
-
#average! ⇒ Object
Convenience method for default :average proc.
-
#calculation(name) ⇒ Proc
Grab a calculation proc.
-
#count! ⇒ Object
Convenience method for default :count proc.
-
#initialize(adapter, config) ⇒ DSL
constructor
A new instance of DSL.
-
#maximum! ⇒ Object
Convenience method for default :maximum proc.
-
#method_missing(meth, *args, &blk) ⇒ Object
private
Used for defining arbitrary stats within the DSL:.
-
#minimum! ⇒ Object
Convenience method for default :minimum proc.
-
#respond_to_missing?(*args) ⇒ Boolean
rubocop: enable Style/MethodMissingSuper.
-
#sum! ⇒ Object
Convenience method for default :sum proc.
Constructor Details
#initialize(adapter, config) ⇒ DSL
Returns a new instance of DSL.
32 33 34 35 36 37 38 39 |
# File 'lib/graphiti/stats/dsl.rb', line 32 def initialize(adapter, config) config = {config => []} if config.is_a?(Symbol) @adapter = adapter @calculations = {} @name = config.keys.first Array(config.values.first).each { |c| send(:"#{c}!") } end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args, &blk) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Used for defining arbitrary stats within the DSL:
allow_stat :total do
standard_deviation { |scope, attr| ... }
end
…will hit method_missing
and store the proc for future reference.
49 50 51 |
# File 'lib/graphiti/stats/dsl.rb', line 49 def method_missing(meth, *args, &blk) @calculations[meth] = blk end |
Instance Attribute Details
#calculations ⇒ Hash (readonly)
procs for various metrics
27 28 29 |
# File 'lib/graphiti/stats/dsl.rb', line 27 def calculations @calculations end |
#name ⇒ Symbol (readonly)
the stat, e.g. :total
27 28 29 |
# File 'lib/graphiti/stats/dsl.rb', line 27 def name @name end |
Instance Method Details
#average! ⇒ Object
Convenience method for default :average proc
79 80 81 |
# File 'lib/graphiti/stats/dsl.rb', line 79 def average! @calculations[:average] = @adapter.method(:average) end |
#calculation(name) ⇒ Proc
Grab a calculation proc. Raises error if no corresponding stat has been configured.
63 64 65 66 |
# File 'lib/graphiti/stats/dsl.rb', line 63 def calculation(name) callable = @calculations[name] || @calculations[name.to_sym] callable || raise(Errors::StatNotFound.new(@name, name)) end |
#count! ⇒ Object
Convenience method for default :count proc
69 70 71 |
# File 'lib/graphiti/stats/dsl.rb', line 69 def count! @calculations[:count] = @adapter.method(:count) end |
#maximum! ⇒ Object
Convenience method for default :maximum proc
84 85 86 |
# File 'lib/graphiti/stats/dsl.rb', line 84 def maximum! @calculations[:maximum] = @adapter.method(:maximum) end |
#minimum! ⇒ Object
Convenience method for default :minimum proc
89 90 91 |
# File 'lib/graphiti/stats/dsl.rb', line 89 def minimum! @calculations[:minimum] = @adapter.method(:minimum) end |
#respond_to_missing?(*args) ⇒ Boolean
rubocop: enable Style/MethodMissingSuper
54 55 56 |
# File 'lib/graphiti/stats/dsl.rb', line 54 def respond_to_missing?(*args) true end |
#sum! ⇒ Object
Convenience method for default :sum proc
74 75 76 |
# File 'lib/graphiti/stats/dsl.rb', line 74 def sum! @calculations[:sum] = @adapter.method(:sum) end |