Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.Sign up
Provide friendlier access to request variants #18939
request.variant = :phone
This PR implements a friendlier interface:
request.variant = :phone request.variant.phone? # true request.variant.tablet? # false request.variant = [:phone, :tablet] request.variant.phone? # true request.variant.tablet? # true request.variant.desktop? # false request.variant.any?(:phone, :desktop) # true request.variant.any?(:desktop, :watch) # false
@dhh would an ArrayInquirer also imply an ArrayWithIndifferentAccess? or do a runtime cast
e.g. in this example:
class SoftString def to_s 'soft' end end class HardString def to_str 'hard' end end ai = ActiveSupport::ArrayInquirer.new(['foo', :bar, SoftString.new, HardString.new])
What should each of these return? true, false, or cast exception?
ai.foo? ai.bar? ai.soft? ai.hard?
And BTW this is not current behavior of StringInquirer:
ActiveSupport::StringInquirer.new(:foo) # => TypeError: can't convert Symbol into String
It only works for Strings and objects which implement
(perhaps for consistency,
I was thinking about StringInquirer taking a symbol, but then it’s not a symbol any more, it’s a string. I’d rather just be explicit and have a SymbolInquirer that inherits from Symbol.
Awesome, George. Not a mega rush. Thanks for helping with this!
Yes please merge this
My initial inclination when working with
Failing that, I was surprised/dismayed by the fact
added a commit
this pull request
Mar 27, 2015
I mean, it will still be highlighted in the CHANGELOG and documentation but I don't think it needs a
I like the #inquiry form, as it makes it possible to use this inline on a passed parameter without extra setup. And it then maps directly to String#inquiry as well.