diff options
author | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2015-03-27 16:20:52 -0300 |
---|---|---|
committer | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2015-03-27 16:20:52 -0300 |
commit | 5cb8e0046cf93d1e5468db8abb95a1f7b8a04b42 (patch) | |
tree | c2b3f5d97d469831d671aa60663de419508b4e0f /activesupport/lib/active_support | |
parent | 72a02f98614261741d39432b239cded410f46b74 (diff) | |
parent | c64b99ecc98341d504aced72448bee758f3cfdaf (diff) | |
download | rails-5cb8e0046cf93d1e5468db8abb95a1f7b8a04b42.tar.gz rails-5cb8e0046cf93d1e5468db8abb95a1f7b8a04b42.tar.bz2 rails-5cb8e0046cf93d1e5468db8abb95a1f7b8a04b42.zip |
Merge pull request #18939 from georgeclaghorn/variant-inquiry
Provide friendlier access to request variants
Diffstat (limited to 'activesupport/lib/active_support')
-rw-r--r-- | activesupport/lib/active_support/array_inquirer.rb | 38 | ||||
-rw-r--r-- | activesupport/lib/active_support/core_ext/array.rb | 1 | ||||
-rw-r--r-- | activesupport/lib/active_support/core_ext/array/inquiry.rb | 15 |
3 files changed, 54 insertions, 0 deletions
diff --git a/activesupport/lib/active_support/array_inquirer.rb b/activesupport/lib/active_support/array_inquirer.rb new file mode 100644 index 0000000000..0ae534da00 --- /dev/null +++ b/activesupport/lib/active_support/array_inquirer.rb @@ -0,0 +1,38 @@ +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 + # + # variants.any?(:phone, :tablet) # => true + # variants.any?(:phone, :desktop) # => true + # variants.any?(:desktop, :watch) # => false + class ArrayInquirer < Array + def any?(*candidates, &block) + if candidates.none? + super + else + candidates.any? do |candidate| + include?(candidate) || include?(candidate.to_sym) + end + end + end + + private + def respond_to_missing?(name, include_private = false) + name[-1] == '?' + end + + def method_missing(name, *args) + if name[-1] == '?' + any?(name[0..-2]) + else + super + end + end + end +end diff --git a/activesupport/lib/active_support/core_ext/array.rb b/activesupport/lib/active_support/core_ext/array.rb index 7d0c1e4c8d..7551551bd7 100644 --- a/activesupport/lib/active_support/core_ext/array.rb +++ b/activesupport/lib/active_support/core_ext/array.rb @@ -4,3 +4,4 @@ require 'active_support/core_ext/array/conversions' require 'active_support/core_ext/array/extract_options' require 'active_support/core_ext/array/grouping' require 'active_support/core_ext/array/prepend_and_append' +require 'active_support/core_ext/array/inquiry' diff --git a/activesupport/lib/active_support/core_ext/array/inquiry.rb b/activesupport/lib/active_support/core_ext/array/inquiry.rb new file mode 100644 index 0000000000..de623c466c --- /dev/null +++ b/activesupport/lib/active_support/core_ext/array/inquiry.rb @@ -0,0 +1,15 @@ +class Array + # Wraps the array in an +ArrayInquirer+ object, which gives a friendlier way + # to check its string-like contents. + # + # pets = [:cat, :dog].inquiry + # + # pets.cat? # => true + # pets.ferret? # => false + # + # pets.any?(:cat, :ferret) # => true + # pets.any?(:ferret, :alligator) # => false + def inquiry + ActiveSupport::ArrayInquirer.new(self) + end +end |