From 0289b0e7dff1c114a090f905406cc212d9e3570e Mon Sep 17 00:00:00 2001 From: gbuesing Date: Sat, 12 Apr 2008 17:31:29 -0500 Subject: Refactor TimeWithZone: don't send #since, #ago, #+, #-, #advance through method_missing --- activesupport/lib/active_support/time_with_zone.rb | 27 ++++++++++++++-------- 1 file changed, 18 insertions(+), 9 deletions(-) (limited to 'activesupport/lib') diff --git a/activesupport/lib/active_support/time_with_zone.rb b/activesupport/lib/active_support/time_with_zone.rb index 1c984f07d4..f1a2498298 100644 --- a/activesupport/lib/active_support/time_with_zone.rb +++ b/activesupport/lib/active_support/time_with_zone.rb @@ -134,7 +134,8 @@ module ActiveSupport # If wrapped #time is a DateTime, use DateTime#since instead of #+ # Otherwise, just pass on to #method_missing def +(other) - time.acts_like?(:date) ? method_missing(:since, other) : method_missing(:+, other) + result = utc.acts_like?(:date) ? utc.since(other) : utc + other + result.in_time_zone(time_zone) end # If a time-like object is passed in, compare it with #utc @@ -144,10 +145,23 @@ module ActiveSupport if other.acts_like?(:time) utc - other else - time.acts_like?(:date) ? method_missing(:ago, other) : method_missing(:-, other) + result = utc.acts_like?(:date) ? utc.ago(other) : utc - other + result.in_time_zone(time_zone) end end + def since(other) + utc.since(other).in_time_zone(time_zone) + end + + def ago(other) + utc.ago(other).in_time_zone(time_zone) + end + + def advance(options) + utc.advance(options).in_time_zone(time_zone) + end + def usec time.respond_to?(:usec) ? time.usec : 0 end @@ -208,13 +222,8 @@ module ActiveSupport # Send the missing method to time instance, and wrap result in a new TimeWithZone with the existing time_zone def method_missing(sym, *args, &block) - if %w(+ - since ago advance).include?(sym.to_s) - result = utc.__send__(sym, *args, &block) - result.acts_like?(:time) ? result.in_time_zone(time_zone) : result - else - result = time.__send__(sym, *args, &block) - result.acts_like?(:time) ? self.class.new(nil, time_zone, result) : result - end + result = time.__send__(sym, *args, &block) + result.acts_like?(:time) ? self.class.new(nil, time_zone, result) : result end private -- cgit v1.2.3 From f285b6119b9ca7f598e31c0c8518dce3e1b13386 Mon Sep 17 00:00:00 2001 From: gbuesing Date: Sat, 12 Apr 2008 17:37:50 -0500 Subject: Add #getutc alias for DateTime#utc --- activesupport/lib/active_support/core_ext/date_time/calculations.rb | 1 + 1 file changed, 1 insertion(+) (limited to 'activesupport/lib') diff --git a/activesupport/lib/active_support/core_ext/date_time/calculations.rb b/activesupport/lib/active_support/core_ext/date_time/calculations.rb index 5c351c21c6..fa444f71b1 100644 --- a/activesupport/lib/active_support/core_ext/date_time/calculations.rb +++ b/activesupport/lib/active_support/core_ext/date_time/calculations.rb @@ -86,6 +86,7 @@ module ActiveSupport #:nodoc: def utc new_offset(0) end + alias_method :getutc, :utc # Returns true if offset == 0 def utc? -- cgit v1.2.3 From 9620372a6dc7eeebdb04f1fdb7f150d29e60fc00 Mon Sep 17 00:00:00 2001 From: gbuesing Date: Sat, 12 Apr 2008 19:35:47 -0500 Subject: Time#since behaves correctly when passed a Duration. Closes #11527 [kemiller] --- .../lib/active_support/core_ext/time/calculations.rb | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) (limited to 'activesupport/lib') diff --git a/activesupport/lib/active_support/core_ext/time/calculations.rb b/activesupport/lib/active_support/core_ext/time/calculations.rb index 0bc83af709..ffbdf37789 100644 --- a/activesupport/lib/active_support/core_ext/time/calculations.rb +++ b/activesupport/lib/active_support/core_ext/time/calculations.rb @@ -88,18 +88,21 @@ module ActiveSupport #:nodoc: end # Returns a new Time representing the time a number of seconds ago, this is basically a wrapper around the Numeric extension - # Do not use this method in combination with x.months, use months_ago instead! def ago(seconds) self.since(-seconds) end # Returns a new Time representing the time a number of seconds since the instance time, this is basically a wrapper around - # the Numeric extension. Do not use this method in combination with x.months, use months_since instead! + # the Numeric extension. def since(seconds) - initial_dst = self.dst? ? 1 : 0 f = seconds.since(self) - final_dst = f.dst? ? 1 : 0 - (seconds.abs >= 86400 && initial_dst != final_dst) ? f + (initial_dst - final_dst).hours : f + if ActiveSupport::Duration === seconds + f + else + initial_dst = self.dst? ? 1 : 0 + final_dst = f.dst? ? 1 : 0 + (seconds.abs >= 86400 && initial_dst != final_dst) ? f + (initial_dst - final_dst).hours : f + end rescue self.to_datetime.since(seconds) end -- cgit v1.2.3 From 9e1d506a8cfedef2fdd605e4cbf4bf53651ad214 Mon Sep 17 00:00:00 2001 From: jweiss Date: Thu, 17 Apr 2008 12:58:31 -0500 Subject: Support options passed to ActiveSupport::Cache :mem_cache_store [#14 state:resolved] Signed-off-by: Joshua Peek --- activesupport/lib/active_support/cache/mem_cache_store.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'activesupport/lib') diff --git a/activesupport/lib/active_support/cache/mem_cache_store.rb b/activesupport/lib/active_support/cache/mem_cache_store.rb index 88e324aa3a..9b787702b2 100644 --- a/activesupport/lib/active_support/cache/mem_cache_store.rb +++ b/activesupport/lib/active_support/cache/mem_cache_store.rb @@ -15,9 +15,10 @@ module ActiveSupport def initialize(*addresses) addresses = addresses.flatten + options = addresses.extract_options! addresses = ["localhost"] if addresses.empty? @addresses = addresses - @data = MemCache.new(addresses) + @data = MemCache.new(addresses, options) end def read(key, options = nil) -- cgit v1.2.3 From cf04e621270bb2e5e9e7971d2c59e73d6797482d Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Thu, 17 Apr 2008 23:30:01 -0500 Subject: Tidy up ActiveSupport::Callbacks::CallbackChain instance API. --- activesupport/lib/active_support/callbacks.rb | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) (limited to 'activesupport/lib') diff --git a/activesupport/lib/active_support/callbacks.rb b/activesupport/lib/active_support/callbacks.rb index 329cc2fdc7..282b6cbcbc 100644 --- a/activesupport/lib/active_support/callbacks.rb +++ b/activesupport/lib/active_support/callbacks.rb @@ -96,17 +96,26 @@ module ActiveSupport end end - def find_callback(callback, &block) + def |(chain) + if chain.is_a?(Callback) + if found_callback = find(chain) + index = index(found_callback) + self[index] = chain + else + self << chain + end + else + chain.each { |callback| self | callback } + end + self + end + + def find(callback, &block) select { |c| c == callback && (!block_given? || yield(c)) }.first end - def replace_or_append_callback(callback) - if found_callback = find_callback(callback) - index = index(found_callback) - self[index] = callback - else - self << callback - end + def delete(callback) + super(find(callback)) end private -- cgit v1.2.3 From a4c15303cbc96fffd66c68abdeebe73d8883e5ab Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Fri, 18 Apr 2008 14:44:55 -0500 Subject: Slight optimization to CallbackChain#union and delete. --- activesupport/lib/active_support/callbacks.rb | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) (limited to 'activesupport/lib') diff --git a/activesupport/lib/active_support/callbacks.rb b/activesupport/lib/active_support/callbacks.rb index 282b6cbcbc..5c51237049 100644 --- a/activesupport/lib/active_support/callbacks.rb +++ b/activesupport/lib/active_support/callbacks.rb @@ -97,15 +97,14 @@ module ActiveSupport end def |(chain) - if chain.is_a?(Callback) - if found_callback = find(chain) - index = index(found_callback) + if chain.is_a?(CallbackChain) + chain.each { |callback| self | callback } + elsif chain.is_a?(Callback) + if index = index(chain) self[index] = chain else self << chain end - else - chain.each { |callback| self | callback } end self end @@ -115,7 +114,7 @@ module ActiveSupport end def delete(callback) - super(find(callback)) + super(callback.is_a?(Callback) ? callback : find(callback)) end private -- cgit v1.2.3 From 46ab7422d9ebac0d529f71a3a7c2feaf0b9d5dbd Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Sun, 20 Apr 2008 11:45:44 -0500 Subject: Use define_callbacks helper for ActiveRecord validations. --- activesupport/lib/active_support/callbacks.rb | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'activesupport/lib') diff --git a/activesupport/lib/active_support/callbacks.rb b/activesupport/lib/active_support/callbacks.rb index 5c51237049..9c59b7ac76 100644 --- a/activesupport/lib/active_support/callbacks.rb +++ b/activesupport/lib/active_support/callbacks.rb @@ -99,8 +99,8 @@ module ActiveSupport def |(chain) if chain.is_a?(CallbackChain) chain.each { |callback| self | callback } - elsif chain.is_a?(Callback) - if index = index(chain) + else + if (found_callback = find(chain)) && (index = index(chain)) self[index] = chain else self << chain @@ -224,8 +224,8 @@ module ActiveSupport end end - # Runs all the callbacks defined for the given options. - # + # Runs all the callbacks defined for the given options. + # # If a block is given it will be called after each callback receiving as arguments: # # * the result from the callback @@ -236,31 +236,31 @@ module ActiveSupport # Example: # class Storage # include ActiveSupport::Callbacks - # + # # define_callbacks :before_save, :after_save # end - # + # # class ConfigStorage < Storage # before_save :pass # before_save :pass # before_save :stop # before_save :pass - # + # # def pass # puts "pass" # end - # + # # def stop # puts "stop" # return false # end - # + # # def save # result = run_callbacks(:before_save) { |result, object| result == false } # puts "- save" if result # end # end - # + # # config = ConfigStorage.new # config.save # -- cgit v1.2.3