Class: Trample::Aggregation
- Inherits:
-
Object
- Object
- Trample::Aggregation
- Defined in:
- lib/trample/aggregation.rb
Defined Under Namespace
Instance Attribute Summary collapse
-
#bucket_sort ⇒ Object
Usage: aggregation :foo do |agg| agg.bucket_sort = :count # OR agg.bucket_sort = :alpha # OR add.bucket_sort = proc { |a, b| … } end.
Instance Method Summary collapse
- #buckets ⇒ Object
- #find_or_initialize_bucket(key) ⇒ Object
- #force(key, opts = {}) ⇒ Object
- #selections ⇒ Object
- #selections? ⇒ Boolean
- #to_query ⇒ Object
Instance Attribute Details
#bucket_sort ⇒ Object
Usage: aggregation :foo do |agg|
agg.bucket_sort = :count
# OR agg.bucket_sort = :alpha
# OR add.bucket_sort = proc { |a, b| ... }
end
48 49 50 |
# File 'lib/trample/aggregation.rb', line 48 def bucket_sort @bucket_sort end |
Instance Method Details
#buckets ⇒ Object
66 67 68 69 70 |
# File 'lib/trample/aggregation.rb', line 66 def buckets ordered = super ordered.sort!(&bucket_sort) ordered end |
#find_or_initialize_bucket(key) ⇒ Object
76 77 78 79 80 81 82 83 |
# File 'lib/trample/aggregation.rb', line 76 def find_or_initialize_bucket(key) bucket = buckets.find { |b| b['key'].downcase == key.downcase } if bucket.nil? bucket = Bucket.new(key: key) self.buckets << bucket end bucket end |
#force(key, opts = {}) ⇒ Object
72 73 74 |
# File 'lib/trample/aggregation.rb', line 72 def force(key, opts = {}) self.buckets << opts.merge(key: key) end |
#selections ⇒ Object
38 39 40 |
# File 'lib/trample/aggregation.rb', line 38 def selections buckets.select(&:selected?).map(&:key) end |
#selections? ⇒ Boolean
34 35 36 |
# File 'lib/trample/aggregation.rb', line 34 def selections? !selections.empty? end |
#to_query ⇒ Object
30 31 32 |
# File 'lib/trample/aggregation.rb', line 30 def to_query {name => selections} end |