Changeset 9226 for trunk/activesupport/lib/active_support
- Timestamp:
- 04/05/08 03:52:58 (5 months ago)
- Files:
-
- trunk/activesupport/lib/active_support/core_ext/array/access.rb (modified) (2 diffs)
- trunk/activesupport/lib/active_support/core_ext/array/conversions.rb (modified) (2 diffs)
- trunk/activesupport/lib/active_support/core_ext/array/extract_options.rb (modified) (1 diff)
- trunk/activesupport/lib/active_support/core_ext/array/grouping.rb (modified) (2 diffs)
- trunk/activesupport/lib/active_support/core_ext/array/random_access.rb (modified) (1 diff)
- trunk/activesupport/lib/active_support/core_ext/blank.rb (modified) (1 diff)
- trunk/activesupport/lib/active_support/core_ext/class/removal.rb (modified) (1 diff)
- trunk/activesupport/lib/active_support/core_ext/date_time/conversions.rb (modified) (1 diff)
- trunk/activesupport/lib/active_support/core_ext/date/calculations.rb (modified) (1 diff)
- trunk/activesupport/lib/active_support/core_ext/integer/even_odd.rb (modified) (1 diff)
- trunk/activesupport/lib/active_support/core_ext/integer/inflections.rb (modified) (1 diff)
- trunk/activesupport/lib/active_support/core_ext/object/instance_variables.rb (modified) (1 diff)
- trunk/activesupport/lib/active_support/core_ext/string/unicode.rb (modified) (1 diff)
- trunk/activesupport/lib/active_support/core_ext/time/conversions.rb (modified) (5 diffs)
- trunk/activesupport/lib/active_support/core_ext/time/zones.rb (modified) (1 diff)
- trunk/activesupport/lib/active_support/whiny_nil.rb (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/activesupport/lib/active_support/core_ext/array/access.rb
r8224 r9226 4 4 # Makes it easier to access parts of an array. 5 5 module Access 6 # Returns the remaining of the array from the+position+.6 # Returns the tail of the array from +position+. 7 7 # 8 # Examples:9 8 # %w( a b c d ).from(0) # => %w( a b c d ) 10 9 # %w( a b c d ).from(2) # => %w( c d ) … … 14 13 end 15 14 16 # Returns the beginning of the array up to the+position+.15 # Returns the beginning of the array up to +position+. 17 16 # 18 # Examples:19 17 # %w( a b c d ).to(0) # => %w( a ) 20 18 # %w( a b c d ).to(2) # => %w( a b c ) trunk/activesupport/lib/active_support/core_ext/array/conversions.rb
r9093 r9226 31 31 end 32 32 33 # Converts an array into a string suitable for use as a URL query string, using the given <tt>key</tt> as the 34 # param name. 35 # 36 # Example: 33 # Converts an array into a string suitable for use as a URL query string, 34 # using the given +key+ as the param name. 37 35 # 38 36 # ['Rails', 'coding'].to_query('hobbies') # => "hobbies%5B%5D=Rails&hobbies%5B%5D=coding" … … 81 79 # 82 80 # Root children have as node name the one of the root singularized. 83 #84 # Example:85 81 # 86 82 # [{:foo => 1, :bar => 2}, {:baz => 3}].to_xml trunk/activesupport/lib/active_support/core_ext/array/extract_options.rb
r7217 r9226 3 3 module Array #:nodoc: 4 4 module ExtractOptions 5 # Extract options from a set of arguments. Removes and returns the last element in the array if it's a hash, otherwise returns a blank hash. 5 # Extracts options from a set of arguments. Removes and returns the last 6 # element in the array if it's a hash, otherwise returns a blank hash. 6 7 # 7 8 # def options(*args) trunk/activesupport/lib/active_support/core_ext/array/grouping.rb
r9093 r9226 5 5 module Array #:nodoc: 6 6 module Grouping 7 # Iterate over an array in groups of a certain size, padding any remaining 8 # slots with specified value (<tt>nil</tt> by default) unless it is 9 # <tt>false</tt>. 10 # 11 # Examples: 7 # Iterates over the array in groups of size +number+, padding any remaining 8 # slots with +fill_with+ unless it is +false+. 12 9 # 13 10 # %w(1 2 3 4 5 6 7).in_groups_of(3) {|g| p g} … … 43 40 end 44 41 45 # Divide the array into one or more subarrays based on a delimiting +value+42 # Divides the array into one or more subarrays based on a delimiting +value+ 46 43 # or the result of an optional block. 47 #48 # Examples:49 44 # 50 45 # [1, 2, 3, 4, 5].split(3) # => [[1, 2], [4, 5]] trunk/activesupport/lib/active_support/core_ext/array/random_access.rb
r7490 r9226 3 3 module Array #:nodoc: 4 4 module RandomAccess 5 # Return a random element from the array.5 # Returns a random element from the array. 6 6 def rand 7 7 self[Kernel.rand(length)] trunk/activesupport/lib/active_support/core_ext/blank.rb
r9093 r9226 1 1 class Object 2 2 # An object is blank if it's false, empty, or a whitespace string. 3 # For example, "", " ", nil, [], and {} are blank.3 # For example, "", " ", +nil+, [], and {} are blank. 4 4 # 5 5 # This simplifies 6 # 6 7 # if !address.nil? && !address.empty? 8 # 7 9 # to 10 # 8 11 # if !address.blank? 9 12 def blank? trunk/activesupport/lib/active_support/core_ext/class/removal.rb
r9093 r9226 1 1 class Class #:nodoc: 2 2 3 # Will unassociate the class with its subclasses as well as uninitializing the subclasses themselves.4 # >> Integer.remove_subclasses5 # => [Bignum, Fixnum]6 # >> Fixnum7 # NameError: uninitialized constant Fixnum3 # Unassociates the class with its subclasses and removes the subclasses 4 # themselves. 5 # 6 # Integer.remove_subclasses # => [Bignum, Fixnum] 7 # Fixnum # => NameError: uninitialized constant Fixnum 8 8 def remove_subclasses 9 9 Object.remove_subclasses_of(self) 10 10 end 11 11 12 # Returns a list of classes that inherit from this class in an array. 13 # Example: Integer.subclasses => ["Bignum", "Fixnum"] 12 # Returns an array with the names of the subclasses of +self+ as strings. 13 # 14 # Integer.subclasses # => ["Bignum", "Fixnum"] 14 15 def subclasses 15 16 Object.subclasses_of(self).map { |o| o.to_s } 16 17 end 17 18 18 # Allows you to remove individual subclasses or a selection of subclasses from a class without removing all of them. 19 # Removes the classes in +klasses+ from their parent module. 20 # 21 # Ordinary classes belong to some module via a constant. This method computes 22 # that constant name from the class name and removes it from the module it 23 # belongs to. 24 # 25 # Object.remove_class(Integer) # => [Integer] 26 # Integer # => NameError: uninitialized constant Integer 27 # 28 # Take into account that in general the class object could be still stored 29 # somewhere else. 30 # 31 # i = Integer # => Integer 32 # Object.remove_class(Integer) # => [Integer] 33 # Integer # => NameError: uninitialized constant Integer 34 # i.subclasses # => ["Bignum", "Fixnum"] 35 # Fixnum.superclass # => Integer 19 36 def remove_class(*klasses) 20 37 klasses.flatten.each do |klass| trunk/activesupport/lib/active_support/core_ext/date_time/conversions.rb
r9161 r9226 46 46 end 47 47 48 # Returns the utc_offsetas an +HH:MM formatted string. Examples:48 # Returns the +utc_offset+ as an +HH:MM formatted string. Examples: 49 49 # 50 50 # datetime = DateTime.civil(2000, 1, 1, 0, 0, 0, Rational(-6, 24)) trunk/activesupport/lib/active_support/core_ext/date/calculations.rb
r9093 r9226 17 17 18 18 module ClassMethods 19 # Finds yesterday's date, in the format similar to: Mon, 17 Mar 200819 # Returns a new Date representing the date 1 day ago (i.e. yesterday's date). 20 20 def yesterday 21 21 ::Date.today.yesterday 22 22 end 23 23 24 # Finds tommorrow's date, in the format similar to: Tue, 18 Mar 200824 # Returns a new Date representing the date 1 day after today (i.e. tomorrow's date). 25 25 def tomorrow 26 26 ::Date.today.tomorrow trunk/activesupport/lib/active_support/core_ext/integer/even_odd.rb
r9093 r9226 4 4 # For checking if a fixnum is even or odd. 5 5 # 6 # Examples:7 #8 6 # 1.even? # => false 9 7 # 1.odd? # => true 10 8 # 2.even? # => true 11 # 2.odd? # => false9 # 2.odd? # => false 12 10 module EvenOdd 13 11 def multiple_of?(number) trunk/activesupport/lib/active_support/core_ext/integer/inflections.rb
r9093 r9226 7 7 # Ordinalize turns a number into an ordinal string used to denote the 8 8 # position in an ordered sequence such as 1st, 2nd, 3rd, 4th. 9 #10 # Examples:11 9 # 12 10 # 1.ordinalize # => "1st" trunk/activesupport/lib/active_support/core_ext/object/instance_variables.rb
r9093 r9226 38 38 end 39 39 40 # Copies the instance variables of +object+ into self.40 # Copies the instance variables of +object+ into +self+. 41 41 # 42 42 # Instance variable names in the +exclude+ array are ignored. If +object+ trunk/activesupport/lib/active_support/core_ext/string/unicode.rb
r9093 r9226 11 11 # 12 12 # name = 'Claus MÃŒller' 13 # name.reverse #=> "rell??M sualC"14 # name.length #=> 1313 # name.reverse #=> "rell??M sualC" 14 # name.length #=> 13 15 15 # 16 # name.chars.reverse.to_s #=> "rellÃŒM sualC"17 # name.chars.length #=> 1216 # name.chars.reverse.to_s #=> "rellÃŒM sualC" 17 # name.chars.length #=> 12 18 18 # 19 19 # trunk/activesupport/lib/active_support/core_ext/time/conversions.rb
r8699 r9226 21 21 end 22 22 23 # Convert to a formatted string. See DATE_FORMATS for builtin formats.23 # Converts to a formatted string. See DATE_FORMATS for builtin formats. 24 24 # 25 25 # This method is aliased to <tt>to_s</tt>. 26 26 # 27 # ==== Examples:28 27 # time = Time.now # => Thu Jan 18 06:10:17 CST 2007 29 28 # … … 37 36 # time.to_formatted_s(:rfc822) # => "Thu, 18 Jan 2007 06:10:17 -0600" 38 37 # 39 # == Adding your own time formats to to_formatted_s38 # == Adding your own time formats to +to_formatted_s+ 40 39 # You can add your own formats to the Time::DATE_FORMATS hash. 41 40 # Use the format name as the hash key and either a strftime string … … 50 49 end 51 50 52 # Returns the utc_offset as an +HH:MM formatted string. Examples:51 # Returns the UTC offset as an +HH:MM formatted string. 53 52 # 54 53 # Time.local(2000).formatted_offset # => "-06:00" … … 58 57 end 59 58 60 # Convert a Time object to a Date, dropping hour, minute, and second precision.59 # Converts a Time object to a Date, dropping hour, minute, and second precision. 61 60 # 62 # ==== Examples 63 # my_time = Time.now 64 # # => Mon Nov 12 22:59:51 -0500 2007 61 # my_time = Time.now # => Mon Nov 12 22:59:51 -0500 2007 62 # my_time.to_date #=> Mon, 12 Nov 2007 65 63 # 66 # my_time.to_date 67 # #=> Mon, 12 Nov 2007 68 # 69 # your_time = Time.parse("1/13/2009 1:13:03 P.M.") 70 # # => Tue Jan 13 13:13:03 -0500 2009 71 # 72 # your_time.to_date 73 # # => Tue, 13 Jan 2009 64 # your_time = Time.parse("1/13/2009 1:13:03 P.M.") # => Tue Jan 13 13:13:03 -0500 2009 65 # your_time.to_date # => Tue, 13 Jan 2009 74 66 def to_date 75 67 ::Date.new(year, month, day) … … 84 76 # Converts a Time instance to a Ruby DateTime instance, preserving UTC offset. 85 77 # 86 # ==== Examples 87 # my_time = Time.now 88 # # => Mon Nov 12 23:04:21 -0500 2007 78 # my_time = Time.now # => Mon Nov 12 23:04:21 -0500 2007 79 # my_time.to_datetime # => Mon, 12 Nov 2007 23:04:21 -0500 89 80 # 90 # my_time.to_datetime 91 # # => Mon, 12 Nov 2007 23:04:21 -0500 92 # 93 # your_time = Time.parse("1/13/2009 1:13:03 P.M.") 94 # # => Tue Jan 13 13:13:03 -0500 2009 95 # 96 # your_time.to_datetime 97 # # => Tue, 13 Jan 2009 13:13:03 -0500 81 # your_time = Time.parse("1/13/2009 1:13:03 P.M.") # => Tue Jan 13 13:13:03 -0500 2009 82 # your_time.to_datetime # => Tue, 13 Jan 2009 13:13:03 -0500 98 83 def to_datetime 99 84 ::DateTime.civil(year, month, day, hour, min, sec, Rational(utc_offset, 86400)) trunk/activesupport/lib/active_support/core_ext/time/zones.rb
r9107 r9226 18 18 # Sets a global default time zone, separate from the system time zone in ENV['TZ']. 19 19 # Accepts either a Rails TimeZone object, a string that identifies a 20 # Rails TimeZone object (e.g., "Central Time (US & Canada)"), or a TZInfo::Timezone object 20 # Rails TimeZone object (e.g., "Central Time (US & Canada)"), or a TZInfo::Timezone object. 21 21 # 22 # Any Time or DateTime object can use this default time zone, via #in_time_zone. 23 # Example: 22 # Any Time or DateTime object can use this default time zone, via <tt>in_time_zone</tt>. 24 23 # 25 24 # Time.zone = 'Hawaii' # => 'Hawaii' trunk/activesupport/lib/active_support/whiny_nil.rb
r9093 r9226 1 # Extensions to nil which allow for more helpful error messages for2 # people who are new to rails.1 # Extensions to +nil+ which allow for more helpful error messages for people who 2 # are new to Rails. 3 3 # 4 # The aim is to ensure that when users pass nil to methods where that isn't 5 # appropriate, instead of NoMethodError and the name of some method used 6 # by the framework users will see a message explaining what type of object 7 # was expected. 8 4 # Ruby raises NoMethodError if you invoke a method on an object that does not 5 # respond to it: 6 # 7 # $ ruby -e nil.destroy 8 # -e:1: undefined method `destroy' for nil:NilClass (NoMethodError) 9 # 10 # With these extensions, if the method belongs to the public interface of the 11 # classes in NilClass::WHINERS the error message suggests which could be the 12 # actual intended class: 13 # 14 # $ script/runner nil.destroy 15 # ... 16 # You might have expected an instance of ActiveRecord::Base. 17 # ... 18 # 19 # NilClass#id exists in Ruby 1.8 (though it is deprecated). Since +id+ is a fundamental 20 # method of Active Record models NilClass#id is redefined as well to raise a RuntimeError 21 # and warn the user. She probably wanted a model database identifier and the 4 22 # returned by the original method could result in obscure bugs. 23 # 24 # The flag <tt>config.whiny_nils</tt> determines whether this feature is enabled. 25 # By default it is on in development and test modes, and it is off in production 26 # mode. 9 27 class NilClass 10 28 WHINERS = [::Array] … … 19 37 end 20 38 21 # Raises a RuntimeError when you attempt to call id on nil or a nil object.39 # Raises a RuntimeError when you attempt to call +id+ on +nil+. 22 40 def id 23 41 raise RuntimeError, "Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id", caller … … 29 47 end 30 48 31 # Raises a NoMethodError when you attempt to call a method on nil, or a nil object.49 # Raises a NoMethodError when you attempt to call a method on +nil+. 32 50 def raise_nil_warning_for(class_name = nil, selector = nil, with_caller = nil) 33 51 message = "You have a nil object when you didn't expect it!"