From fa210686fd8e650fc09fb0f56d3c805630b9a14d Mon Sep 17 00:00:00 2001 From: Xavier Noria Date: Sat, 22 Aug 2009 17:03:13 +0200 Subject: AS guide: 2nd pass in aliasing --- .../guides/source/active_support_overview.textile | 29 +++++++++++----------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/railties/guides/source/active_support_overview.textile b/railties/guides/source/active_support_overview.textile index 22e833771e..a4e2534e2d 100644 --- a/railties/guides/source/active_support_overview.textile +++ b/railties/guides/source/active_support_overview.textile @@ -441,7 +441,7 @@ h5. +alias_method_chain+ Using plain Ruby you can wrap methods with other methods, that's called _alias chaining_. -For example, let's say you'd like params to be strings in functional tests, as they are for real requests, but still want the convenience of assigning integers and other kind of values. To accomplish that you could wrap +process+ this way in +test/test_helper.rb+: +For example, let's say you'd like params to be strings in functional tests, as they are in real requests, but still want the convenience of assigning integers and other kind of values. To accomplish that you could wrap +ActionController::TestCase#process+ this way in +test/test_helper.rb+: ActionController::TestCase.class_eval do @@ -450,35 +450,36 @@ ActionController::TestCase.class_eval do # now redefine process and delegate to original_process def process(action, params=nil, session=nil, flash=nil, http_method='GET') - params_str = {} - params.each {|k,v| params_str[k] = v.to_s} - original_process(action, params_str, session, flash, http_method) + params = Hash[*params.map {|k, v| [k, v.to_s]}.flatten] + original_process(action, params, session, flash, http_method) end end That's the method +get+, +post+, etc., delegate the work to. -That technique has a risk, it could be the case that +:original_process+ was taken. To try to avoid collisions people choose some label that characterizes what they are doing and use this pattern: +That technique has a risk, it could be the case that +:original_process+ was taken. To try to avoid collisions people choose some label that characterizes what the chaining is about: ActionController::TestCase.class_eval do - def process_with_stringfied_parameters(...) - ... + def process_with_stringified_params(...) + params = Hash[*params.map {|k, v| [k, v.to_s]}.flatten] + process_without_stringified_params(action, params, session, flash, http_method) end - alias_method :process_without_stringfied_parameters, :process - alias_method :process, :process_with_stringfied_parameters + alias_method :process_without_stringified_params, :process + alias_method :process, :process_with_stringified_params end -The method +alias_method_chain+ provides a shortcut for that idiom: +The method +alias_method_chain+ provides a shortcut for that pattern: ActionController::TestCase.class_eval do - def process_with_stringfied_parameters(...) - ... + def process_with_stringified_params(...) + params = Hash[*params.map {|k, v| [k, v.to_s]}.flatten] + process_without_stringified_params(action, params, session, flash, http_method) end - alias_method_chain :process, :stringfied_parameters + alias_method_chain :process, :stringified_params end @@ -486,7 +487,7 @@ Rails uses +alias_method_chain+ all over the code base. For example validations h5. +alias_attribute+ -Model attributes have a reader, a writer, and a predicate. You can aliase a model attribute having the corresponding three methods defined for you in one shot. As in other aliasing methods, the new name is the first argument, and the old name is the second (same order than in assigments is my mnemonic): +Model attributes have a reader, a writer, and a predicate. You can aliase a model attribute having the corresponding three methods defined for you in one shot. As in other aliasing methods, the new name is the first argument, and the old name is the second (my mnemonic is they go in the same order as if you did an assignment): class User < ActiveRecord::Base -- cgit v1.2.3 From db41df2cd1a1165c6071aa5c8ca1f8ea0c744a23 Mon Sep 17 00:00:00 2001 From: Xavier Noria Date: Sun, 23 Aug 2009 00:53:32 +0200 Subject: AS guide: documents String#bytesize --- railties/guides/source/active_support_overview.textile | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/railties/guides/source/active_support_overview.textile b/railties/guides/source/active_support_overview.textile index a4e2534e2d..65ec023876 100644 --- a/railties/guides/source/active_support_overview.textile +++ b/railties/guides/source/active_support_overview.textile @@ -678,7 +678,9 @@ Symbols from Ruby 1.8.7 on respond to +to_proc+, and Active Support defines it f h3. Extensions to +String+ -... +h4. +bytesize+ + +Ruby 1.9 introduces +String#bytesize+ to obtain the length of a string in bytes. Ruby 1.8.7 defines this method as an alias for +String#size+ for forward compatibility, and Active Support does so for previous versions. h3. Extensions to +Numeric+ -- cgit v1.2.3 From 49a23ad4218e2b37eb3a77091b26eb9308246e39 Mon Sep 17 00:00:00 2001 From: Xavier Noria Date: Sun, 23 Aug 2009 01:23:56 +0200 Subject: AS guide: documents String#squish --- railties/guides/source/active_support_overview.textile | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/railties/guides/source/active_support_overview.textile b/railties/guides/source/active_support_overview.textile index 65ec023876..5e6c2030c6 100644 --- a/railties/guides/source/active_support_overview.textile +++ b/railties/guides/source/active_support_overview.textile @@ -682,6 +682,16 @@ h4. +bytesize+ Ruby 1.9 introduces +String#bytesize+ to obtain the length of a string in bytes. Ruby 1.8.7 defines this method as an alias for +String#size+ for forward compatibility, and Active Support does so for previous versions. +h4. +squish+ + +The method +String#squish+ strips leading and trailing whitespace, and substitutes runs of whitespace with a single space each: + + +" \n foo\n\r \t bar \n".squish # => "foo bar" + + +There's also the destructive version +String#squish!+. + h3. Extensions to +Numeric+ ... -- cgit v1.2.3