Sha256: 0ca0e8d30201a3c1e5e7e85f3a272404bc531a230d9735a64a4bb23060897c4f

Contents?: true

Size: 688 Bytes

Versions: 2

Compression:

Stored size: 688 Bytes

Contents

module Enumerable
  def top(*args, **options, &block)
    if block || !(respond_to?(:scoping) || respond_to?(:with_scope))
      # TODO raise error if too many arguments
      limit = args[0]
      min = options[:min]

      counts = Hash.new(0)
      map(&block).each do |v|
        counts[v] += 1
      end
      counts.delete(nil) unless options[:nil]
      counts.select! { |_, v| v >= min } if min

      arr = counts.sort_by { |_, v| -v }
      arr = arr[0...limit] if limit
      Hash[arr]
    elsif respond_to?(:scoping)
      scoping { @klass.send(:top, *args, **options, &block) }
    else
      with_scope(self) { klass.send(:top, *args, **options, &block) }
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
hightop-0.2.4 lib/hightop/enumerable.rb
hightop-0.2.3 lib/hightop/enumerable.rb