Permalink
Browse files

Wrapped Rails.env in StringQuestioneer so you can do Rails.env.develo…

…pment? [DHH]
  • Loading branch information...
1 parent 7cfa6ec commit 8afa725f4b98a6e0ceee4792e8ebaebb6189e5f6 @dhh dhh committed Jun 3, 2008
View
@@ -4,6 +4,8 @@
* Namespace Inflector, Dependencies, OrderedOptions, and TimeZone under ActiveSupport [Josh Peek]
+* Added StringQuestioneer for doing things like StringQuestioneer.new("production").production? # => true and StringQuestioneer.new("production").development? # => false [DHH]
+
* Fixed Date#end_of_quarter to not blow up on May 31st [#289 state:resolved] (Danger)
@@ -43,6 +43,8 @@
require 'active_support/ordered_options'
require 'active_support/option_merger'
+require 'active_support/string_questioneer'
+
require 'active_support/values/time_zone'
require 'active_support/duration'
@@ -0,0 +1,9 @@
+class StringQuestioneer < String
+ def method_missing(method_name, *arguments)
+ if method_name.to_s.ends_with?("?")
+ self == method_name.to_s[0..-2]
+ else
+ super
+ end
+ end
+end
@@ -0,0 +1,15 @@
+require 'abstract_unit'
+
+class StringQuestioneerTest < Test::Unit::TestCase
+ def test_match
+ assert StringQuestioneer.new("production").production?
+ end
+
+ def test_miss
+ assert !StringQuestioneer.new("production").development?
+ end
+
+ def test_missing_question_mark
+ assert_raises(NoMethodError) { StringQuestioneer.new("production").production }
+ end
+end
View
@@ -1,5 +1,7 @@
*Edge*
+* Wrapped Rails.env in StringQuestioneer so you can do Rails.env.development? [DHH]
+
* Fixed that RailsInfoController wasn't considering all requests local in development mode (Edgard Castro) [#310 state:resolved]
@@ -37,7 +37,7 @@ def root
end
def env
- RAILS_ENV
+ StringQuestioneer.new(RAILS_ENV)
end
def cache

6 comments on commit 8afa725

Contributor

henrik replied Jun 4, 2008

Nice, but the name “StringQuestioneer” strikes me as very odd. Is this intended to be like trainer/trainee? “Someone being questioned”? I would probably go with StringQuestioner with one “e”.

Very simple and very handy, I like it.

I had implemented slightly the same in my local clone, but extending the RAILS_ENV string inside the env method, It was ugly but worked. A separate class seems more appropiate, specially for testing.

Member

josh replied Jun 4, 2008

David, how about StringInquirer?

Contributor

quirkey replied Jun 4, 2008

+1 for StringInquirer

Member

josh replied Jun 4, 2008

Need to namespace that too.

Can it implement case-folding ? At least demonstrate in its unit tests what it does on strings differing only by case..

Please sign in to comment.