From f4f6e57e8c2a446a4a600576f0caf0fb8921ba13 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Tue, 15 Jul 2008 21:24:00 -0500 Subject: Added Object#metaclass --- activesupport/lib/active_support/core_ext/object.rb | 1 + activesupport/lib/active_support/core_ext/object/metaclass.rb | 8 ++++++++ 2 files changed, 9 insertions(+) create mode 100644 activesupport/lib/active_support/core_ext/object/metaclass.rb (limited to 'activesupport/lib/active_support') diff --git a/activesupport/lib/active_support/core_ext/object.rb b/activesupport/lib/active_support/core_ext/object.rb index bbc7d81672..0796a7b710 100644 --- a/activesupport/lib/active_support/core_ext/object.rb +++ b/activesupport/lib/active_support/core_ext/object.rb @@ -1,4 +1,5 @@ require 'active_support/core_ext/object/conversions' require 'active_support/core_ext/object/extending' require 'active_support/core_ext/object/instance_variables' +require 'active_support/core_ext/object/metaclass' require 'active_support/core_ext/object/misc' diff --git a/activesupport/lib/active_support/core_ext/object/metaclass.rb b/activesupport/lib/active_support/core_ext/object/metaclass.rb new file mode 100644 index 0000000000..169a76dfb7 --- /dev/null +++ b/activesupport/lib/active_support/core_ext/object/metaclass.rb @@ -0,0 +1,8 @@ +class Object + # Get object's meta (ghost, eigenclass, singleton) class + def metaclass + class << self + self + end + end +end -- cgit v1.2.3 From be078ee162fcae883a5621a30929879cd783a238 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Tue, 15 Jul 2008 21:55:28 -0500 Subject: Run callbacks from object's metaclass [#575 state:resolved] --- activesupport/lib/active_support/callbacks.rb | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'activesupport/lib/active_support') diff --git a/activesupport/lib/active_support/callbacks.rb b/activesupport/lib/active_support/callbacks.rb index 9c59b7ac76..f125a56246 100644 --- a/activesupport/lib/active_support/callbacks.rb +++ b/activesupport/lib/active_support/callbacks.rb @@ -269,7 +269,15 @@ module ActiveSupport # pass # stop def run_callbacks(kind, options = {}, &block) - self.class.send("#{kind}_callback_chain").run(self, options, &block) + callback_chain_method = "#{kind}_callback_chain" + + # Meta class inherits Class so we don't have to merge it in 1.9 + if RUBY_VERSION >= '1.9' + metaclass.send(callback_chain_method).run(self, options, &block) + else + callbacks = self.class.send(callback_chain_method) | metaclass.send(callback_chain_method) + callbacks.run(self, options, &block) + end end end end -- cgit v1.2.3 From 0432d151647f2178ddee79979827d552447c251f Mon Sep 17 00:00:00 2001 From: Pratik Naik Date: Wed, 16 Jul 2008 13:00:36 +0100 Subject: Merge with docrails. --- .../active_support/core_ext/bigdecimal/conversions.rb | 2 +- .../lib/active_support/core_ext/hash/except.rb | 2 +- .../lib/active_support/core_ext/hash/reverse_merge.rb | 17 ++++++++++++----- .../lib/active_support/core_ext/string/inflections.rb | 4 ++-- .../lib/active_support/core_ext/time/calculations.rb | 2 +- activesupport/lib/active_support/json.rb | 2 +- activesupport/lib/active_support/json/encoders/date.rb | 11 +++++++++-- .../lib/active_support/json/encoders/date_time.rb | 11 +++++++++-- activesupport/lib/active_support/json/encoders/time.rb | 13 ++++++++++--- activesupport/lib/active_support/time_with_zone.rb | 14 +++++++++++++- .../vendor/builder-2.1.2/builder/xmlevents.rb | 2 +- .../vendor/memcache-client-1.5.0/memcache.rb | 4 ++-- .../vendor/tzinfo-0.3.9/tzinfo/data_timezone.rb | 2 +- .../vendor/tzinfo-0.3.9/tzinfo/data_timezone_info.rb | 2 +- .../vendor/tzinfo-0.3.9/tzinfo/linked_timezone.rb | 2 +- .../vendor/tzinfo-0.3.9/tzinfo/timezone.rb | 2 +- .../tzinfo-0.3.9/tzinfo/timezone_transition_info.rb | 2 +- .../vendor/xml-simple-1.0.11/xmlsimple.rb | 6 +++--- 18 files changed, 70 insertions(+), 30 deletions(-) (limited to 'activesupport/lib/active_support') diff --git a/activesupport/lib/active_support/core_ext/bigdecimal/conversions.rb b/activesupport/lib/active_support/core_ext/bigdecimal/conversions.rb index d2b01b1b8d..94c7c779f7 100644 --- a/activesupport/lib/active_support/core_ext/bigdecimal/conversions.rb +++ b/activesupport/lib/active_support/core_ext/bigdecimal/conversions.rb @@ -21,7 +21,7 @@ module ActiveSupport #:nodoc: # This emits the number without any scientific notation. # I prefer it to using self.to_f.to_s, which would lose precision. # - # Note that YAML allows that when reconsituting floats + # Note that YAML allows that when reconstituting floats # to native types, some precision may get lost. # There is no full precision real YAML tag that I am aware of. str = self.to_s diff --git a/activesupport/lib/active_support/core_ext/hash/except.rb b/activesupport/lib/active_support/core_ext/hash/except.rb index bc97fa35a6..f26d01553d 100644 --- a/activesupport/lib/active_support/core_ext/hash/except.rb +++ b/activesupport/lib/active_support/core_ext/hash/except.rb @@ -13,7 +13,7 @@ module ActiveSupport #:nodoc: clone.except!(*keys) end - # Replaces the hash without only the given keys. + # Replaces the hash without the given keys. def except!(*keys) keys.map! { |key| convert_key(key) } if respond_to?(:convert_key) keys.each { |key| delete(key) } diff --git a/activesupport/lib/active_support/core_ext/hash/reverse_merge.rb b/activesupport/lib/active_support/core_ext/hash/reverse_merge.rb index 7af10846e7..546e261cc9 100644 --- a/activesupport/lib/active_support/core_ext/hash/reverse_merge.rb +++ b/activesupport/lib/active_support/core_ext/hash/reverse_merge.rb @@ -1,21 +1,28 @@ module ActiveSupport #:nodoc: module CoreExtensions #:nodoc: module Hash #:nodoc: - # Allows for reverse merging where its the keys in the calling hash that wins over those in the other_hash. - # This is particularly useful for initializing an incoming option hash with default values: + # Allows for reverse merging two hashes where the keys in the calling hash take precedence over those + # in the other_hash. This is particularly useful for initializing an option hash with default values: # # def setup(options = {}) # options.reverse_merge! :size => 25, :velocity => 10 # end # - # The default :size and :velocity is only set if the +options+ passed in doesn't already have those keys set. + # Using merge, the above example would look as follows: + # + # def setup(options = {}) + # { :size => 25, :velocity => 10 }.merge(options) + # end + # + # The default :size and :velocity are only set if the +options+ hash passed in doesn't already + # have the respective key. module ReverseMerge - # Performs the opposite of merge, with the keys and values from the first hash taking precedence over the second. + # Performs the opposite of merge, with the keys and values from the first hash taking precedence over the second. def reverse_merge(other_hash) other_hash.merge(self) end - # Performs the opposite of merge, with the keys and values from the first hash taking precedence over the second. + # Performs the opposite of merge, with the keys and values from the first hash taking precedence over the second. # Modifies the receiver in place. def reverse_merge!(other_hash) replace(reverse_merge(other_hash)) diff --git a/activesupport/lib/active_support/core_ext/string/inflections.rb b/activesupport/lib/active_support/core_ext/string/inflections.rb index a009d7c085..3bbad7dad8 100644 --- a/activesupport/lib/active_support/core_ext/string/inflections.rb +++ b/activesupport/lib/active_support/core_ext/string/inflections.rb @@ -24,8 +24,8 @@ module ActiveSupport #:nodoc: # # "posts".singularize # => "post" # "octopi".singularize # => "octopus" - # "sheep".singluarize # => "sheep" - # "word".singluarize # => "word" + # "sheep".singularize # => "sheep" + # "word".singularize # => "word" # "the blue mailmen".singularize # => "the blue mailman" # "CamelOctopi".singularize # => "CamelOctopus" def singularize diff --git a/activesupport/lib/active_support/core_ext/time/calculations.rb b/activesupport/lib/active_support/core_ext/time/calculations.rb index 2cce782676..cd234c9b89 100644 --- a/activesupport/lib/active_support/core_ext/time/calculations.rb +++ b/activesupport/lib/active_support/core_ext/time/calculations.rb @@ -261,7 +261,7 @@ module ActiveSupport #:nodoc: # Layers additional behavior on Time#<=> so that DateTime and ActiveSupport::TimeWithZone instances # can be chronologically compared with a Time def compare_with_coercion(other) - # if other is an ActiveSupport::TimeWithZone, coerce a Time instance from it so we can do <=> comparision + # if other is an ActiveSupport::TimeWithZone, coerce a Time instance from it so we can do <=> comparison other = other.comparable_time if other.respond_to?(:comparable_time) if other.acts_like?(:date) # other is a Date/DateTime, so coerce self #to_datetime and hand off to DateTime#<=> diff --git a/activesupport/lib/active_support/json.rb b/activesupport/lib/active_support/json.rb index 54a7becd0f..2bdb4a7b11 100644 --- a/activesupport/lib/active_support/json.rb +++ b/activesupport/lib/active_support/json.rb @@ -1,5 +1,5 @@ module ActiveSupport - # If true, use ISO 8601 format for dates and times. Otherwise, fall back to the Active Support legacy format. + # If true, use ISO 8601 format for dates and times. Otherwise, fall back to the Active Support legacy format. mattr_accessor :use_standard_json_time_format class << self diff --git a/activesupport/lib/active_support/json/encoders/date.rb b/activesupport/lib/active_support/json/encoders/date.rb index cb9419d29d..1fc99c466f 100644 --- a/activesupport/lib/active_support/json/encoders/date.rb +++ b/activesupport/lib/active_support/json/encoders/date.rb @@ -1,7 +1,14 @@ class Date - # Returns a JSON string representing the date. + # Returns a JSON string representing the date. If ActiveSupport.use_standard_json_time_format is set to true, the + # ISO 8601 format is used. # - # ==== Example: + # ==== Examples: + # + # # With ActiveSupport.use_standard_json_time_format = true + # Date.new(2005,2,1).to_json + # # => "2005-02-01" + # + # # With ActiveSupport.use_standard_json_time_format = false # Date.new(2005,2,1).to_json # # => "2005/02/01" def to_json(options = nil) diff --git a/activesupport/lib/active_support/json/encoders/date_time.rb b/activesupport/lib/active_support/json/encoders/date_time.rb index a4a5efbfb1..e259930033 100644 --- a/activesupport/lib/active_support/json/encoders/date_time.rb +++ b/activesupport/lib/active_support/json/encoders/date_time.rb @@ -1,7 +1,14 @@ class DateTime - # Returns a JSON string representing the datetime. + # Returns a JSON string representing the datetime. If ActiveSupport.use_standard_json_time_format is set to true, the + # ISO 8601 format is used. # - # ==== Example: + # ==== Examples: + # + # # With ActiveSupport.use_standard_json_time_format = true + # DateTime.civil(2005,2,1,15,15,10).to_json + # # => "2005-02-01T15:15:10+00:00" + # + # # With ActiveSupport.use_standard_json_time_format = false # DateTime.civil(2005,2,1,15,15,10).to_json # # => "2005/02/01 15:15:10 +0000" def to_json(options = nil) diff --git a/activesupport/lib/active_support/json/encoders/time.rb b/activesupport/lib/active_support/json/encoders/time.rb index 57ed3c9e31..09fc614889 100644 --- a/activesupport/lib/active_support/json/encoders/time.rb +++ b/activesupport/lib/active_support/json/encoders/time.rb @@ -1,9 +1,16 @@ class Time - # Returns a JSON string representing the time. + # Returns a JSON string representing the time. If ActiveSupport.use_standard_json_time_format is set to true, the + # ISO 8601 format is used. # - # ==== Example: + # ==== Examples: + # + # # With ActiveSupport.use_standard_json_time_format = true + # Time.utc(2005,2,1,15,15,10).to_json + # # => "2005-02-01T15:15:10Z" + # + # # With ActiveSupport.use_standard_json_time_format = false # Time.utc(2005,2,1,15,15,10).to_json - # # => 2005/02/01 15:15:10 +0000" + # # => "2005/02/01 15:15:10 +0000" def to_json(options = nil) if ActiveSupport.use_standard_json_time_format xmlschema.inspect diff --git a/activesupport/lib/active_support/time_with_zone.rb b/activesupport/lib/active_support/time_with_zone.rb index e85bfe9b2e..4866fa0dc8 100644 --- a/activesupport/lib/active_support/time_with_zone.rb +++ b/activesupport/lib/active_support/time_with_zone.rb @@ -102,7 +102,19 @@ module ActiveSupport "#{time.strftime("%Y-%m-%dT%H:%M:%S")}#{formatted_offset(true, 'Z')}" end alias_method :iso8601, :xmlschema - + + # Returns a JSON string representing the TimeWithZone. If ActiveSupport.use_standard_json_time_format is set to + # true, the ISO 8601 format is used. + # + # ==== Examples: + # + # # With ActiveSupport.use_standard_json_time_format = true + # Time.utc(2005,2,1,15,15,10).in_time_zone.to_json + # # => "2005-02-01T15:15:10Z" + # + # # With ActiveSupport.use_standard_json_time_format = false + # Time.utc(2005,2,1,15,15,10).in_time_zone.to_json + # # => "2005/02/01 15:15:10 +0000" def to_json(options = nil) if ActiveSupport.use_standard_json_time_format xmlschema.inspect diff --git a/activesupport/lib/active_support/vendor/builder-2.1.2/builder/xmlevents.rb b/activesupport/lib/active_support/vendor/builder-2.1.2/builder/xmlevents.rb index 91fcd21e13..b373e4da3c 100644 --- a/activesupport/lib/active_support/vendor/builder-2.1.2/builder/xmlevents.rb +++ b/activesupport/lib/active_support/vendor/builder-2.1.2/builder/xmlevents.rb @@ -20,7 +20,7 @@ module Builder # markup. # # Usage: - # xe = Builder::XmlEvents.new(hander) + # xe = Builder::XmlEvents.new(handler) # xe.title("HI") # Sends start_tag/end_tag/text messages to the handler. # # Indentation may also be selected by providing value for the diff --git a/activesupport/lib/active_support/vendor/memcache-client-1.5.0/memcache.rb b/activesupport/lib/active_support/vendor/memcache-client-1.5.0/memcache.rb index dda7f2c30e..30113201a6 100644 --- a/activesupport/lib/active_support/vendor/memcache-client-1.5.0/memcache.rb +++ b/activesupport/lib/active_support/vendor/memcache-client-1.5.0/memcache.rb @@ -119,7 +119,7 @@ class MemCache # Valid options for +opts+ are: # # [:namespace] Prepends this value to all keys added or retrieved. - # [:readonly] Raises an exeception on cache writes when true. + # [:readonly] Raises an exception on cache writes when true. # [:multithread] Wraps cache access in a Mutex for thread safety. # # Other options are ignored. @@ -207,7 +207,7 @@ class MemCache end ## - # Deceremets the value for +key+ by +amount+ and returns the new value. + # Decrements the value for +key+ by +amount+ and returns the new value. # +key+ must already exist. If +key+ is not an integer, it is assumed to be # 0. +key+ can not be decremented below 0. diff --git a/activesupport/lib/active_support/vendor/tzinfo-0.3.9/tzinfo/data_timezone.rb b/activesupport/lib/active_support/vendor/tzinfo-0.3.9/tzinfo/data_timezone.rb index 5eccbdf0db..2510e90b5b 100644 --- a/activesupport/lib/active_support/vendor/tzinfo-0.3.9/tzinfo/data_timezone.rb +++ b/activesupport/lib/active_support/vendor/tzinfo-0.3.9/tzinfo/data_timezone.rb @@ -38,7 +38,7 @@ module TZInfo # Returns the set of TimezonePeriod instances that are valid for the given # local time as an array. If you just want a single period, use - # period_for_local instead and specify how abiguities should be resolved. + # period_for_local instead and specify how ambiguities should be resolved. # Raises PeriodNotFound if no periods are found for the given time. def periods_for_local(local) info.periods_for_local(local) diff --git a/activesupport/lib/active_support/vendor/tzinfo-0.3.9/tzinfo/data_timezone_info.rb b/activesupport/lib/active_support/vendor/tzinfo-0.3.9/tzinfo/data_timezone_info.rb index a45d94554b..8829ba9cc8 100644 --- a/activesupport/lib/active_support/vendor/tzinfo-0.3.9/tzinfo/data_timezone_info.rb +++ b/activesupport/lib/active_support/vendor/tzinfo-0.3.9/tzinfo/data_timezone_info.rb @@ -66,7 +66,7 @@ module TZInfo # ArgumentError will be raised if a transition is added out of order. # offset_id refers to an id defined with offset. ArgumentError will be # raised if the offset_id cannot be found. numerator_or_time and - # denomiator specify the time the transition occurs as. See + # denominator specify the time the transition occurs as. See # TimezoneTransitionInfo for more detail about specifying times. def transition(year, month, offset_id, numerator_or_time, denominator = nil) offset = @offsets[offset_id] diff --git a/activesupport/lib/active_support/vendor/tzinfo-0.3.9/tzinfo/linked_timezone.rb b/activesupport/lib/active_support/vendor/tzinfo-0.3.9/tzinfo/linked_timezone.rb index f8ec4fca87..c757485b84 100644 --- a/activesupport/lib/active_support/vendor/tzinfo-0.3.9/tzinfo/linked_timezone.rb +++ b/activesupport/lib/active_support/vendor/tzinfo-0.3.9/tzinfo/linked_timezone.rb @@ -36,7 +36,7 @@ module TZInfo # Returns the set of TimezonePeriod instances that are valid for the given # local time as an array. If you just want a single period, use - # period_for_local instead and specify how abiguities should be resolved. + # period_for_local instead and specify how ambiguities should be resolved. # Raises PeriodNotFound if no periods are found for the given time. def periods_for_local(local) @linked_timezone.periods_for_local(local) diff --git a/activesupport/lib/active_support/vendor/tzinfo-0.3.9/tzinfo/timezone.rb b/activesupport/lib/active_support/vendor/tzinfo-0.3.9/tzinfo/timezone.rb index f87fb6fb70..eeaa772d0f 100644 --- a/activesupport/lib/active_support/vendor/tzinfo-0.3.9/tzinfo/timezone.rb +++ b/activesupport/lib/active_support/vendor/tzinfo-0.3.9/tzinfo/timezone.rb @@ -121,7 +121,7 @@ module TZInfo TimezoneProxy.new(identifier) end - # If identifier is nil calls super(), otherwise calls get. An identfier + # If identifier is nil calls super(), otherwise calls get. An identifier # should always be passed in when called externally. def self.new(identifier = nil) if identifier diff --git a/activesupport/lib/active_support/vendor/tzinfo-0.3.9/tzinfo/timezone_transition_info.rb b/activesupport/lib/active_support/vendor/tzinfo-0.3.9/tzinfo/timezone_transition_info.rb index 3fecb24f0d..781764f0b0 100644 --- a/activesupport/lib/active_support/vendor/tzinfo-0.3.9/tzinfo/timezone_transition_info.rb +++ b/activesupport/lib/active_support/vendor/tzinfo-0.3.9/tzinfo/timezone_transition_info.rb @@ -37,7 +37,7 @@ module TZInfo attr_reader :numerator_or_time protected :numerator_or_time - # Either the denominotor of the DateTime if the transition time is defined + # Either the denominator of the DateTime if the transition time is defined # as a DateTime, otherwise nil. attr_reader :denominator protected :denominator diff --git a/activesupport/lib/active_support/vendor/xml-simple-1.0.11/xmlsimple.rb b/activesupport/lib/active_support/vendor/xml-simple-1.0.11/xmlsimple.rb index 0de24c0eff..ec6dab513f 100644 --- a/activesupport/lib/active_support/vendor/xml-simple-1.0.11/xmlsimple.rb +++ b/activesupport/lib/active_support/vendor/xml-simple-1.0.11/xmlsimple.rb @@ -121,7 +121,7 @@ class XmlSimple # Create a "global" cache. @@cache = Cache.new - # Creates and intializes a new XmlSimple object. + # Creates and initializes a new XmlSimple object. # # defaults:: # Default values for options. @@ -497,7 +497,7 @@ class XmlSimple } end - # Fold Hases containing a single anonymous Array up into just the Array. + # Fold Hashes containing a single anonymous Array up into just the Array. if count == 1 anonymoustag = @options['anonymoustag'] if result.has_key?(anonymoustag) && result[anonymoustag].instance_of?(Array) @@ -907,7 +907,7 @@ class XmlSimple # Thanks to Norbert Gawor for a bugfix. # # value:: - # Value to be checked for emptyness. + # Value to be checked for emptiness. def empty(value) case value when Hash -- cgit v1.2.3 From 7ae2105d57d3c08bebde44bd72093dd43c48d613 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Wed, 16 Jul 2008 18:46:04 -0500 Subject: MemCacheStore#decrement should use data instance variable not local variable [#521 state:resolved] --- .../lib/active_support/cache/mem_cache_store.rb | 30 +++++++++++----------- 1 file changed, 15 insertions(+), 15 deletions(-) (limited to 'activesupport/lib/active_support') diff --git a/activesupport/lib/active_support/cache/mem_cache_store.rb b/activesupport/lib/active_support/cache/mem_cache_store.rb index b3769b812f..58958dccef 100644 --- a/activesupport/lib/active_support/cache/mem_cache_store.rb +++ b/activesupport/lib/active_support/cache/mem_cache_store.rb @@ -29,8 +29,8 @@ module ActiveSupport nil end - # Set key = value. Pass :unless_exist => true if you don't - # want to update the cache if the key is already set. + # Set key = value. Pass :unless_exist => true if you don't + # want to update the cache if the key is already set. def write(key, value, options = nil) super method = options && options[:unless_exist] ? :add : :set @@ -56,33 +56,33 @@ module ActiveSupport !read(key, options).nil? end - def increment(key, amount = 1) + def increment(key, amount = 1) log("incrementing", key, amount) - - response = @data.incr(key, amount) + + response = @data.incr(key, amount) response == Response::NOT_FOUND ? nil : response - rescue MemCache::MemCacheError + rescue MemCache::MemCacheError nil end def decrement(key, amount = 1) log("decrement", key, amount) - - response = data.decr(key, amount) + + response = @data.decr(key, amount) response == Response::NOT_FOUND ? nil : response - rescue MemCache::MemCacheError + rescue MemCache::MemCacheError nil - end - + end + def delete_matched(matcher, options = nil) super raise "Not supported by Memcache" - end - + end + def clear @data.flush_all - end - + end + def stats @data.stats end -- cgit v1.2.3 From 396f9df8916b71f83aad8d56559cf55fc8501679 Mon Sep 17 00:00:00 2001 From: Josh Owens Date: Wed, 16 Jul 2008 19:31:37 -0500 Subject: Hash#slice supports an array of keys [#613 state:resolved] Signed-off-by: Joshua Peek --- activesupport/lib/active_support/core_ext/hash/slice.rb | 1 + 1 file changed, 1 insertion(+) (limited to 'activesupport/lib/active_support') diff --git a/activesupport/lib/active_support/core_ext/hash/slice.rb b/activesupport/lib/active_support/core_ext/hash/slice.rb index be4dec6e53..d3837d2e54 100644 --- a/activesupport/lib/active_support/core_ext/hash/slice.rb +++ b/activesupport/lib/active_support/core_ext/hash/slice.rb @@ -12,6 +12,7 @@ module ActiveSupport #:nodoc: module Slice # Returns a new hash with only the given keys. def slice(*keys) + keys.flatten! keys = keys.map! { |key| convert_key(key) } if respond_to?(:convert_key) hash = {} keys.each { |k| hash[k] = self[k] if has_key?(k) } -- cgit v1.2.3 From 40dbebba28bfa1c55737da7354542c3bdca4e1a1 Mon Sep 17 00:00:00 2001 From: Lawrence Pit Date: Mon, 14 Jul 2008 11:53:41 +1000 Subject: Allow deep merging of hash values for nested with_options. [#490 state:resolved] Signed-off-by: Pratik Naik --- activesupport/lib/active_support/core_ext/hash.rb | 3 ++- .../lib/active_support/core_ext/hash/deep_merge.rb | 23 ++++++++++++++++++++++ activesupport/lib/active_support/option_merger.rb | 10 +--------- 3 files changed, 26 insertions(+), 10 deletions(-) create mode 100644 activesupport/lib/active_support/core_ext/hash/deep_merge.rb (limited to 'activesupport/lib/active_support') diff --git a/activesupport/lib/active_support/core_ext/hash.rb b/activesupport/lib/active_support/core_ext/hash.rb index 6cbd9dd378..a6065ab48e 100644 --- a/activesupport/lib/active_support/core_ext/hash.rb +++ b/activesupport/lib/active_support/core_ext/hash.rb @@ -1,10 +1,11 @@ -%w(keys indifferent_access reverse_merge conversions diff slice except).each do |ext| +%w(keys indifferent_access deep_merge reverse_merge conversions diff slice except).each do |ext| require "active_support/core_ext/hash/#{ext}" end class Hash #:nodoc: include ActiveSupport::CoreExtensions::Hash::Keys include ActiveSupport::CoreExtensions::Hash::IndifferentAccess + include ActiveSupport::CoreExtensions::Hash::DeepMerge include ActiveSupport::CoreExtensions::Hash::ReverseMerge include ActiveSupport::CoreExtensions::Hash::Conversions include ActiveSupport::CoreExtensions::Hash::Diff diff --git a/activesupport/lib/active_support/core_ext/hash/deep_merge.rb b/activesupport/lib/active_support/core_ext/hash/deep_merge.rb new file mode 100644 index 0000000000..f8842ba57a --- /dev/null +++ b/activesupport/lib/active_support/core_ext/hash/deep_merge.rb @@ -0,0 +1,23 @@ +module ActiveSupport #:nodoc: + module CoreExtensions #:nodoc: + module Hash #:nodoc: + # Allows for deep merging + module DeepMerge + # Returns a new hash with +self+ and +other_hash+ merged recursively. + def deep_merge(other_hash) + self.merge(other_hash) do |key, oldval, newval| + oldval = oldval.to_hash if oldval.respond_to?(:to_hash) + newval = newval.to_hash if newval.respond_to?(:to_hash) + oldval.class.to_s == 'Hash' && newval.class.to_s == 'Hash' ? oldval.deep_merge(newval) : newval + end + end + + # Returns a new hash with +self+ and +other_hash+ merged recursively. + # Modifies the receiver in place. + def deep_merge!(other_hash) + replace(deep_merge(other_hash)) + end + end + end + end +end diff --git a/activesupport/lib/active_support/option_merger.rb b/activesupport/lib/active_support/option_merger.rb index 1a4ff9db9a..c77bca1ac9 100644 --- a/activesupport/lib/active_support/option_merger.rb +++ b/activesupport/lib/active_support/option_merger.rb @@ -10,16 +10,8 @@ module ActiveSupport private def method_missing(method, *arguments, &block) - merge_argument_options! arguments + arguments << (arguments.last.respond_to?(:to_hash) ? @options.deep_merge(arguments.pop) : @options.dup) @context.send!(method, *arguments, &block) end - - def merge_argument_options!(arguments) - arguments << if arguments.last.respond_to? :to_hash - @options.merge(arguments.pop) - else - @options.dup - end - end end end -- cgit v1.2.3 From d8a72b32c5b0c32abf257f05b89bad7d21f178ec Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Wed, 16 Jul 2008 20:23:44 -0500 Subject: Revert "Run callbacks from object's metaclass" --- activesupport/lib/active_support/callbacks.rb | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) (limited to 'activesupport/lib/active_support') diff --git a/activesupport/lib/active_support/callbacks.rb b/activesupport/lib/active_support/callbacks.rb index f125a56246..9c59b7ac76 100644 --- a/activesupport/lib/active_support/callbacks.rb +++ b/activesupport/lib/active_support/callbacks.rb @@ -269,15 +269,7 @@ module ActiveSupport # pass # stop def run_callbacks(kind, options = {}, &block) - callback_chain_method = "#{kind}_callback_chain" - - # Meta class inherits Class so we don't have to merge it in 1.9 - if RUBY_VERSION >= '1.9' - metaclass.send(callback_chain_method).run(self, options, &block) - else - callbacks = self.class.send(callback_chain_method) | metaclass.send(callback_chain_method) - callbacks.run(self, options, &block) - end + self.class.send("#{kind}_callback_chain").run(self, options, &block) end end end -- cgit v1.2.3 From b3a2ee7b87a6b2a4c6ff086644f40a472a676b65 Mon Sep 17 00:00:00 2001 From: Pratik Naik Date: Thu, 17 Jul 2008 15:28:02 +0100 Subject: Revert "Hash#slice supports an array of keys [#613 state:resolved]" This reverts commit 396f9df8916b71f83aad8d56559cf55fc8501679. --- activesupport/lib/active_support/core_ext/hash/slice.rb | 1 - 1 file changed, 1 deletion(-) (limited to 'activesupport/lib/active_support') diff --git a/activesupport/lib/active_support/core_ext/hash/slice.rb b/activesupport/lib/active_support/core_ext/hash/slice.rb index d3837d2e54..be4dec6e53 100644 --- a/activesupport/lib/active_support/core_ext/hash/slice.rb +++ b/activesupport/lib/active_support/core_ext/hash/slice.rb @@ -12,7 +12,6 @@ module ActiveSupport #:nodoc: module Slice # Returns a new hash with only the given keys. def slice(*keys) - keys.flatten! keys = keys.map! { |key| convert_key(key) } if respond_to?(:convert_key) hash = {} keys.each { |k| hash[k] = self[k] if has_key?(k) } -- cgit v1.2.3 From 7e8aee7e6cbd23c1eb18bec1869465e923915e7a Mon Sep 17 00:00:00 2001 From: MatthewRudy Date: Thu, 17 Jul 2008 13:58:42 +0100 Subject: Add extra tests to ensure Hash#slice works with an array as a key. #613 Signed-off-by: Pratik Naik --- activesupport/lib/active_support/core_ext/hash/slice.rb | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'activesupport/lib/active_support') diff --git a/activesupport/lib/active_support/core_ext/hash/slice.rb b/activesupport/lib/active_support/core_ext/hash/slice.rb index be4dec6e53..3f14470f36 100644 --- a/activesupport/lib/active_support/core_ext/hash/slice.rb +++ b/activesupport/lib/active_support/core_ext/hash/slice.rb @@ -9,6 +9,11 @@ module ActiveSupport #:nodoc: # end # # search(options.slice(:mass, :velocity, :time)) + # + # If you have an array of keys you want to limit to, you should splat them: + # + # valid_keys = [:mass, :velocity, :time] + # search(options.slice(*valid_keys)) module Slice # Returns a new hash with only the given keys. def slice(*keys) -- cgit v1.2.3