Class: JsonapiCompliable::Stats::DSL
- Inherits:
-
Object
- Object
- JsonapiCompliable::Stats::DSL
show all
- Defined in:
- lib/jsonapi_compliable/stats/dsl.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(config) ⇒ DSL
Returns a new instance of DSL.
16
17
18
19
20
21
22
|
# File 'lib/jsonapi_compliable/stats/dsl.rb', line 16
def initialize(config)
config = { config => [] } if config.is_a?(Symbol)
@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
24
25
26
|
# File 'lib/jsonapi_compliable/stats/dsl.rb', line 24
def method_missing(meth, *args, &blk)
@calculations[meth] = blk
end
|
Instance Attribute Details
#calculations ⇒ Object
Returns the value of attribute calculations.
4
5
6
|
# File 'lib/jsonapi_compliable/stats/dsl.rb', line 4
def calculations
@calculations
end
|
#name ⇒ Object
Returns the value of attribute name.
4
5
6
|
# File 'lib/jsonapi_compliable/stats/dsl.rb', line 4
def name
@name
end
|
Class Method Details
.defaults ⇒ Object
6
7
8
9
10
11
12
13
14
|
# File 'lib/jsonapi_compliable/stats/dsl.rb', line 6
def self.defaults
{
count: ->(scope, attr) { scope.count },
average: ->(scope, attr) { scope.average(attr).to_f },
sum: ->(scope, attr) { scope.sum(attr) },
maximum: ->(scope, attr) { scope.maximum(attr) },
minimum: ->(scope, attr) { scope.minimum(attr) }
}
end
|
Instance Method Details
#average! ⇒ Object
41
42
43
|
# File 'lib/jsonapi_compliable/stats/dsl.rb', line 41
def average!
@calculations[:average] = self.class.defaults[:average]
end
|
#calculation(name) ⇒ Object
28
29
30
31
|
# File 'lib/jsonapi_compliable/stats/dsl.rb', line 28
def calculation(name)
callable = @calculations[name] || @calculations[name.to_sym]
callable || raise(Errors::StatNotFound.new(@name, name))
end
|
#count! ⇒ Object
33
34
35
|
# File 'lib/jsonapi_compliable/stats/dsl.rb', line 33
def count!
@calculations[:count] = self.class.defaults[:count]
end
|
#maximum! ⇒ Object
45
46
47
|
# File 'lib/jsonapi_compliable/stats/dsl.rb', line 45
def maximum!
@calculations[:maximum] = self.class.defaults[:maximum]
end
|
#minimum! ⇒ Object
49
50
51
|
# File 'lib/jsonapi_compliable/stats/dsl.rb', line 49
def minimum!
@calculations[:minimum] = self.class.defaults[:minimum]
end
|
#sum! ⇒ Object
37
38
39
|
# File 'lib/jsonapi_compliable/stats/dsl.rb', line 37
def sum!
@calculations[:sum] = self.class.defaults[:sum]
end
|