public
Rubygem
Description: Merb Core: All you need. None you don't.
Homepage: http://www.merbivore.com
Clone URL: git://github.com/wycats/merb-core.git
Search Repo:
Allow http status to be a symbol - refactored String snake_case method

It's now possible to assign a Merb Controller's status-code using a 
symbol,
thanks to a proper Exceptions::STATUS_CODES lookup hash.

Along the way this uncovered some flaws in the current snake_case method. 
With
some help from mattetti a new version was crafted that's a bit faster than

my original implementation. Two or more capitals in a row are now kept 
together,
which means that previously 'CNNNews'.snake_case would become 
'c_n_n_news',
whereas now, it will be what you'd expect: 'cnn_news'.
fabien (author)
Sun May 04 03:18:22 -0700 2008
commit  2e673fbb7e4db064824af6e2aa3552149313d7fc
tree    8cdcb7b726bc82eda5e90806012b0b1974fc309b
parent  65dc024eb6551bec259c9b971fb1a083b999e298
...
111
112
113
114
 
115
116
117
...
171
172
173
174
 
175
176
177
...
111
112
113
 
114
115
116
117
...
171
172
173
 
174
175
176
177
0
@@ -111,7 +111,7 @@
0
         # ==== Returns
0
         # String:: The snake cased name of the class without the namespace.
0
         def name
0
- self.to_s.snake_case.split('::').last
0
+ self.to_s.split('::').last.snake_case
0
         end
0
       
0
         # Get the actual status-code for an Exception class.
0
@@ -171,7 +171,7 @@
0
         # ==== Parameters
0
         # num<~to_i>:: The status code
0
         def register_status_code(klass, code)
0
- STATUS_CODES[klass.name.snake_case.to_sym] = code.to_i
0
+ STATUS_CODES[klass.name.e.to_sym] = code.to_i
0
         end
0
         
0
       end
...
194
195
196
197
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
198
199
200
...
194
195
196
 
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
0
@@ -194,7 +194,21 @@
0
   end
0
 
0
   attr_reader :request, :headers
0
- attr_accessor :status
0
+ attr_reader :status
0
+
0
+ # Set the response status code.
0
+ #
0
+ # ==== Parameters
0
+ # s<Fixnum, Symbol>:: A status-code or named http-status
0
+ def status=(s)
0
+ if s.is_a?(Symbol) && STATUS_CODES.key?(s)
0
+ @status = STATUS_CODES[s]
0
+ elsif s.is_a?(Fixnum)
0
+ @status = s
0
+ else
0
+ raise ArgumentError, "Status should be of type Fixnum or Symbol, was #{s.class}"
0
+ end
0
+ end
0
 
0
   # ==== Returns
0
   # Hash:: The parameters from the request object
...
25
26
27
 
 
28
29
 
 
 
30
31
32
...
25
26
27
28
29
30
 
31
32
33
34
35
36
0
@@ -25,8 +25,12 @@
0
   #
0
   # ==== Examples
0
   # "FooBar".snake_case #=> "foo_bar"
0
+ # "HeadlineCNNNews".snake_case #=> "headline_cnn_news"
0
+ # "CNN".snake_case #=> "cnn"
0
   def snake_case
0
- gsub(/\B[A-Z]/, '_\&').downcase
0
+ return self.downcase if self =~ /^[A-Z]+$/
0
+ self.gsub(/([A-Z]+)(?=[A-Z][a-z]?)|\B[A-Z]/, '_\&') =~ /_*(.*)/
0
+ return $+.downcase
0
   end
0
 
0
   # ==== Returns
...
74
75
76
77
 
 
 
78
79
80
...
74
75
76
 
77
78
79
80
81
82
0
@@ -74,7 +74,9 @@
0
   end
0
 
0
   it "handles CamelCase with more than 2 capital letter in a row" do
0
- "CNNNews".snake_case.should == "c_n_n_news"
0
+ "CNN".snake_case.should == "cnn"
0
+ "CNNNews".snake_case.should == "cnn_news"
0
+ "HeadlineCNNNews".snake_case.should == "headline_cnn_news"
0
   end
0
 
0
   it "does NOT change one word lowercase" do
...
10
11
12
13
 
 
 
14
15
16
...
26
27
28
29
30
 
31
...
10
11
12
 
13
14
15
16
17
18
...
28
29
30
 
 
31
32
0
@@ -10,7 +10,9 @@
0
   end
0
   
0
   it "should dispatch to callable actions" do
0
- dispatch_to(Merb::Test::Fixtures::Controllers::Base, :index).body.should == "index"
0
+ controller = dispatch_to(Merb::Test::Fixtures::Controllers::Base, :index)
0
+ controller.body.should == "index"
0
+ controller.status.should == 200
0
   end
0
 
0
   it "should not dispatch to hidden actions" do
0
@@ -26,7 +28,6 @@
0
     calling { dispatch_to(Merb::Test::Fixtures::Controllers::Base, :bat) }.
0
       should raise_error(Merb::ControllerExceptions::ActionNotFound)
0
   end
0
-
0
-
0
+
0
 end
...
28
29
30
 
31
32
33
...
28
29
30
31
32
33
34
0
@@ -28,6 +28,7 @@
0
       include Inclusion
0
       
0
       def index
0
+ self.status = :ok
0
         "index"
0
       end
0
 

Comments

    No one has commented yet.