diff options
author | George Claghorn <george.claghorn@gmail.com> | 2015-03-10 01:00:37 -0400 |
---|---|---|
committer | George Claghorn <george@basecamp.com> | 2015-03-24 13:33:01 -0500 |
commit | c64b99ecc98341d504aced72448bee758f3cfdaf (patch) | |
tree | d704a17a440c57e6e45a4005e7e331a7dc00672f /activesupport/test | |
parent | 9d9cc4777be3787ed3645d704f02e5ba1228be13 (diff) | |
download | rails-c64b99ecc98341d504aced72448bee758f3cfdaf.tar.gz rails-c64b99ecc98341d504aced72448bee758f3cfdaf.tar.bz2 rails-c64b99ecc98341d504aced72448bee758f3cfdaf.zip |
Add ActiveSupport::ArrayInquirer and Array#inquiry
Wrapping an array in an `ArrayInquirer` gives a friendlier way to check its
string-like contents. For example, `request.variant` returns an `ArrayInquirer`
object. To check a request's variants, you can call:
request.variant.phone?
request.variant.any?(:phone, :tablet)
...instead of:
request.variant.include?(:phone)
request.variant.any? { |v| v.in?([:phone, :tablet]) }
`Array#inquiry` is a shortcut for wrapping the receiving array in an
`ArrayInquirer`:
pets = [:cat, :dog]
pets.cat? # => true
pets.ferret? # => false
pets.any?(:cat, :ferret} # => true
Diffstat (limited to 'activesupport/test')
-rw-r--r-- | activesupport/test/array_inquirer_test.rb | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/activesupport/test/array_inquirer_test.rb b/activesupport/test/array_inquirer_test.rb new file mode 100644 index 0000000000..488e0d34a9 --- /dev/null +++ b/activesupport/test/array_inquirer_test.rb @@ -0,0 +1,35 @@ +require 'abstract_unit' + +class ArrayInquirerTest < ActiveSupport::TestCase + def setup + @array_inquirer = ActiveSupport::ArrayInquirer.new([:mobile, :tablet]) + end + + def test_individual + assert @array_inquirer.mobile? + assert @array_inquirer.tablet? + assert_not @array_inquirer.desktop? + end + + def test_any + assert @array_inquirer.any?(:mobile, :desktop) + assert @array_inquirer.any?(:watch, :tablet) + assert_not @array_inquirer.any?(:desktop, :watch) + end + + def test_any_with_block + assert @array_inquirer.any? { |v| v == :mobile } + assert_not @array_inquirer.any? { |v| v == :desktop } + end + + def test_respond_to + assert_respond_to @array_inquirer, :development? + end + + def test_inquiry + result = [:mobile, :tablet].inquiry + + assert_instance_of ActiveSupport::ArrayInquirer, result + assert_equal @array_inquirer, result + end +end |