Permalink
Browse files

Use keyword arguments

  • Loading branch information...
1 parent 2c06f56 commit 0d31a28e91220b9fcc2487a4525b0a91da462c75 @ankane committed Oct 7, 2016
Showing with 16 additions and 10 deletions.
  1. +4 −0 CHANGELOG.md
  2. +6 −10 lib/hightop.rb
  3. +6 −0 test/hightop_test.rb
View
@@ -1,3 +1,7 @@
+## 0.2.0 [unreleased]
+
+- Use keyword arguments
+
## 0.1.4
- Added `distinct` option to replace `uniq`
View
@@ -2,27 +2,23 @@
require "active_record"
module Hightop
- def top(column, limit = nil, options = {})
- if limit.is_a?(Hash)
- options = limit
- limit = nil
- end
-
- distinct = options[:distinct] || options[:uniq]
+ def top(column, limit = nil, distinct: nil, uniq: nil, min: nil, nil: nil)
+ distinct ||= uniq
order_str = column.is_a?(Array) ? column.map(&:to_s).join(", ") : column
relation = group(column).order("count_#{distinct || 'all'} DESC, #{order_str}")
if limit
relation = relation.limit(limit)
end
- unless options[:nil]
+ # terribly named option
+ unless binding.local_variable_get(:nil)
(column.is_a?(Array) ? column : [column]).each do |c|
relation = relation.where("#{c} IS NOT NULL")
end
end
- if options[:min]
- relation = relation.having("COUNT(#{distinct ? "DISTINCT #{distinct}" : '*'}) >= #{options[:min].to_i}")
+ if min
+ relation = relation.having("COUNT(#{distinct ? "DISTINCT #{distinct}" : '*'}) >= #{min.to_i}")
end
if distinct
@@ -101,6 +101,12 @@ def test_min_distinct
assert_equal expected, Visit.top(:city, min: 2, distinct: :user_id)
end
+ def test_bad_argument
+ assert_raises(ArgumentError) do
+ Visit.top(:city, boom: true)
+ end
+ end
+
def create_city(city, count = 1)
create({city: city}, count)
end

0 comments on commit 0d31a28

Please sign in to comment.