class JsonapiCompliable::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

calculations[R]
name[R]

Public Class Methods

new(adapter, config) click to toggle source

@param [Adapters::Abstract] adapter the Resource adapter @param [Symbol, Hash] config example: :total or +{ total: [:count] }+

# File lib/jsonapi_compliable/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

Public Instance Methods

average!() click to toggle source

Convenience method for default :average proc

# File lib/jsonapi_compliable/stats/dsl.rb, line 74
def average!
  @calculations[:average] = @adapter.method(:average)
end
calculation(name) click to toggle source

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

# File lib/jsonapi_compliable/stats/dsl.rb, line 58
def calculation(name)
  callable = @calculations[name] || @calculations[name.to_sym]
  callable || raise(Errors::StatNotFound.new(@name, name))
end
count!() click to toggle source

Convenience method for default :count proc

# File lib/jsonapi_compliable/stats/dsl.rb, line 64
def count!
  @calculations[:count] = @adapter.method(:count)
end
maximum!() click to toggle source

Convenience method for default :maximum proc

# File lib/jsonapi_compliable/stats/dsl.rb, line 79
def maximum!
  @calculations[:maximum] = @adapter.method(:maximum)
end
method_missing(meth, *args, &blk) click to toggle source

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

# File lib/jsonapi_compliable/stats/dsl.rb, line 49
def method_missing(meth, *args, &blk)
  @calculations[meth] = blk
end
minimum!() click to toggle source

Convenience method for default :minimum proc

# File lib/jsonapi_compliable/stats/dsl.rb, line 84
def minimum!
  @calculations[:minimum] = @adapter.method(:minimum)
end
sum!() click to toggle source

Convenience method for default :sum proc

# File lib/jsonapi_compliable/stats/dsl.rb, line 69
def sum!
  @calculations[:sum] = @adapter.method(:sum)
end