class Graphiti::Stats::DSL
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
@see Resource.allow_stat @attr_reader [Symbol] name the stat, e.g. :total @attr_reader [Hash] calculations procs for various metrics
Attributes
Public Class Methods
Source
# 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
@param [Adapters::Abstract] adapter the Resource
adapter @param [Symbol, Hash] config example: :total
or +{ total: [:count] }+
Public Instance Methods
Source
# File lib/graphiti/stats/dsl.rb, line 79 def average! @calculations[:average] = @adapter.method(:average) end
Convenience method for default :average proc
Source
# 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
Grab a calculation proc. Raises error if no corresponding stat has been configured.
@param [String, Symbol] name the name of the calculation, e.g. :total
@return [Proc] the proc to run the calculation
Source
# File lib/graphiti/stats/dsl.rb, line 69 def count! @calculations[:count] = @adapter.method(:count) end
Convenience method for default :count proc
Source
# File lib/graphiti/stats/dsl.rb, line 84 def maximum! @calculations[:maximum] = @adapter.method(:maximum) end
Convenience method for default :maximum proc
Source
# File lib/graphiti/stats/dsl.rb, line 49 def method_missing(meth, *args, &blk) @calculations[meth] = blk end
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. @api private
Source
# File lib/graphiti/stats/dsl.rb, line 89 def minimum! @calculations[:minimum] = @adapter.method(:minimum) end
Convenience method for default :minimum proc
Source
# File lib/graphiti/stats/dsl.rb, line 54 def respond_to_missing?(*args) true end
rubocop: enable Style/MethodMissingSuper
Source
# File lib/graphiti/stats/dsl.rb, line 74 def sum! @calculations[:sum] = @adapter.method(:sum) end
Convenience method for default :sum proc