02. 액티브서포트
액티브서포트
Active Support is a collection of useful classes and default libraries extensions which were considered useful for Ruby on Rails Applications. (wikipedia)
ActiveSupport::CoreExtensions::Date::Calculations
Time#end_of_day
Returns the current date with the time set to 11:59:59 PM.
Time#end_of_week
Returns the end of the week (Sunday 11:59:59 PM).
Time#end_of_quarter
Returns a Date object representing the end of the trimester. In other words, it returns the last day of either march, june, september or december, depending on the current date.
Time#end_of_year
Returns December 31 at 11:59:59 PM
Time#in_time_zone
This method is similar to Time#localtime, except by the fact that it uses Time.zone instead of the underlying operating system's timezone. You can pass either TimeZone or String as a parameter. Look at some examples:
- Time.zone = 'Hawaii'
Time.utc(2000).in_time_zone
# => Fri, 31 Dec 1999 14:00:00 HST -10:00
Time.utc(2000).in_time_zone('Alaska')
# => Fri, 31 Dec 1999 15:00:00 AKST -09:00
Time#days_in_month
A bug in the method days_in_month was fixed, which returned the wrong number of days in february when the year was was not specified.
Changes comprise in using the current year as the default value when not specifying the year in method call. Suppose you were in a leap year. Look the following example:
- Loading development environment (Rails 2.0.2)
>> Time.days_in_month(2)
=> 28
Loading development environment (Rails 2.1.0)
>> Time.days_in_month(2)
=> 29
DateTime#to_f
DateTime class received a new method called to_f which returns the date as a type float number representing the number of seconds since the Unix epoch (number of seconds since january 1st, 1970, midnight).
Date.current
Date class received a new method called current which must now be used instead of Date.today, because it considers the timezone set in config.time_zone in case it is set, returning a Time.zone.today. If it is not set, then it returns a Date.today.
Fragment_exist?
Two new methods were added to cache_store: fragment_exist? and exist?.
The method fragment_exist? does exactly what you would expect it to. It verifies if a cached fragment informed by one key exists. Basically replacing the famous:
- read_fragment(path).nil?
exist? method was added to cache_store, while fragment_exist? is a helper which you could use inside your controller.
UTC or GMT?
Until now Rails has been using the UTC acronym for most things. When the method to_s from TimeZone is called, it will print GMT not UTC. This is due to the fact that the GMT acronym is the most common among end users.
If you take a look in Windows control panel where you can choose timezone, you'll notice the acronym used is GMT. Google and Yahoo also have been using GMT within their products.
- TimeZone['Moscow'].to_s #=> "(GMT+03:00) Moscow"
JSON escape
json_escape method works like html_escape. It's very useful when we need to show JSON strings in a HTML page, for example, in a documentation process.
- puts json_escape("is a > 0 & a < 10?")
# => is a \u003E 0 \u0026 a \u003C 10?
We can also use the shortcut j when in ERB:
- <%= j @person.to_json %>
If you want all JSON code to be 'escaped' by default, include the following line in your environment.rb file:
- ActiveSupport.escape_html_entities_in_json = true
Mem_cache_store now accepts options
The inclusion of Memcache-Client inside ActiveSupport::Cache made things easier than ever, but it also took away its flexibility in not allowing us to customize nothing more than the IP of the memcached server.
Jonathan Weiss made a patch which was included in Rails allowing extra options like these:
- ActiveSupport::Cache.lookup_store :mem_cache_store, "localhost"
ActiveSupport::Cache.lookup_store :mem_cache_store, "localhost", '192.168.1.1',
:namespace => 'foo'
or
- config.action_controller.fragment_cache_store = :mem_cache_store, 'localhost',
{:compression => true, :debug => true, :namespace =>'foo'}
Time.current
A new method for Time class. The current method's return depends on config.time_zone, if it was specified before, the method will return a Time.zone.now, otherwise will be a Time.now.
- # return value depends on config.time_zone
Time.current
since and ago methods also had their returning values changed, returning a TimeWithZone in case config.time_zone as specified.
It makes the Time.current method as new default method to get the actual time, replacing the Time.now (which keeps existing, but it doesn't consider the specified timezone).
The datetime_select methods, select_datetime and select_time also have been updated to have their default returning as Time.current.
Removing whitespaces with squish method
Two new methods added to the String object, squish and squish!.
These methods do the same as strip method. It removes white spaces from the beginning and the end of the text. It also removes unused white-spaces (multiple white-spaces) from the middle of the text Look the example:
- “ A text full of spaces “.strip
#=> “A text full of spaces”
“ A text full of spaces “.squish
#=> “A text full of spaces”
History
Last edited on 06/20/2008 00:18 by deepblue
Comments (0)