Class: Counter::Definition
- Inherits:
-
Object
- Object
- Counter::Definition
- Includes:
- Singleton
- Defined in:
- lib/counter/definition.rb
Overview
Example usage…
class ProductCounter
include Counter::Definition
# This specifies the association we're counting
count :products
sum :price # optional
filters: { # optional
create: ->(product) { product.premium? }
update: ->(product) { product.has_changed? :premium, to: :true }
delete: ->(product) { product.premium? }
}
end
Instance Attribute Summary collapse
-
#association_name ⇒ Object
Attributes set by Counters#counter integration:.
-
#column_to_count ⇒ Object
When using sum, set the column we’re summing.
-
#conditional ⇒ Object
writeonly
Sets the attribute conditional.
- #conditions ⇒ Object
-
#countable_model ⇒ Object
Set the thing we’re counting (set by Counters#counter).
- #counter_hooks ⇒ Object
- #global_counters ⇒ Object
-
#inverse_association ⇒ Object
Set the inverse association (i.e., from the products to the user).
-
#method_name ⇒ Object
Set the name of the counter (used as the method name).
-
#model ⇒ Object
Set the model we’re attached to (set by Counters#counter).
-
#name ⇒ Object
Returns the value of attribute name.
Class Method Summary collapse
- .after_change(block) ⇒ Object
-
.association_name ⇒ Object
Get the name of the association we’re counting.
-
.count(association_name, as: "#{association_name}_counter") ⇒ Object
Set the association we’re counting.
-
.counter ⇒ Object
Access the counter value for global counters.
-
.find_definition(name) ⇒ Object
for global counter instances to find their definition.
- .global(name = nil) ⇒ Object
-
.on(action, &block) ⇒ Object
Define a conditional filter.
-
.sum(column_name) ⇒ Object
Set the column we’re summing.
Instance Method Summary collapse
- #conditional? ⇒ Boolean
- #global? ⇒ Boolean
-
#record_name ⇒ Object
What we record in Counter::Value#name.
- #sum? ⇒ Boolean
Instance Attribute Details
#association_name ⇒ Object
Attributes set by Counters#counter integration:
18 19 20 |
# File 'lib/counter/definition.rb', line 18 def association_name @association_name end |
#column_to_count ⇒ Object
When using sum, set the column we’re summing
26 27 28 |
# File 'lib/counter/definition.rb', line 26 def column_to_count @column_to_count end |
#conditional=(value) ⇒ Object (writeonly)
Sets the attribute conditional
29 30 31 |
# File 'lib/counter/definition.rb', line 29 def conditional=(value) @conditional = value end |
#conditions ⇒ Object
70 71 72 73 |
# File 'lib/counter/definition.rb', line 70 def conditions @conditions ||= {} @conditions end |
#countable_model ⇒ Object
Set the thing we’re counting (set by Counters#counter)
22 23 24 |
# File 'lib/counter/definition.rb', line 22 def countable_model @countable_model end |
#counter_hooks ⇒ Object
80 81 82 83 |
# File 'lib/counter/definition.rb', line 80 def counter_hooks @counter_hooks ||= [] @counter_hooks end |
#global_counters ⇒ Object
75 76 77 78 |
# File 'lib/counter/definition.rb', line 75 def global_counters @global_counters ||= [] @global_counters end |
#inverse_association ⇒ Object
Set the inverse association (i.e., from the products to the user)
24 25 26 |
# File 'lib/counter/definition.rb', line 24 def inverse_association @inverse_association end |
#method_name ⇒ Object
Set the name of the counter (used as the method name)
31 32 33 |
# File 'lib/counter/definition.rb', line 31 def method_name @method_name end |
#model ⇒ Object
Set the model we’re attached to (set by Counters#counter)
20 21 22 |
# File 'lib/counter/definition.rb', line 20 def model @model end |
#name ⇒ Object
Returns the value of attribute name.
32 33 34 |
# File 'lib/counter/definition.rb', line 32 def name @name end |
Class Method Details
.after_change(block) ⇒ Object
125 126 127 |
# File 'lib/counter/definition.rb', line 125 def self.after_change block instance.counter_hooks << block end |
.association_name ⇒ Object
Get the name of the association we’re counting
105 106 107 |
# File 'lib/counter/definition.rb', line 105 def self.association_name instance.association_name end |
.count(association_name, as: "#{association_name}_counter") ⇒ Object
Set the association we’re counting
91 92 93 94 95 96 |
# File 'lib/counter/definition.rb', line 91 def self.count association_name, as: "#{association_name}_counter" instance.association_name = association_name instance.name = as.to_s # How the counter can be accessed e.g. counter.products_counter instance.method_name = as.to_s end |
.counter ⇒ Object
Access the counter value for global counters
58 59 60 61 62 |
# File 'lib/counter/definition.rb', line 58 def self.counter raise "Unable to find counter instances via #{name}#counter. Use must use #{instance.model}#find_counter or #{instance.model}##{instance.counter_name}" unless instance.global? Counter::Value.find_counter self end |
.find_definition(name) ⇒ Object
for global counter instances to find their definition
53 54 55 |
# File 'lib/counter/definition.rb', line 53 def self.find_definition name Counter::Definition.instance.global_counters.find { |c| c.name == name } end |
.global(name = nil) ⇒ Object
98 99 100 101 102 |
# File 'lib/counter/definition.rb', line 98 def self.global name = nil name ||= name.underscore instance.name = name.to_s Counter::Definition.instance.global_counters << instance end |
.on(action, &block) ⇒ Object
Define a conditional filter
115 116 117 118 119 120 121 122 123 |
# File 'lib/counter/definition.rb', line 115 def self.on action, &block instance.conditional = true conditions = Counter::Conditions.new conditions.instance_eval(&block) instance.conditions[action] ||= [] instance.conditions[action] << conditions end |
.sum(column_name) ⇒ Object
Set the column we’re summing. Leave blank to count the number of items
110 111 112 |
# File 'lib/counter/definition.rb', line 110 def self.sum column_name instance.column_to_count = column_name end |
Instance Method Details
#conditional? ⇒ Boolean
48 49 50 |
# File 'lib/counter/definition.rb', line 48 def conditional? @conditional end |
#global? ⇒ Boolean
44 45 46 |
# File 'lib/counter/definition.rb', line 44 def global? model.nil? && association_name.nil? end |
#record_name ⇒ Object
What we record in Counter::Value#name
65 66 67 68 |
# File 'lib/counter/definition.rb', line 65 def record_name return name if global? "#{model.name.underscore}-#{association_name}" end |
#sum? ⇒ Boolean
40 41 42 |
# File 'lib/counter/definition.rb', line 40 def sum? column_to_count.present? end |