Class: Trample::Aggregation

Inherits:
Object
  • Object
show all
Defined in:
lib/trample/aggregation.rb

Defined Under Namespace

Classes: Bucket, Buckets

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#bucket_sortObject

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

#bucketsObject



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

#selectionsObject



38
39
40
# File 'lib/trample/aggregation.rb', line 38

def selections
  buckets.select(&:selected?).map(&:key)
end

#selections?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/trample/aggregation.rb', line 34

def selections?
  !selections.empty?
end

#to_queryObject



30
31
32
# File 'lib/trample/aggregation.rb', line 30

def to_query
  {name => selections}
end