From 0d874b4a5a39646bbfc3e062db62a3b3ab68b47c Mon Sep 17 00:00:00 2001 From: Ryuta Kamizono Date: Sat, 20 May 2017 12:36:16 +0900 Subject: Make `VALID_DIRECTIONS` to `Set` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ```ruby require "benchmark/ips" require "set" array = [:asc, :desc, :ASC, :DESC, "asc", "desc", "ASC", "DESC"] set = array.to_set item = "DESC" Benchmark.ips do |x| x.report "array" do array.include?(item) end x.report "set" do set.include?(item) end end ``` ``` % ruby array_vs_set.rb Warming up -------------------------------------- array 188.441k i/100ms set 229.531k i/100ms Calculating ------------------------------------- array 3.508M (± 9.0%) i/s - 17.525M in 5.043058s set 5.134M (± 7.6%) i/s - 25.707M in 5.038921s ``` --- activerecord/lib/active_record/relation/query_methods.rb | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb index 76e529f2de..79e65baae5 100644 --- a/activerecord/lib/active_record/relation/query_methods.rb +++ b/activerecord/lib/active_record/relation/query_methods.rb @@ -1100,14 +1100,16 @@ module ActiveRecord end VALID_DIRECTIONS = [:asc, :desc, :ASC, :DESC, - "asc", "desc", "ASC", "DESC"] # :nodoc: + "asc", "desc", "ASC", "DESC"].to_set # :nodoc: def validate_order_args(args) args.each do |arg| next unless arg.is_a?(Hash) arg.each do |_key, value| - raise ArgumentError, "Direction \"#{value}\" is invalid. Valid " \ - "directions are: #{VALID_DIRECTIONS.inspect}" unless VALID_DIRECTIONS.include?(value) + unless VALID_DIRECTIONS.include?(value) + raise ArgumentError, + "Direction \"#{value}\" is invalid. Valid directions are: #{VALID_DIRECTIONS.to_a.inspect}" + end end end end -- cgit v1.2.3