2017-07-09 08:06:36 -04:00
|
|
|
# frozen_string_literal: true
|
2017-07-10 09:39:13 -04:00
|
|
|
|
2015-03-10 01:00:37 -04:00
|
|
|
module ActiveSupport
|
|
|
|
# Wrapping an array in an +ArrayInquirer+ gives a friendlier way to check
|
|
|
|
# its string-like contents:
|
|
|
|
#
|
|
|
|
# variants = ActiveSupport::ArrayInquirer.new([:phone, :tablet])
|
|
|
|
#
|
|
|
|
# variants.phone? # => true
|
|
|
|
# variants.tablet? # => true
|
|
|
|
# variants.desktop? # => false
|
|
|
|
class ArrayInquirer < Array
|
2015-05-21 14:26:41 -04:00
|
|
|
# Passes each element of +candidates+ collection to ArrayInquirer collection.
|
2016-05-11 10:15:01 -04:00
|
|
|
# The method returns true if any element from the ArrayInquirer collection
|
|
|
|
# is equal to the stringified or symbolized form of any element in the +candidates+ collection.
|
|
|
|
#
|
|
|
|
# If +candidates+ collection is not given, method returns true.
|
2015-05-21 14:26:41 -04:00
|
|
|
#
|
|
|
|
# variants = ActiveSupport::ArrayInquirer.new([:phone, :tablet])
|
|
|
|
#
|
|
|
|
# variants.any? # => true
|
|
|
|
# variants.any?(:phone, :tablet) # => true
|
|
|
|
# variants.any?('phone', 'desktop') # => true
|
|
|
|
# variants.any?(:desktop, :watch) # => false
|
2017-01-14 12:43:13 -05:00
|
|
|
def any?(*candidates)
|
2015-03-10 01:00:37 -04:00
|
|
|
if candidates.none?
|
|
|
|
super
|
|
|
|
else
|
|
|
|
candidates.any? do |candidate|
|
2015-08-28 16:00:48 -04:00
|
|
|
include?(candidate.to_sym) || include?(candidate.to_s)
|
2015-03-10 01:00:37 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
def respond_to_missing?(name, include_private = false)
|
2017-01-14 12:38:21 -05:00
|
|
|
(name[-1] == "?") || super
|
2015-03-10 01:00:37 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def method_missing(name, *args)
|
2016-08-06 11:58:50 -04:00
|
|
|
if name[-1] == "?"
|
2015-03-10 01:00:37 -04:00
|
|
|
any?(name[0..-2])
|
|
|
|
else
|
|
|
|
super
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|