diff options
author | Sven Fuchs <svenfuchs@artweb-design.de> | 2008-07-16 03:41:11 +0200 |
---|---|---|
committer | Sven Fuchs <svenfuchs@artweb-design.de> | 2008-07-16 03:41:11 +0200 |
commit | 931f366ffcacc0444fcca2fb2e2b44644db9642f (patch) | |
tree | 4c056de1273d23e2b8494cfe452caaeb98ade820 /activesupport/lib | |
parent | 8691e255402b27eae594530001227fc05416a00c (diff) | |
parent | fbef982e4b906b879240a35a1ecff447007da6b2 (diff) | |
download | rails-931f366ffcacc0444fcca2fb2e2b44644db9642f.tar.gz rails-931f366ffcacc0444fcca2fb2e2b44644db9642f.tar.bz2 rails-931f366ffcacc0444fcca2fb2e2b44644db9642f.zip |
merge forward to current rails/master
Diffstat (limited to 'activesupport/lib')
19 files changed, 275 insertions, 65 deletions
diff --git a/activesupport/lib/active_support.rb b/activesupport/lib/active_support.rb index de50aafe16..1df911a3f2 100644 --- a/activesupport/lib/active_support.rb +++ b/activesupport/lib/active_support.rb @@ -43,6 +43,7 @@ require 'active_support/ordered_hash' require 'active_support/ordered_options' require 'active_support/option_merger' +require 'active_support/memoizable' require 'active_support/string_inquirer' require 'active_support/values/time_zone' diff --git a/activesupport/lib/active_support/cache.rb b/activesupport/lib/active_support/cache.rb index 2f1143e610..3e3dc18263 100644 --- a/activesupport/lib/active_support/cache.rb +++ b/activesupport/lib/active_support/cache.rb @@ -19,19 +19,19 @@ module ActiveSupport def self.expand_cache_key(key, namespace = nil) expanded_cache_key = namespace ? "#{namespace}/" : "" - + if ENV["RAILS_CACHE_ID"] || ENV["RAILS_APP_VERSION"] expanded_cache_key << "#{ENV["RAILS_CACHE_ID"] || ENV["RAILS_APP_VERSION"]}/" end expanded_cache_key << case - when key.respond_to?(:cache_key) - key.cache_key - when key.is_a?(Array) - key.collect { |element| expand_cache_key(element) }.to_param - when key.respond_to?(:to_param) - key.to_param - end + when key.respond_to?(:cache_key) + key.cache_key + when key.is_a?(Array) + key.collect { |element| expand_cache_key(element) }.to_param + when key + key.to_param + end.to_s expanded_cache_key end diff --git a/activesupport/lib/active_support/core_ext/array/conversions.rb b/activesupport/lib/active_support/core_ext/array/conversions.rb index 59dc96754f..e67b719ddb 100644 --- a/activesupport/lib/active_support/core_ext/array/conversions.rb +++ b/activesupport/lib/active_support/core_ext/array/conversions.rb @@ -30,7 +30,7 @@ module ActiveSupport #:nodoc: # Calls <tt>to_param</tt> on all its elements and joins the result with # slashes. This is used by <tt>url_for</tt> in Action Pack. def to_param - map(&:to_param).join '/' + collect { |e| e.to_param }.join '/' end # Converts an array into a string suitable for use as a URL query string, @@ -38,7 +38,8 @@ module ActiveSupport #:nodoc: # # ['Rails', 'coding'].to_query('hobbies') # => "hobbies%5B%5D=Rails&hobbies%5B%5D=coding" def to_query(key) - collect { |value| value.to_query("#{key}[]") } * '&' + prefix = "#{key}[]" + collect { |value| value.to_query(prefix) }.join '&' end def self.included(base) #:nodoc: diff --git a/activesupport/lib/active_support/core_ext/array/grouping.rb b/activesupport/lib/active_support/core_ext/array/grouping.rb index 767acc4e07..df37afb053 100644 --- a/activesupport/lib/active_support/core_ext/array/grouping.rb +++ b/activesupport/lib/active_support/core_ext/array/grouping.rb @@ -4,8 +4,8 @@ module ActiveSupport #:nodoc: module CoreExtensions #:nodoc: module Array #:nodoc: module Grouping - # Iterates over the array in groups of size +number+, padding any remaining - # slots with +fill_with+ unless it is +false+. + # Splits or iterates over the array in groups of size +number+, + # padding any remaining slots with +fill_with+ unless it is +false+. # # %w(1 2 3 4 5 6 7).in_groups_of(3) {|g| p g} # ["1", "2", "3"] @@ -39,6 +39,49 @@ module ActiveSupport #:nodoc: end end + # Splits or iterates over the array in +number+ of groups, padding any + # remaining slots with +fill_with+ unless it is +false+. + # + # %w(1 2 3 4 5 6 7 8 9 10).in_groups(3) {|g| p g} + # ["1", "2", "3", "4"] + # ["5", "6", "7", nil] + # ["8", "9", "10", nil] + # + # %w(1 2 3 4 5 6 7).in_groups(3, ' ') {|g| p g} + # ["1", "2", "3"] + # ["4", "5", " "] + # ["6", "7", " "] + # + # %w(1 2 3 4 5 6 7).in_groups(3, false) {|g| p g} + # ["1", "2", "3"] + # ["4", "5"] + # ["6", "7"] + def in_groups(number, fill_with = nil) + # size / number gives minor group size; + # size % number gives how many objects need extra accomodation; + # each group hold either division or division + 1 items. + division = size / number + modulo = size % number + + # create a new array avoiding dup + groups = [] + start = 0 + + number.times do |index| + length = division + (modulo > 0 && modulo > index ? 1 : 0) + padding = fill_with != false && + modulo > 0 && length == division ? 1 : 0 + groups << slice(start, length).concat([fill_with] * padding) + start += length + end + + if block_given? + groups.each{|g| yield(g) } + else + groups + end + end + # Divides the array into one or more subarrays based on a delimiting +value+ # or the result of an optional block. # diff --git a/activesupport/lib/active_support/core_ext/enumerable.rb b/activesupport/lib/active_support/core_ext/enumerable.rb index 9647797ec2..e451e9933a 100644 --- a/activesupport/lib/active_support/core_ext/enumerable.rb +++ b/activesupport/lib/active_support/core_ext/enumerable.rb @@ -79,7 +79,9 @@ module Enumerable end # Returns true if the collection has more than 1 element. Functionally equivalent to collection.size > 1. - def many? + # Works with a block too ala any?, so people.many? { |p| p.age > 26 } # => returns true if more than 1 person is over 26. + def many?(&block) + size = block_given? ? select(&block).size : self.size size > 1 end end diff --git a/activesupport/lib/active_support/core_ext/module/introspection.rb b/activesupport/lib/active_support/core_ext/module/introspection.rb index 40bbebb7c4..45f3e4bf5c 100644 --- a/activesupport/lib/active_support/core_ext/module/introspection.rb +++ b/activesupport/lib/active_support/core_ext/module/introspection.rb @@ -1,4 +1,14 @@ class Module + # Returns the name of the module containing this one. + # + # p M::N.parent_name # => "M" + def parent_name + unless defined? @parent_name + @parent_name = name =~ /::[^:]+\Z/ ? $`.freeze : nil + end + @parent_name + end + # Returns the module which contains this one according to its name. # # module M @@ -16,8 +26,7 @@ class Module # p Module.new.parent # => Object # def parent - parent_name = name.split('::')[0..-2] * '::' - parent_name.empty? ? Object : parent_name.constantize + parent_name ? parent_name.constantize : Object end # Returns all the parents of this module according to its name, ordered from @@ -35,10 +44,12 @@ class Module # def parents parents = [] - parts = name.split('::')[0..-2] - until parts.empty? - parents << (parts * '::').constantize - parts.pop + if parent_name + parts = parent_name.split('::') + until parts.empty? + parents << (parts * '::').constantize + parts.pop + end end parents << Object unless parents.include? Object parents @@ -70,6 +81,6 @@ class Module # Returns the names of the constants defined locally rather than the # constants themselves. See <tt>local_constants</tt>. def local_constant_names - local_constants.map(&:to_s) + local_constants.map { |c| c.to_s } end end diff --git a/activesupport/lib/active_support/core_ext/module/model_naming.rb b/activesupport/lib/active_support/core_ext/module/model_naming.rb index 26e76ab556..5518f5417b 100644 --- a/activesupport/lib/active_support/core_ext/module/model_naming.rb +++ b/activesupport/lib/active_support/core_ext/module/model_naming.rb @@ -1,12 +1,13 @@ module ActiveSupport class ModelName < String - attr_reader :singular, :plural, :partial_path + attr_reader :singular, :plural, :cache_key, :partial_path def initialize(name) super @singular = underscore.tr('/', '_').freeze @plural = @singular.pluralize.freeze - @partial_path = "#{tableize}/#{demodulize.underscore}".freeze + @cache_key = tableize.freeze + @partial_path = "#{@cache_key}/#{demodulize.underscore}".freeze end end diff --git a/activesupport/lib/active_support/core_ext/object/instance_variables.rb b/activesupport/lib/active_support/core_ext/object/instance_variables.rb index 9f1d4ed2aa..4ecaab3bbb 100644 --- a/activesupport/lib/active_support/core_ext/object/instance_variables.rb +++ b/activesupport/lib/active_support/core_ext/object/instance_variables.rb @@ -35,7 +35,7 @@ class Object # C.new(0, 1).instance_variable_names # => ["@y", "@x"] if RUBY_VERSION >= '1.9' def instance_variable_names - instance_variables.map(&:to_s) + instance_variables.map { |var| var.to_s } end else alias_method :instance_variable_names, :instance_variables diff --git a/activesupport/lib/active_support/core_ext/test.rb b/activesupport/lib/active_support/core_ext/test.rb deleted file mode 100644 index c0b19bdc58..0000000000 --- a/activesupport/lib/active_support/core_ext/test.rb +++ /dev/null @@ -1 +0,0 @@ -require 'active_support/core_ext/test/unit/assertions' diff --git a/activesupport/lib/active_support/dependencies.rb b/activesupport/lib/active_support/dependencies.rb index 7a8c4d0326..2f3fa72bb4 100644 --- a/activesupport/lib/active_support/dependencies.rb +++ b/activesupport/lib/active_support/dependencies.rb @@ -299,7 +299,7 @@ module ActiveSupport #:nodoc: # Will the provided constant descriptor be unloaded? def will_unload?(const_desc) - autoloaded?(desc) || + autoloaded?(const_desc) || explicitly_unloadable_constants.include?(to_constant_name(const_desc)) end @@ -387,7 +387,7 @@ module ActiveSupport #:nodoc: ensure # Remove the stack frames that we added. if defined?(watch_frames) && ! watch_frames.blank? - frame_ids = watch_frames.collect(&:object_id) + frame_ids = watch_frames.collect { |frame| frame.object_id } constant_watch_stack.delete_if do |watch_frame| frame_ids.include? watch_frame.object_id end @@ -437,7 +437,7 @@ module ActiveSupport #:nodoc: protected def log_call(*args) if defined?(RAILS_DEFAULT_LOGGER) && RAILS_DEFAULT_LOGGER && log_activity - arg_str = args.collect(&:inspect) * ', ' + arg_str = args.collect { |arg| arg.inspect } * ', ' /in `([a-z_\?\!]+)'/ =~ caller(1).first selector = $1 || '<unknown>' log "called #{selector}(#{arg_str})" diff --git a/activesupport/lib/active_support/inflector.rb b/activesupport/lib/active_support/inflector.rb index 47bd6e1767..6651569d33 100644 --- a/activesupport/lib/active_support/inflector.rb +++ b/activesupport/lib/active_support/inflector.rb @@ -30,10 +30,10 @@ module ActiveSupport class Inflections include Singleton - attr_reader :plurals, :singulars, :uncountables + attr_reader :plurals, :singulars, :uncountables, :humans def initialize - @plurals, @singulars, @uncountables = [], [], [] + @plurals, @singulars, @uncountables, @humans = [], [], [], [] end # Specifies a new pluralization rule and its replacement. The rule can either be a string or a regular expression. @@ -76,9 +76,20 @@ module ActiveSupport (@uncountables << words).flatten! end + # Specifies a humanized form of a string by a regular expression rule or by a string mapping. + # When using a regular expression based replacement, the normal humanize formatting is called after the replacement. + # When a string is used, the human form should be specified as desired (example: 'The name', not 'the_name') + # + # Examples: + # human /_cnt$/i, '\1_count' + # human "legacy_col_person_name", "Name" + def human(rule, replacement) + @humans.insert(0, [rule, replacement]) + end + # Clears the loaded inflections within a given scope (default is <tt>:all</tt>). # Give the scope as a symbol of the inflection type, the options are: <tt>:plurals</tt>, - # <tt>:singulars</tt>, <tt>:uncountables</tt>. + # <tt>:singulars</tt>, <tt>:uncountables</tt>, <tt>:humans</tt>. # # Examples: # clear :all @@ -209,7 +220,10 @@ module ActiveSupport # "employee_salary" # => "Employee salary" # "author_id" # => "Author" def humanize(lower_case_and_underscored_word) - lower_case_and_underscored_word.to_s.gsub(/_id$/, "").gsub(/_/, " ").capitalize + result = lower_case_and_underscored_word.to_s.dup + + inflections.humans.each { |(rule, replacement)| break if result.gsub!(rule, replacement) } + result.gsub(/_id$/, "").gsub(/_/, " ").capitalize end # Removes the module part from the expression in the string. @@ -307,4 +321,9 @@ module ActiveSupport end end +# in case active_support/inflector is required without the rest of active_support require 'active_support/inflections' +require 'active_support/core_ext/string/inflections' +unless String.included_modules.include?(ActiveSupport::CoreExtensions::String::Inflections) + String.send :include, ActiveSupport::CoreExtensions::String::Inflections +end
\ No newline at end of file diff --git a/activesupport/lib/active_support/json/encoders/date_time.rb b/activesupport/lib/active_support/json/encoders/date_time.rb index d41c3e9786..a4a5efbfb1 100644 --- a/activesupport/lib/active_support/json/encoders/date_time.rb +++ b/activesupport/lib/active_support/json/encoders/date_time.rb @@ -8,7 +8,7 @@ class DateTime if ActiveSupport.use_standard_json_time_format xmlschema.inspect else - %("#{strftime("%Y/%m/%d %H:%M:%S %z")}") + strftime('"%Y/%m/%d %H:%M:%S %z"') end end end diff --git a/activesupport/lib/active_support/memoizable.rb b/activesupport/lib/active_support/memoizable.rb new file mode 100644 index 0000000000..d06250171a --- /dev/null +++ b/activesupport/lib/active_support/memoizable.rb @@ -0,0 +1,35 @@ +module ActiveSupport + module Memoizable + def self.included(base) #:nodoc: + base.extend(ClassMethods) + end + + module ClassMethods + def memoize(symbol) + original_method = "_unmemoized_#{symbol}" + memoized_ivar = "@_memoized_#{symbol}" + raise "Already memoized #{symbol}" if instance_methods.map(&:to_s).include?(original_method) + + alias_method original_method, symbol + class_eval <<-EOS, __FILE__, __LINE__ + def #{symbol} + if defined? #{memoized_ivar} + #{memoized_ivar} + else + #{memoized_ivar} = #{original_method} + end + end + EOS + end + end + + def freeze + methods.each do |method| + if m = method.to_s.match(/\A_unmemoized_(.*)/) + send(m[1]).freeze + end + end + super + end + end +end diff --git a/activesupport/lib/active_support/test_case.rb b/activesupport/lib/active_support/test_case.rb index 2fd02d5313..0f531b0c79 100644 --- a/activesupport/lib/active_support/test_case.rb +++ b/activesupport/lib/active_support/test_case.rb @@ -1,11 +1,7 @@ require 'test/unit/testcase' -require 'active_support/testing/setup_and_teardown' require 'active_support/testing/default' +require 'active_support/testing/core_ext/test' -# TODO: move to core_ext -class Test::Unit::TestCase #:nodoc: - include ActiveSupport::Testing::SetupAndTeardown -end module ActiveSupport class TestCase < Test::Unit::TestCase diff --git a/activesupport/lib/active_support/testing/core_ext/test.rb b/activesupport/lib/active_support/testing/core_ext/test.rb new file mode 100644 index 0000000000..d3f38f0bc7 --- /dev/null +++ b/activesupport/lib/active_support/testing/core_ext/test.rb @@ -0,0 +1,6 @@ +require 'active_support/testing/core_ext/test/unit/assertions' +require 'active_support/testing/setup_and_teardown' + +class Test::Unit::TestCase #:nodoc: + include ActiveSupport::Testing::SetupAndTeardown +end
\ No newline at end of file diff --git a/activesupport/lib/active_support/core_ext/test/unit/assertions.rb b/activesupport/lib/active_support/testing/core_ext/test/unit/assertions.rb index 77fe325fb4..70a44eab8c 100644 --- a/activesupport/lib/active_support/core_ext/test/unit/assertions.rb +++ b/activesupport/lib/active_support/testing/core_ext/test/unit/assertions.rb @@ -1,9 +1,10 @@ -module Test - module Unit +require 'test/unit/assertions' +module Test + module Unit #-- # FIXME: no Proc#binding in Ruby 2, must change this API #++ - module Assertions + module Assertions # Test numeric difference between the return value of an expression as a result of what is evaluated # in the yielded block. # diff --git a/activesupport/lib/active_support/testing/performance.rb b/activesupport/lib/active_support/testing/performance.rb index 24936029a2..5f2027eb3b 100644 --- a/activesupport/lib/active_support/testing/performance.rb +++ b/activesupport/lib/active_support/testing/performance.rb @@ -12,13 +12,13 @@ module ActiveSupport if benchmark = ARGV.include?('--benchmark') # HAX for rake test { :benchmark => true, :runs => 10, - :metrics => [:process_time, :memory, :objects], + :metrics => [:process_time, :memory, :objects, :gc_runs, :gc_time], :output => 'tmp/performance' } else { :benchmark => false, :runs => 1, :min_percent => 0.02, - :metrics => [:wall_time, :memory, :objects], + :metrics => [:process_time, :memory, :objects], :formats => [:flat, :graph_html, :call_tree], :output => 'tmp/performance' } end @@ -44,7 +44,7 @@ module ActiveSupport run_profile(klass.new) result.add_run else - $stderr.puts "Skipping unknown metric #{metric_name.inspect}. Expected :process_time, :wall_time, :cpu_time, :memory, or :objects." + $stderr.puts '%20s: unsupported' % metric_name.to_s end end @@ -72,9 +72,13 @@ module ActiveSupport protected def run_warmup + 5.times { GC.start } + time = Metrics::Time.new run_test(time, :benchmark) puts "%s (%s warmup)" % [full_test_name, time.format(time.total)] + + 5.times { GC.start } end def run_profile(metric) @@ -95,8 +99,13 @@ module ActiveSupport def report rate = @total / profile_options[:runs] - '%20s: %s/run' % [@metric.name, @metric.format(rate)] + '%20s: %s' % [@metric.name, @metric.format(rate)] end + + protected + def output_filename + "#{profile_options[:output]}/#{full_test_name}_#{@metric.name}" + end end class Benchmarker < Performer @@ -107,9 +116,9 @@ module ActiveSupport def record avg = @metric.total / profile_options[:runs].to_i - data = [full_test_name, @metric.name, avg, Time.now.utc.xmlschema] * ',' + now = Time.now.utc.xmlschema with_output_file do |file| - file.puts "#{data},#{environment}" + file.puts "#{avg},#{now},#{environment}" end end @@ -134,10 +143,10 @@ module ActiveSupport end protected - HEADER = 'test,metric,measurement,created_at,app,rails,ruby,platform' + HEADER = 'measurement,created_at,app,rails,ruby,platform' def with_output_file - fname = "#{profile_options[:output]}/benchmarks.csv" + fname = output_filename if new = !File.exist?(fname) FileUtils.mkdir_p(File.dirname(fname)) @@ -148,6 +157,10 @@ module ActiveSupport yield file end end + + def output_filename + "#{super}.csv" + end end class Profiler < Performer @@ -183,7 +196,7 @@ module ActiveSupport else printer_class.name.sub(/Printer$/, '').underscore end - "#{profile_options[:output]}/#{full_test_name}_#{@metric.name}_#{suffix}" + "#{super()}_#{suffix}" end end @@ -210,6 +223,10 @@ module ActiveSupport self.class::Mode end + def measure + 0 + end + def benchmark with_gc_stats do before = measure @@ -271,7 +288,7 @@ module ActiveSupport end class CpuTime < Time - Mode = RubyProf::CPU_TIME + Mode = RubyProf::CPU_TIME if RubyProf.const_defined?(:CPU_TIME) def initialize(*args) # FIXME: yeah my CPU is 2.33 GHz @@ -285,16 +302,27 @@ module ActiveSupport end class Memory < Base - Mode = RubyProf::MEMORY + Mode = RubyProf::MEMORY if RubyProf.const_defined?(:MEMORY) + # ruby-prof wrapper if RubyProf.respond_to?(:measure_memory) def measure RubyProf.measure_memory / 1024.0 end + + # Ruby 1.8 + adymo patch elsif GC.respond_to?(:allocated_size) def measure GC.allocated_size / 1024.0 end + + # Ruby 1.8 + lloyd patch + elsif GC.respond_to?(:heap_info) + def measure + GC.heap_info['heap_current_memory'] / 1024.0 + end + + # Ruby 1.9 unpatched elsif GC.respond_to?(:malloc_allocated_size) def measure GC.malloc_allocated_size / 1024.0 @@ -307,7 +335,7 @@ module ActiveSupport end class Objects < Base - Mode = RubyProf::ALLOCATIONS + Mode = RubyProf::ALLOCATIONS if RubyProf.const_defined?(:ALLOCATIONS) if RubyProf.respond_to?(:measure_allocations) def measure @@ -323,6 +351,46 @@ module ActiveSupport measurement.to_i.to_s end end + + class GcRuns < Base + Mode = RubyProf::GC_RUNS if RubyProf.const_defined?(:GC_RUNS) + + if RubyProf.respond_to?(:measure_gc_runs) + def measure + RubyProf.measure_gc_runs + end + elsif GC.respond_to?(:collections) + def measure + GC.collections + end + elsif GC.respond_to?(:heap_info) + def measure + GC.heap_info['num_gc_passes'] + end + end + + def format(measurement) + measurement.to_i.to_s + end + end + + class GcTime < Base + Mode = RubyProf::GC_TIME if RubyProf.const_defined?(:GC_TIME) + + if RubyProf.respond_to?(:measure_gc_time) + def measure + RubyProf.measure_gc_time + end + elsif GC.respond_to?(:time) + def measure + GC.time + end + end + + def format(measurement) + '%d ms' % (measurement / 1000) + end + end end end end diff --git a/activesupport/lib/active_support/time_with_zone.rb b/activesupport/lib/active_support/time_with_zone.rb index bfc5b16039..e85bfe9b2e 100644 --- a/activesupport/lib/active_support/time_with_zone.rb +++ b/activesupport/lib/active_support/time_with_zone.rb @@ -159,19 +159,24 @@ module ActiveSupport utc == other end - # If wrapped +time+ is a DateTime, use DateTime#since instead of <tt>+</tt>. - # Otherwise, just pass on to +method_missing+. def +(other) - result = utc.acts_like?(:date) ? utc.since(other) : utc + other rescue utc.since(other) - result.in_time_zone(time_zone) + # If we're adding a Duration of variable length (i.e., years, months, days), move forward from #time, + # otherwise move forward from #utc, for accuracy when moving across DST boundaries + if duration_of_variable_length?(other) + method_missing(:+, other) + else + result = utc.acts_like?(:date) ? utc.since(other) : utc + other rescue utc.since(other) + result.in_time_zone(time_zone) + end end - - # If a time-like object is passed in, compare it with +utc+. - # Else if wrapped +time+ is a DateTime, use DateTime#ago instead of DateTime#-. - # Otherwise, just pass on to +method_missing+. + def -(other) + # If we're subtracting a Duration of variable length (i.e., years, months, days), move backwards from #time, + # otherwise move backwards #utc, for accuracy when moving across DST boundaries if other.acts_like?(:time) utc - other + elsif duration_of_variable_length?(other) + method_missing(:-, other) else result = utc.acts_like?(:date) ? utc.ago(other) : utc - other rescue utc.ago(other) result.in_time_zone(time_zone) @@ -179,15 +184,27 @@ module ActiveSupport end def since(other) - utc.since(other).in_time_zone(time_zone) + # If we're adding a Duration of variable length (i.e., years, months, days), move forward from #time, + # otherwise move forward from #utc, for accuracy when moving across DST boundaries + if duration_of_variable_length?(other) + method_missing(:since, other) + else + utc.since(other).in_time_zone(time_zone) + end end def ago(other) - utc.ago(other).in_time_zone(time_zone) + since(-other) end - + def advance(options) - utc.advance(options).in_time_zone(time_zone) + # If we're advancing a value of variable length (i.e., years, weeks, months, days), advance from #time, + # otherwise advance from #utc, for accuracy when moving across DST boundaries + if options.detect {|k,v| [:years, :weeks, :months, :days].include? k} + method_missing(:advance, options) + else + utc.advance(options).in_time_zone(time_zone) + end end %w(year mon month day mday hour min sec).each do |method_name| @@ -246,7 +263,7 @@ module ActiveSupport end def marshal_load(variables) - initialize(variables[0], ::Time.send!(:get_zone, variables[1]), variables[2]) + initialize(variables[0].utc, ::Time.send!(:get_zone, variables[1]), variables[2].utc) end # Ensure proxy class responds to all methods that underlying time instance responds to. @@ -279,5 +296,9 @@ module ActiveSupport def transfer_time_values_to_utc_constructor(time) ::Time.utc_time(time.year, time.month, time.day, time.hour, time.min, time.sec, time.respond_to?(:usec) ? time.usec : 0) end + + def duration_of_variable_length?(obj) + ActiveSupport::Duration === obj && obj.parts.flatten.detect {|p| [:years, :months, :days].include? p } + end end end diff --git a/activesupport/lib/active_support/values/time_zone.rb b/activesupport/lib/active_support/values/time_zone.rb index 5b2d42aa3c..788d40bfa8 100644 --- a/activesupport/lib/active_support/values/time_zone.rb +++ b/activesupport/lib/active_support/values/time_zone.rb @@ -201,6 +201,12 @@ module ActiveSupport result end + # Compare #name and TZInfo identifier to a supplied regexp, returning true + # if a match is found. + def =~(re) + return true if name =~ re || MAPPING[name] =~ re + end + # Returns a textual representation of this time zone. def to_s "(GMT#{formatted_offset}) #{name}" |