Class: Graphiti::Stats::DSL

Inherits:
Object show all
Defined in:
lib/graphiti/stats/dsl.rb

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

See Also:

  • Resource.allow_stat

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(adapter, config) ⇒ DSL

Returns a new instance of DSL.

Parameters:

  • adapter (Adapters::Abstract)

    the Resource adapter

  • config (Symbol, Hash)

    example: :total or { total: [:count] }



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

#calculationsHash (readonly)

procs for various metrics

Returns:

  • (Hash)

    the current value of calculations



27
28
29
# File 'lib/graphiti/stats/dsl.rb', line 27

def calculations
  @calculations
end

#nameSymbol (readonly)

the stat, e.g. :total

Returns:

  • (Symbol)

    the current value of name



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.

Parameters:

  • name (String, Symbol)

    the name of the calculation, e.g. :total

Returns:

  • (Proc)

    the proc to run the calculation



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

Returns:

  • (Boolean)


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