From bd3cdeea354ebff97b0d5102a0857ce85eedcfa4 Mon Sep 17 00:00:00 2001 From: Akira Matsuda Date: Sun, 3 Apr 2011 12:47:51 +0900 Subject: s/ERb/ERB/g The author of ERB sais, his eRuby implementation was originally named "ERb/ERbLight" and then renamed to "ERB" when started bundled as a Ruby standard lib. http://www2a.biglobe.ne.jp/~seki/ruby/erb.html --- activesupport/lib/active_support/core_ext/string/output_safety.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activesupport') diff --git a/activesupport/lib/active_support/core_ext/string/output_safety.rb b/activesupport/lib/active_support/core_ext/string/output_safety.rb index addd4dab95..c27cbc37c5 100644 --- a/activesupport/lib/active_support/core_ext/string/output_safety.rb +++ b/activesupport/lib/active_support/core_ext/string/output_safety.rb @@ -9,7 +9,7 @@ class ERB # A utility method for escaping HTML tag characters. # This method is also aliased as h. # - # In your ERb templates, use this method to escape any unsafe content. For example: + # In your ERB templates, use this method to escape any unsafe content. For example: # <%=h @person.name %> # # ==== Example: -- cgit v1.2.3 From 1c4db4d7c34a1062e998bbdee03e992c4c5bff6d Mon Sep 17 00:00:00 2001 From: Marc-Andre Lafortune Date: Wed, 16 Mar 2011 09:43:14 -0400 Subject: Raise on invalid timezone Signed-off-by: Santiago Pastorino --- .../lib/active_support/core_ext/time/zones.rb | 20 ++++++++------ activesupport/lib/active_support/railtie.rb | 2 +- activesupport/test/core_ext/time_with_zone_test.rb | 31 ++++++++++++++++++---- 3 files changed, 39 insertions(+), 14 deletions(-) (limited to 'activesupport') diff --git a/activesupport/lib/active_support/core_ext/time/zones.rb b/activesupport/lib/active_support/core_ext/time/zones.rb index ff90d7ca58..3ee6053073 100644 --- a/activesupport/lib/active_support/core_ext/time/zones.rb +++ b/activesupport/lib/active_support/core_ext/time/zones.rb @@ -39,23 +39,27 @@ class Time # Allows override of Time.zone locally inside supplied block; resets Time.zone to existing value when done. def use_zone(time_zone) - old_zone, ::Time.zone = ::Time.zone, get_zone(time_zone) - yield - ensure - ::Time.zone = old_zone + new_zone = get_zone(time_zone) + begin + old_zone, ::Time.zone = ::Time.zone, new_zone + yield + ensure + ::Time.zone = old_zone + end end private + # Returns a TimeZone instance or nil, or raises an ArgumentError for invalid timezones. def get_zone(time_zone) return time_zone if time_zone.nil? || time_zone.is_a?(ActiveSupport::TimeZone) # lookup timezone based on identifier (unless we've been passed a TZInfo::Timezone) unless time_zone.respond_to?(:period_for_local) - time_zone = ActiveSupport::TimeZone[time_zone] || TZInfo::Timezone.get(time_zone) rescue nil + time_zone = ActiveSupport::TimeZone[time_zone] || TZInfo::Timezone.get(time_zone) end # Return if a TimeZone instance, or wrap in a TimeZone instance if a TZInfo::Timezone - if time_zone - time_zone.is_a?(ActiveSupport::TimeZone) ? time_zone : ActiveSupport::TimeZone.create(time_zone.name, nil, time_zone) - end + time_zone.is_a?(ActiveSupport::TimeZone) ? time_zone : ActiveSupport::TimeZone.create(time_zone.name, nil, time_zone) + rescue TZInfo::InvalidTimezoneIdentifier + raise ArgumentError, "Invalid Timezone: #{time_zone}" end end diff --git a/activesupport/lib/active_support/railtie.rb b/activesupport/lib/active_support/railtie.rb index c2deba3b1b..910e9640d1 100644 --- a/activesupport/lib/active_support/railtie.rb +++ b/activesupport/lib/active_support/railtie.rb @@ -46,7 +46,7 @@ module ActiveSupport # If assigned value cannot be matched to a TimeZone, an exception will be raised. initializer "active_support.initialize_time_zone" do |app| require 'active_support/core_ext/time/zones' - zone_default = Time.__send__(:get_zone, app.config.time_zone) + zone_default = Time.__send__(:get_zone, app.config.time_zone) rescue nil unless zone_default raise \ diff --git a/activesupport/test/core_ext/time_with_zone_test.rb b/activesupport/test/core_ext/time_with_zone_test.rb index bafa335a09..cebced5d55 100644 --- a/activesupport/test/core_ext/time_with_zone_test.rb +++ b/activesupport/test/core_ext/time_with_zone_test.rb @@ -36,6 +36,12 @@ class TimeWithZoneTest < Test::Unit::TestCase assert_equal @twz.object_id, @twz.in_time_zone(ActiveSupport::TimeZone['Eastern Time (US & Canada)']).object_id end + def test_in_time_zone_with_bad_argument + assert_raise(ArgumentError) { @twz.in_time_zone('No such timezone exists') } + assert_raise(ArgumentError) { @twz.in_time_zone(-15.hours) } + assert_raise(ArgumentError) { @twz.in_time_zone(Object.new) } + end + def test_localtime assert_equal @twz.localtime, @twz.utc.getlocal end @@ -760,6 +766,15 @@ class TimeWithZoneMethodsForTimeAndDateTimeTest < Test::Unit::TestCase end end + def test_in_time_zone_with_invalid_argument + assert_raise(ArgumentError) { @t.in_time_zone("No such timezone exists") } + assert_raise(ArgumentError) { @dt.in_time_zone("No such timezone exists") } + assert_raise(ArgumentError) { @t.in_time_zone(-15.hours) } + assert_raise(ArgumentError) { @dt.in_time_zone(-15.hours) } + assert_raise(ArgumentError) { @t.in_time_zone(Object.new) } + assert_raise(ArgumentError) { @dt.in_time_zone(Object.new) } + end + def test_in_time_zone_with_time_local_instance with_env_tz 'US/Eastern' do time = Time.local(1999, 12, 31, 19) # == Time.utc(2000) @@ -790,6 +805,14 @@ class TimeWithZoneMethodsForTimeAndDateTimeTest < Test::Unit::TestCase assert_equal ActiveSupport::TimeZone['Alaska'], Time.zone end + def test_use_zone_raises_on_invalid_timezone + Time.zone = 'Alaska' + assert_raise ArgumentError do + Time.use_zone("No such timezone exists") { } + end + assert_equal ActiveSupport::TimeZone['Alaska'], Time.zone + end + def test_time_zone_getter_and_setter Time.zone = ActiveSupport::TimeZone['Alaska'] assert_equal ActiveSupport::TimeZone['Alaska'], Time.zone @@ -843,11 +866,9 @@ class TimeWithZoneMethodsForTimeAndDateTimeTest < Test::Unit::TestCase end def test_time_zone_setter_with_invalid_zone - Time.zone = 'foo' - assert_nil Time.zone - - Time.zone = -15.hours - assert_nil Time.zone + assert_raise(ArgumentError){ Time.zone = "No such timezone exists" } + assert_raise(ArgumentError){ Time.zone = -15.hours } + assert_raise(ArgumentError){ Time.zone = Object.new } end def test_current_returns_time_now_when_zone_not_set -- cgit v1.2.3 From e9020b4b5dbd4a19e288c613a86c78e32010c361 Mon Sep 17 00:00:00 2001 From: Josh Kalderimis Date: Tue, 5 Apr 2011 00:33:29 +0200 Subject: added find_zone and find_zone! to AS timezones and changed the AS Railtie to use find_zone! as well as adding Railtie tests Signed-off-by: Santiago Pastorino --- .../lib/active_support/core_ext/date_time/zones.rb | 2 +- .../lib/active_support/core_ext/time/zones.rb | 33 ++++++++++++---------- activesupport/lib/active_support/railtie.rb | 2 +- activesupport/lib/active_support/time_with_zone.rb | 2 +- activesupport/test/core_ext/time_with_zone_test.rb | 18 ++++++++++++ 5 files changed, 39 insertions(+), 18 deletions(-) (limited to 'activesupport') diff --git a/activesupport/lib/active_support/core_ext/date_time/zones.rb b/activesupport/lib/active_support/core_ext/date_time/zones.rb index 82a4f7ac5a..6fa55a9255 100644 --- a/activesupport/lib/active_support/core_ext/date_time/zones.rb +++ b/activesupport/lib/active_support/core_ext/date_time/zones.rb @@ -16,6 +16,6 @@ class DateTime def in_time_zone(zone = ::Time.zone) return self unless zone - ActiveSupport::TimeWithZone.new(utc? ? self : getutc, ::Time.__send__(:get_zone, zone)) + ActiveSupport::TimeWithZone.new(utc? ? self : getutc, ::Time.find_zone!(zone)) end end diff --git a/activesupport/lib/active_support/core_ext/time/zones.rb b/activesupport/lib/active_support/core_ext/time/zones.rb index 3ee6053073..0c5962858e 100644 --- a/activesupport/lib/active_support/core_ext/time/zones.rb +++ b/activesupport/lib/active_support/core_ext/time/zones.rb @@ -34,12 +34,12 @@ class Time # end # end def zone=(time_zone) - Thread.current[:time_zone] = get_zone(time_zone) + Thread.current[:time_zone] = find_zone!(time_zone) end # Allows override of Time.zone locally inside supplied block; resets Time.zone to existing value when done. def use_zone(time_zone) - new_zone = get_zone(time_zone) + new_zone = find_zone!(time_zone) begin old_zone, ::Time.zone = ::Time.zone, new_zone yield @@ -48,19 +48,22 @@ class Time end end - private - # Returns a TimeZone instance or nil, or raises an ArgumentError for invalid timezones. - def get_zone(time_zone) - return time_zone if time_zone.nil? || time_zone.is_a?(ActiveSupport::TimeZone) - # lookup timezone based on identifier (unless we've been passed a TZInfo::Timezone) - unless time_zone.respond_to?(:period_for_local) - time_zone = ActiveSupport::TimeZone[time_zone] || TZInfo::Timezone.get(time_zone) - end - # Return if a TimeZone instance, or wrap in a TimeZone instance if a TZInfo::Timezone - time_zone.is_a?(ActiveSupport::TimeZone) ? time_zone : ActiveSupport::TimeZone.create(time_zone.name, nil, time_zone) - rescue TZInfo::InvalidTimezoneIdentifier - raise ArgumentError, "Invalid Timezone: #{time_zone}" + # Returns a TimeZone instance or nil, or raises an ArgumentError for invalid timezones. + def find_zone!(time_zone) + return time_zone if time_zone.nil? || time_zone.is_a?(ActiveSupport::TimeZone) + # lookup timezone based on identifier (unless we've been passed a TZInfo::Timezone) + unless time_zone.respond_to?(:period_for_local) + time_zone = ActiveSupport::TimeZone[time_zone] || TZInfo::Timezone.get(time_zone) end + # Return if a TimeZone instance, or wrap in a TimeZone instance if a TZInfo::Timezone + time_zone.is_a?(ActiveSupport::TimeZone) ? time_zone : ActiveSupport::TimeZone.create(time_zone.name, nil, time_zone) + rescue TZInfo::InvalidTimezoneIdentifier + raise ArgumentError, "Invalid Timezone: #{time_zone}" + end + + def find_zone(time_zone) + find_zone!(time_zone) rescue nil + end end # Returns the simultaneous time in Time.zone. @@ -78,6 +81,6 @@ class Time def in_time_zone(zone = ::Time.zone) return self unless zone - ActiveSupport::TimeWithZone.new(utc? ? self : getutc, ::Time.__send__(:get_zone, zone)) + ActiveSupport::TimeWithZone.new(utc? ? self : getutc, ::Time.find_zone!(zone)) end end diff --git a/activesupport/lib/active_support/railtie.rb b/activesupport/lib/active_support/railtie.rb index 910e9640d1..04df2ea562 100644 --- a/activesupport/lib/active_support/railtie.rb +++ b/activesupport/lib/active_support/railtie.rb @@ -46,7 +46,7 @@ module ActiveSupport # If assigned value cannot be matched to a TimeZone, an exception will be raised. initializer "active_support.initialize_time_zone" do |app| require 'active_support/core_ext/time/zones' - zone_default = Time.__send__(:get_zone, app.config.time_zone) rescue nil + zone_default = Time.find_zone!(app.config.time_zone) unless zone_default raise \ diff --git a/activesupport/lib/active_support/time_with_zone.rb b/activesupport/lib/active_support/time_with_zone.rb index c66aa78ce8..d3937154f7 100644 --- a/activesupport/lib/active_support/time_with_zone.rb +++ b/activesupport/lib/active_support/time_with_zone.rb @@ -309,7 +309,7 @@ module ActiveSupport end def marshal_load(variables) - initialize(variables[0].utc, ::Time.__send__(:get_zone, variables[1]), variables[2].utc) + initialize(variables[0].utc, ::Time.find_zone(variables[1]), variables[2].utc) end # Ensure proxy class responds to all methods that underlying time instance responds to. diff --git a/activesupport/test/core_ext/time_with_zone_test.rb b/activesupport/test/core_ext/time_with_zone_test.rb index cebced5d55..72b55183ba 100644 --- a/activesupport/test/core_ext/time_with_zone_test.rb +++ b/activesupport/test/core_ext/time_with_zone_test.rb @@ -871,6 +871,24 @@ class TimeWithZoneMethodsForTimeAndDateTimeTest < Test::Unit::TestCase assert_raise(ArgumentError){ Time.zone = Object.new } end + def test_find_zone_without_bang_returns_nil_if_time_zone_can_not_be_found + assert_nil Time.find_zone('No such timezone exists') + assert_nil Time.find_zone(-15.hours) + assert_nil Time.find_zone(Object.new) + end + + def test_find_zone_with_bang_raises_if_time_zone_can_not_be_found + assert_raise(ArgumentError) { Time.find_zone!('No such timezone exists') } + assert_raise(ArgumentError) { Time.find_zone!(-15.hours) } + assert_raise(ArgumentError) { Time.find_zone!(Object.new) } + end + + def test_time_zone_setter_with_find_zone_without_bang + assert_nil Time.zone = Time.find_zone('No such timezone exists') + assert_nil Time.zone = Time.find_zone(-15.hours) + assert_nil Time.zone = Time.find_zone(Object.new) + end + def test_current_returns_time_now_when_zone_not_set with_env_tz 'US/Eastern' do Time.stubs(:now).returns Time.local(2000) -- cgit v1.2.3 From 635d991683c439da56fa72853880e88e6ac291ed Mon Sep 17 00:00:00 2001 From: "Prem Sichanugrist, Brian Morearty, John Reitano" Date: Sun, 10 Apr 2011 20:27:08 +0800 Subject: Add support for Object#in? and Object#either? in Active Support [#6321 state:committed] This will allow you to check if an object is included in another object or the list of objects or not. This patch is derived from patch by Brian Morearty and John Reitano on Lighthouse ticket. I've rewrite it and make sure that we support both 'another object' and 'list of objects' version, as it surely be useful to support both. --- activesupport/CHANGELOG | 2 + .../active_support/core_ext/object/inclusion.rb | 20 +++++++++ .../test/core_ext/object/inclusion_test.rb | 52 ++++++++++++++++++++++ 3 files changed, 74 insertions(+) create mode 100644 activesupport/lib/active_support/core_ext/object/inclusion.rb create mode 100644 activesupport/test/core_ext/object/inclusion_test.rb (limited to 'activesupport') diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG index 373236ce9a..8e7e2d188b 100644 --- a/activesupport/CHANGELOG +++ b/activesupport/CHANGELOG @@ -1,5 +1,7 @@ *Rails 3.1.0 (unreleased)* +* Add Object#in? to test if an object is included in another object, and Object#either? to test if an object is included in a list of objects which will be passed as arguments. [Prem Sichanugrist, Brian Morearty, John Reitano] + * LocalCache strategy is now a real middleware class, not an anonymous class posing for pictures. diff --git a/activesupport/lib/active_support/core_ext/object/inclusion.rb b/activesupport/lib/active_support/core_ext/object/inclusion.rb new file mode 100644 index 0000000000..79e9fd6c88 --- /dev/null +++ b/activesupport/lib/active_support/core_ext/object/inclusion.rb @@ -0,0 +1,20 @@ +class Object + # Returns true if this object is included in the argument. Argument must be + # any object which respond to +#include?+. Usage: + # + # characters = ["Konata", "Kagami", "Tsukasa"] + # "Konata".in?(characters) # => true + # + def in?(another_object) + another_object.include?(self) + end + + # Returns true if this object is included in the argument list. Usage: + # + # username = "sikachu" + # username.either?("josevalim", "dhh", "wycats") # => false + # + def either?(*objects) + objects.include?(self) + end +end diff --git a/activesupport/test/core_ext/object/inclusion_test.rb b/activesupport/test/core_ext/object/inclusion_test.rb new file mode 100644 index 0000000000..b4207c20ea --- /dev/null +++ b/activesupport/test/core_ext/object/inclusion_test.rb @@ -0,0 +1,52 @@ +require 'abstract_unit' +require 'active_support/core_ext/object/inclusion' + +class InTest < Test::Unit::TestCase + def test_in_array + assert 1.in?([1,2]) + assert !3.in?([1,2]) + end + + def test_in_hash + h = { "a" => 100, "b" => 200 } + assert "a".in?(h) + assert !"z".in?(h) + end + + def test_in_string + assert "lo".in?("hello") + assert !"ol".in?("hello") + assert ?h.in?("hello") + end + + def test_in_range + assert 25.in?(1..50) + assert !75.in?(1..50) + end + + def test_in_set + s = Set.new([1,2]) + assert 1.in?(s) + assert !3.in?(s) + end + + def test_either + assert 1.either?(1,2,3) + assert !5.either?(1,2,3) + assert [1,2,3].either?([1,2,3], 2, [3,4,5]) + end + + module A + end + class B + include A + end + class C < B + end + + def test_in_module + assert A.in?(B) + assert A.in?(C) + assert !A.in?(A) + end +end -- cgit v1.2.3 From a9f3c9da01d721963d05949604ead909aaabbf36 Mon Sep 17 00:00:00 2001 From: Prem Sichanugrist Date: Mon, 11 Apr 2011 00:52:42 +0800 Subject: Using Object#in? and Object#either? in various places There're a lot of places in Rails source code which make a lot of sense to switching to Object#in? or Object#either? instead of using [].include?. --- activesupport/bin/generate_tables | 2 +- activesupport/lib/active_support/cache/file_store.rb | 5 +++-- activesupport/lib/active_support/callbacks.rb | 3 ++- activesupport/lib/active_support/time_with_zone.rb | 3 ++- activesupport/test/transliterate_test.rb | 3 ++- 5 files changed, 10 insertions(+), 6 deletions(-) (limited to 'activesupport') diff --git a/activesupport/bin/generate_tables b/activesupport/bin/generate_tables index 5fefa429df..0ca2f52363 100644 --- a/activesupport/bin/generate_tables +++ b/activesupport/bin/generate_tables @@ -105,7 +105,7 @@ module ActiveSupport def normalize_boundary_map @ucd.boundary.each do |k,v| - if [:lf, :cr].include? k + if k.in(:lf, :cr) @ucd.boundary[k] = v[0] end end diff --git a/activesupport/lib/active_support/cache/file_store.rb b/activesupport/lib/active_support/cache/file_store.rb index 18182bbb40..5eeb17efd5 100644 --- a/activesupport/lib/active_support/cache/file_store.rb +++ b/activesupport/lib/active_support/cache/file_store.rb @@ -1,5 +1,6 @@ require 'active_support/core_ext/file/atomic' require 'active_support/core_ext/string/conversions' +require 'active_support/core_ext/object/inclusion' require 'rack/utils' module ActiveSupport @@ -20,7 +21,7 @@ module ActiveSupport end def clear(options = nil) - root_dirs = Dir.entries(cache_path).reject{|f| ['.', '..'].include?(f)} + root_dirs = Dir.entries(cache_path).reject{|f| f.either?('.', '..')} FileUtils.rm_r(root_dirs.collect{|f| File.join(cache_path, f)}) end @@ -161,7 +162,7 @@ module ActiveSupport # Delete empty directories in the cache. def delete_empty_directories(dir) return if dir == cache_path - if Dir.entries(dir).reject{|f| ['.', '..'].include?(f)}.empty? + if Dir.entries(dir).reject{|f| f.either?('.', '..')}.empty? File.delete(dir) rescue nil delete_empty_directories(File.dirname(dir)) end diff --git a/activesupport/lib/active_support/callbacks.rb b/activesupport/lib/active_support/callbacks.rb index 418102352f..738ce0e994 100644 --- a/activesupport/lib/active_support/callbacks.rb +++ b/activesupport/lib/active_support/callbacks.rb @@ -4,6 +4,7 @@ require 'active_support/core_ext/array/wrap' require 'active_support/core_ext/class/attribute' require 'active_support/core_ext/kernel/reporting' require 'active_support/core_ext/kernel/singleton_class' +require 'active_support/core_ext/object/inclusion' module ActiveSupport # \Callbacks are code hooks that are run at key points in an object's lifecycle. @@ -412,7 +413,7 @@ module ActiveSupport # CallbackChain. # def __update_callbacks(name, filters = [], block = nil) #:nodoc: - type = [:before, :after, :around].include?(filters.first) ? filters.shift : :before + type = filters.first.either?(:before, :after, :around) ? filters.shift : :before options = filters.last.is_a?(Hash) ? filters.pop : {} filters.unshift(block) if block diff --git a/activesupport/lib/active_support/time_with_zone.rb b/activesupport/lib/active_support/time_with_zone.rb index d3937154f7..3b053d7830 100644 --- a/activesupport/lib/active_support/time_with_zone.rb +++ b/activesupport/lib/active_support/time_with_zone.rb @@ -1,5 +1,6 @@ require "active_support/values/time_zone" require 'active_support/core_ext/object/acts_like' +require 'active_support/core_ext/object/inclusion' module ActiveSupport # A Time-like class that can represent a time in any time zone. Necessary because standard Ruby Time instances are @@ -344,7 +345,7 @@ module ActiveSupport end def duration_of_variable_length?(obj) - ActiveSupport::Duration === obj && obj.parts.any? {|p| [:years, :months, :days].include? p[0] } + ActiveSupport::Duration === obj && obj.parts.any? {|p| p[0].either?(:years, :months, :days) } end end end diff --git a/activesupport/test/transliterate_test.rb b/activesupport/test/transliterate_test.rb index b054855d08..09271b759d 100644 --- a/activesupport/test/transliterate_test.rb +++ b/activesupport/test/transliterate_test.rb @@ -1,6 +1,7 @@ # encoding: utf-8 require 'abstract_unit' require 'active_support/inflector/transliterate' +require 'active_support/core_ext/object/inclusion' class TransliterateTest < Test::Unit::TestCase @@ -15,7 +16,7 @@ class TransliterateTest < Test::Unit::TestCase # create string with range of Unicode"s western characters with # diacritics, excluding the division and multiplication signs which for # some reason or other are floating in the middle of all the letters. - string = (0xC0..0x17E).to_a.reject {|c| [0xD7, 0xF7].include? c}.pack("U*") + string = (0xC0..0x17E).to_a.reject {|c| c.either?(0xD7, 0xF7)}.pack("U*") string.each_char do |char| assert_match %r{^[a-zA-Z']*$}, ActiveSupport::Inflector.transliterate(string) end -- cgit v1.2.3 From d6edaeeaf8b6c0f0b741c0827ab6e091bdd4e197 Mon Sep 17 00:00:00 2001 From: Prem Sichanugrist Date: Mon, 11 Apr 2011 12:35:20 +0700 Subject: Fix failing test case on master It turned out that I overlook at some replacements .. --- activesupport/bin/generate_tables | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activesupport') diff --git a/activesupport/bin/generate_tables b/activesupport/bin/generate_tables index 0ca2f52363..5fefa429df 100644 --- a/activesupport/bin/generate_tables +++ b/activesupport/bin/generate_tables @@ -105,7 +105,7 @@ module ActiveSupport def normalize_boundary_map @ucd.boundary.each do |k,v| - if k.in(:lf, :cr) + if [:lf, :cr].include? k @ucd.boundary[k] = v[0] end end -- cgit v1.2.3 From 1d8bf4f4f900a905e6350e246a683659b5480ec9 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Mon, 11 Apr 2011 15:25:39 +0200 Subject: Cant use inclusion in commands/application.rb as the frameworks havent all been required yet --- activesupport/lib/active_support/core_ext/object.rb | 1 + 1 file changed, 1 insertion(+) (limited to 'activesupport') diff --git a/activesupport/lib/active_support/core_ext/object.rb b/activesupport/lib/active_support/core_ext/object.rb index 790a26f5c1..9ad1e12699 100644 --- a/activesupport/lib/active_support/core_ext/object.rb +++ b/activesupport/lib/active_support/core_ext/object.rb @@ -2,6 +2,7 @@ require 'active_support/core_ext/object/acts_like' require 'active_support/core_ext/object/blank' require 'active_support/core_ext/object/duplicable' require 'active_support/core_ext/object/try' +require 'active_support/core_ext/object/inclusion' require 'active_support/core_ext/object/conversions' require 'active_support/core_ext/object/instance_variables' -- cgit v1.2.3 From d1575ae1b9658c91145d6a46ec2a144a5a089207 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Tue, 12 Apr 2011 00:23:07 +0200 Subject: Change Object#either? to Object#among? -- thanks to @jamesarosen for the suggestion! --- activesupport/CHANGELOG | 2 +- activesupport/lib/active_support/cache/file_store.rb | 4 ++-- activesupport/lib/active_support/callbacks.rb | 2 +- activesupport/lib/active_support/core_ext/object/inclusion.rb | 4 ++-- activesupport/lib/active_support/time_with_zone.rb | 2 +- activesupport/test/core_ext/object/inclusion_test.rb | 6 +++--- activesupport/test/transliterate_test.rb | 2 +- 7 files changed, 11 insertions(+), 11 deletions(-) (limited to 'activesupport') diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG index 8e7e2d188b..eb027795a8 100644 --- a/activesupport/CHANGELOG +++ b/activesupport/CHANGELOG @@ -1,6 +1,6 @@ *Rails 3.1.0 (unreleased)* -* Add Object#in? to test if an object is included in another object, and Object#either? to test if an object is included in a list of objects which will be passed as arguments. [Prem Sichanugrist, Brian Morearty, John Reitano] +* Add Object#in? to test if an object is included in another object, and Object#among? to test if an object is included in a list of objects which will be passed as arguments. [Prem Sichanugrist, Brian Morearty, John Reitano] * LocalCache strategy is now a real middleware class, not an anonymous class posing for pictures. diff --git a/activesupport/lib/active_support/cache/file_store.rb b/activesupport/lib/active_support/cache/file_store.rb index 5eeb17efd5..bdef47a237 100644 --- a/activesupport/lib/active_support/cache/file_store.rb +++ b/activesupport/lib/active_support/cache/file_store.rb @@ -21,7 +21,7 @@ module ActiveSupport end def clear(options = nil) - root_dirs = Dir.entries(cache_path).reject{|f| f.either?('.', '..')} + root_dirs = Dir.entries(cache_path).reject{|f| f.among?('.', '..')} FileUtils.rm_r(root_dirs.collect{|f| File.join(cache_path, f)}) end @@ -162,7 +162,7 @@ module ActiveSupport # Delete empty directories in the cache. def delete_empty_directories(dir) return if dir == cache_path - if Dir.entries(dir).reject{|f| f.either?('.', '..')}.empty? + if Dir.entries(dir).reject{|f| f.among?('.', '..')}.empty? File.delete(dir) rescue nil delete_empty_directories(File.dirname(dir)) end diff --git a/activesupport/lib/active_support/callbacks.rb b/activesupport/lib/active_support/callbacks.rb index 738ce0e994..6a0bffb6d7 100644 --- a/activesupport/lib/active_support/callbacks.rb +++ b/activesupport/lib/active_support/callbacks.rb @@ -413,7 +413,7 @@ module ActiveSupport # CallbackChain. # def __update_callbacks(name, filters = [], block = nil) #:nodoc: - type = filters.first.either?(:before, :after, :around) ? filters.shift : :before + type = filters.first.among?(:before, :after, :around) ? filters.shift : :before options = filters.last.is_a?(Hash) ? filters.pop : {} filters.unshift(block) if block diff --git a/activesupport/lib/active_support/core_ext/object/inclusion.rb b/activesupport/lib/active_support/core_ext/object/inclusion.rb index 79e9fd6c88..cf89288aed 100644 --- a/activesupport/lib/active_support/core_ext/object/inclusion.rb +++ b/activesupport/lib/active_support/core_ext/object/inclusion.rb @@ -12,9 +12,9 @@ class Object # Returns true if this object is included in the argument list. Usage: # # username = "sikachu" - # username.either?("josevalim", "dhh", "wycats") # => false + # username.among?("josevalim", "dhh", "wycats") # => false # - def either?(*objects) + def among?(*objects) objects.include?(self) end end diff --git a/activesupport/lib/active_support/time_with_zone.rb b/activesupport/lib/active_support/time_with_zone.rb index 3b053d7830..6b9120e51f 100644 --- a/activesupport/lib/active_support/time_with_zone.rb +++ b/activesupport/lib/active_support/time_with_zone.rb @@ -345,7 +345,7 @@ module ActiveSupport end def duration_of_variable_length?(obj) - ActiveSupport::Duration === obj && obj.parts.any? {|p| p[0].either?(:years, :months, :days) } + ActiveSupport::Duration === obj && obj.parts.any? {|p| p[0].among?(:years, :months, :days) } end end end diff --git a/activesupport/test/core_ext/object/inclusion_test.rb b/activesupport/test/core_ext/object/inclusion_test.rb index b4207c20ea..25120deb81 100644 --- a/activesupport/test/core_ext/object/inclusion_test.rb +++ b/activesupport/test/core_ext/object/inclusion_test.rb @@ -31,9 +31,9 @@ class InTest < Test::Unit::TestCase end def test_either - assert 1.either?(1,2,3) - assert !5.either?(1,2,3) - assert [1,2,3].either?([1,2,3], 2, [3,4,5]) + assert 1.among?(1,2,3) + assert !5.among?(1,2,3) + assert [1,2,3].among?([1,2,3], 2, [3,4,5]) end module A diff --git a/activesupport/test/transliterate_test.rb b/activesupport/test/transliterate_test.rb index 09271b759d..90b40b5478 100644 --- a/activesupport/test/transliterate_test.rb +++ b/activesupport/test/transliterate_test.rb @@ -16,7 +16,7 @@ class TransliterateTest < Test::Unit::TestCase # create string with range of Unicode"s western characters with # diacritics, excluding the division and multiplication signs which for # some reason or other are floating in the middle of all the letters. - string = (0xC0..0x17E).to_a.reject {|c| c.either?(0xD7, 0xF7)}.pack("U*") + string = (0xC0..0x17E).to_a.reject {|c| c.among?(0xD7, 0xF7)}.pack("U*") string.each_char do |char| assert_match %r{^[a-zA-Z']*$}, ActiveSupport::Inflector.transliterate(string) end -- cgit v1.2.3 From a616e7a88cc35487b979d5f786092ba53aa47135 Mon Sep 17 00:00:00 2001 From: Sebastian Martinez Date: Tue, 12 Apr 2011 20:10:50 -0300 Subject: Update test name to the corresponding method name Signed-off-by: Santiago Pastorino --- activesupport/test/core_ext/object/inclusion_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activesupport') diff --git a/activesupport/test/core_ext/object/inclusion_test.rb b/activesupport/test/core_ext/object/inclusion_test.rb index 25120deb81..9f2ace0ef7 100644 --- a/activesupport/test/core_ext/object/inclusion_test.rb +++ b/activesupport/test/core_ext/object/inclusion_test.rb @@ -30,7 +30,7 @@ class InTest < Test::Unit::TestCase assert !3.in?(s) end - def test_either + def test_among assert 1.among?(1,2,3) assert !5.among?(1,2,3) assert [1,2,3].among?([1,2,3], 2, [3,4,5]) -- cgit v1.2.3 From 733bfa63f5d8d3b963202b6d3e9f00b4db070b91 Mon Sep 17 00:00:00 2001 From: Prem Sichanugrist Date: Wed, 13 Apr 2011 00:04:40 +0800 Subject: Remove `#among?` from Active Support After a long list of discussion about the performance problem from using varargs and the reason that we can't find a great pair for it, it would be best to remove support for it for now. It will come back if we can find a good pair for it. For now, Bon Voyage, `#among?`. --- activesupport/CHANGELOG | 2 +- activesupport/lib/active_support/cache/file_store.rb | 4 ++-- activesupport/lib/active_support/callbacks.rb | 2 +- activesupport/lib/active_support/core_ext/object/inclusion.rb | 9 --------- activesupport/lib/active_support/time_with_zone.rb | 2 +- activesupport/test/core_ext/object/inclusion_test.rb | 6 ------ activesupport/test/transliterate_test.rb | 2 +- 7 files changed, 6 insertions(+), 21 deletions(-) (limited to 'activesupport') diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG index eb027795a8..163f1c932c 100644 --- a/activesupport/CHANGELOG +++ b/activesupport/CHANGELOG @@ -1,6 +1,6 @@ *Rails 3.1.0 (unreleased)* -* Add Object#in? to test if an object is included in another object, and Object#among? to test if an object is included in a list of objects which will be passed as arguments. [Prem Sichanugrist, Brian Morearty, John Reitano] +* Add Object#in? to test if an object is included in another object [Prem Sichanugrist, Brian Morearty, John Reitano] * LocalCache strategy is now a real middleware class, not an anonymous class posing for pictures. diff --git a/activesupport/lib/active_support/cache/file_store.rb b/activesupport/lib/active_support/cache/file_store.rb index bdef47a237..9936b33e22 100644 --- a/activesupport/lib/active_support/cache/file_store.rb +++ b/activesupport/lib/active_support/cache/file_store.rb @@ -21,7 +21,7 @@ module ActiveSupport end def clear(options = nil) - root_dirs = Dir.entries(cache_path).reject{|f| f.among?('.', '..')} + root_dirs = Dir.entries(cache_path).reject{|f| f.in?(['.', '..'])} FileUtils.rm_r(root_dirs.collect{|f| File.join(cache_path, f)}) end @@ -162,7 +162,7 @@ module ActiveSupport # Delete empty directories in the cache. def delete_empty_directories(dir) return if dir == cache_path - if Dir.entries(dir).reject{|f| f.among?('.', '..')}.empty? + if Dir.entries(dir).reject{|f| f.in?(['.', '..'])}.empty? File.delete(dir) rescue nil delete_empty_directories(File.dirname(dir)) end diff --git a/activesupport/lib/active_support/callbacks.rb b/activesupport/lib/active_support/callbacks.rb index 6a0bffb6d7..656cba625c 100644 --- a/activesupport/lib/active_support/callbacks.rb +++ b/activesupport/lib/active_support/callbacks.rb @@ -413,7 +413,7 @@ module ActiveSupport # CallbackChain. # def __update_callbacks(name, filters = [], block = nil) #:nodoc: - type = filters.first.among?(:before, :after, :around) ? filters.shift : :before + type = filters.first.in?([:before, :after, :around]) ? filters.shift : :before options = filters.last.is_a?(Hash) ? filters.pop : {} filters.unshift(block) if block diff --git a/activesupport/lib/active_support/core_ext/object/inclusion.rb b/activesupport/lib/active_support/core_ext/object/inclusion.rb index cf89288aed..cc7edc0f17 100644 --- a/activesupport/lib/active_support/core_ext/object/inclusion.rb +++ b/activesupport/lib/active_support/core_ext/object/inclusion.rb @@ -8,13 +8,4 @@ class Object def in?(another_object) another_object.include?(self) end - - # Returns true if this object is included in the argument list. Usage: - # - # username = "sikachu" - # username.among?("josevalim", "dhh", "wycats") # => false - # - def among?(*objects) - objects.include?(self) - end end diff --git a/activesupport/lib/active_support/time_with_zone.rb b/activesupport/lib/active_support/time_with_zone.rb index 6b9120e51f..3d092529d6 100644 --- a/activesupport/lib/active_support/time_with_zone.rb +++ b/activesupport/lib/active_support/time_with_zone.rb @@ -345,7 +345,7 @@ module ActiveSupport end def duration_of_variable_length?(obj) - ActiveSupport::Duration === obj && obj.parts.any? {|p| p[0].among?(:years, :months, :days) } + ActiveSupport::Duration === obj && obj.parts.any? {|p| p[0].in?([:years, :months, :days]) } end end end diff --git a/activesupport/test/core_ext/object/inclusion_test.rb b/activesupport/test/core_ext/object/inclusion_test.rb index 9f2ace0ef7..382271d42d 100644 --- a/activesupport/test/core_ext/object/inclusion_test.rb +++ b/activesupport/test/core_ext/object/inclusion_test.rb @@ -30,12 +30,6 @@ class InTest < Test::Unit::TestCase assert !3.in?(s) end - def test_among - assert 1.among?(1,2,3) - assert !5.among?(1,2,3) - assert [1,2,3].among?([1,2,3], 2, [3,4,5]) - end - module A end class B diff --git a/activesupport/test/transliterate_test.rb b/activesupport/test/transliterate_test.rb index 90b40b5478..08e11d4f38 100644 --- a/activesupport/test/transliterate_test.rb +++ b/activesupport/test/transliterate_test.rb @@ -16,7 +16,7 @@ class TransliterateTest < Test::Unit::TestCase # create string with range of Unicode"s western characters with # diacritics, excluding the division and multiplication signs which for # some reason or other are floating in the middle of all the letters. - string = (0xC0..0x17E).to_a.reject {|c| c.among?(0xD7, 0xF7)}.pack("U*") + string = (0xC0..0x17E).to_a.reject {|c| c.in?([0xD7, 0xF7])}.pack("U*") string.each_char do |char| assert_match %r{^[a-zA-Z']*$}, ActiveSupport::Inflector.transliterate(string) end -- cgit v1.2.3 From 4db4f8c6244f017def5668d44a62bdad231f4c18 Mon Sep 17 00:00:00 2001 From: Ben Orenstein Date: Fri, 25 Mar 2011 15:48:52 -0400 Subject: Add tests for InheritableOptions. [#6625 state:committed] Signed-off-by: Santiago Pastorino --- activesupport/test/ordered_options_test.rb | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'activesupport') diff --git a/activesupport/test/ordered_options_test.rb b/activesupport/test/ordered_options_test.rb index e48425ca25..b215b60df3 100644 --- a/activesupport/test/ordered_options_test.rb +++ b/activesupport/test/ordered_options_test.rb @@ -50,4 +50,30 @@ class OrderedOptionsTest < Test::Unit::TestCase assert_equal 2, a.size assert_equal 56, a.else_where end + + def test_inheritable_options_continues_lookup_in_parent + parent = ActiveSupport::OrderedOptions.new + parent[:foo] = true + + child = ActiveSupport::InheritableOptions.new(parent) + assert child.foo + end + + def test_inheritable_options_can_override_parent + parent = ActiveSupport::OrderedOptions.new + parent[:foo] = :bar + + child = ActiveSupport::InheritableOptions.new(parent) + child[:foo] = :baz + + assert_equal :baz, child.foo + end + + def test_inheritable_options_inheritable_copy + original = ActiveSupport::InheritableOptions.new + copy = original.inheritable_copy + + assert copy.kind_of?(original.class) + assert_not_equal copy.object_id, original.object_id + end end -- cgit v1.2.3 From 7660e7cb4df69ad127661ad87f495a3b212170b9 Mon Sep 17 00:00:00 2001 From: Andre Arko Date: Thu, 14 Apr 2011 16:25:08 +0800 Subject: attributes no longer disappear if a tag contains whitespace old: Hash.from_xml("\n") => {"tag"=>"\n"} new: Hash.from_xml("\n") => {"tag"=>{"foo"=>"bar", "__content__"=>"\n"} --- activesupport/lib/active_support/core_ext/hash/conversions.rb | 2 +- activesupport/test/core_ext/hash_ext_test.rb | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) (limited to 'activesupport') diff --git a/activesupport/lib/active_support/core_ext/hash/conversions.rb b/activesupport/lib/active_support/core_ext/hash/conversions.rb index 3005fef44c..06bd8341ca 100644 --- a/activesupport/lib/active_support/core_ext/hash/conversions.rb +++ b/activesupport/lib/active_support/core_ext/hash/conversions.rb @@ -108,7 +108,7 @@ class Hash raise "can't typecast #{entries.inspect}" end end - elsif value.has_key?("__content__") + elsif value.has_key?("__content__") && value["__content__"].gsub(/\s/, '').present? content = value["__content__"] if parser = ActiveSupport::XmlMini::PARSING[value["type"]] parser.arity == 1 ? parser.call(content) : parser.call(content, value) diff --git a/activesupport/test/core_ext/hash_ext_test.rb b/activesupport/test/core_ext/hash_ext_test.rb index a0479d45ac..012b956d7f 100644 --- a/activesupport/test/core_ext/hash_ext_test.rb +++ b/activesupport/test/core_ext/hash_ext_test.rb @@ -889,6 +889,15 @@ class HashToXmlTest < Test::Unit::TestCase assert_equal 'application/octet-stream', file.content_type end + def test_tag_with_attrs_and_whitespace + xml = <<-XML + + + XML + hash = Hash.from_xml(xml) + assert_equal "bacon is the best", hash['blog']['name'] + end + def test_xsd_like_types_from_xml bacon_xml = <<-EOT -- cgit v1.2.3 From d01be122b84bd0a5f6315650593564b1d8aa50d0 Mon Sep 17 00:00:00 2001 From: Andre Arko Date: Thu, 14 Apr 2011 16:36:13 +0800 Subject: gsub is not needed (thanks @fxn!) --- activesupport/lib/active_support/core_ext/hash/conversions.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activesupport') diff --git a/activesupport/lib/active_support/core_ext/hash/conversions.rb b/activesupport/lib/active_support/core_ext/hash/conversions.rb index 06bd8341ca..7e3a8c6775 100644 --- a/activesupport/lib/active_support/core_ext/hash/conversions.rb +++ b/activesupport/lib/active_support/core_ext/hash/conversions.rb @@ -108,7 +108,7 @@ class Hash raise "can't typecast #{entries.inspect}" end end - elsif value.has_key?("__content__") && value["__content__"].gsub(/\s/, '').present? + elsif value.has_key?("__content__") && value["__content__"].present? content = value["__content__"] if parser = ActiveSupport::XmlMini::PARSING[value["type"]] parser.arity == 1 ? parser.call(content) : parser.call(content, value) -- cgit v1.2.3 From bd032fe97eabed62130bb745b0955090099fe525 Mon Sep 17 00:00:00 2001 From: Andre Arko Date: Thu, 14 Apr 2011 17:05:30 +0800 Subject: files are a special case and need whitespace to be significant --- activesupport/lib/active_support/core_ext/hash/conversions.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activesupport') diff --git a/activesupport/lib/active_support/core_ext/hash/conversions.rb b/activesupport/lib/active_support/core_ext/hash/conversions.rb index 7e3a8c6775..61a1d88b0e 100644 --- a/activesupport/lib/active_support/core_ext/hash/conversions.rb +++ b/activesupport/lib/active_support/core_ext/hash/conversions.rb @@ -108,7 +108,7 @@ class Hash raise "can't typecast #{entries.inspect}" end end - elsif value.has_key?("__content__") && value["__content__"].present? + elsif value['type'] == 'file' || value["__content__"].present? content = value["__content__"] if parser = ActiveSupport::XmlMini::PARSING[value["type"]] parser.arity == 1 ? parser.call(content) : parser.call(content, value) -- cgit v1.2.3 From 2db9f8a41c0e9d8978c0f6b80cb6efe5665168a7 Mon Sep 17 00:00:00 2001 From: Jared McFarland Date: Thu, 14 Apr 2011 09:00:34 +0800 Subject: added an exception to Object#in? to match the methods documentation --- activesupport/lib/active_support/core_ext/object/inclusion.rb | 1 + 1 file changed, 1 insertion(+) (limited to 'activesupport') diff --git a/activesupport/lib/active_support/core_ext/object/inclusion.rb b/activesupport/lib/active_support/core_ext/object/inclusion.rb index cc7edc0f17..f30333fd02 100644 --- a/activesupport/lib/active_support/core_ext/object/inclusion.rb +++ b/activesupport/lib/active_support/core_ext/object/inclusion.rb @@ -6,6 +6,7 @@ class Object # "Konata".in?(characters) # => true # def in?(another_object) + raise ArgumentError.new("You must supply another object that responds to include?") unless another_object.respond_to?(:include?) another_object.include?(self) end end -- cgit v1.2.3 From cd233dd87e6f0f9113a397531d37075cfa7c6526 Mon Sep 17 00:00:00 2001 From: Jared McFarland Date: Fri, 15 Apr 2011 00:04:21 +0800 Subject: Only rescue a thrown NoMethodError, don't preemptively check for #include?; added tests --- activesupport/lib/active_support/core_ext/object/inclusion.rb | 5 ++++- activesupport/test/core_ext/object/inclusion_test.rb | 4 ++++ 2 files changed, 8 insertions(+), 1 deletion(-) (limited to 'activesupport') diff --git a/activesupport/lib/active_support/core_ext/object/inclusion.rb b/activesupport/lib/active_support/core_ext/object/inclusion.rb index f30333fd02..51cfc62f2b 100644 --- a/activesupport/lib/active_support/core_ext/object/inclusion.rb +++ b/activesupport/lib/active_support/core_ext/object/inclusion.rb @@ -5,8 +5,11 @@ class Object # characters = ["Konata", "Kagami", "Tsukasa"] # "Konata".in?(characters) # => true # + # This will throw an ArgumentError if the supplied argument doesnt not respond + # to +#include?+. def in?(another_object) - raise ArgumentError.new("You must supply another object that responds to include?") unless another_object.respond_to?(:include?) another_object.include?(self) + rescue NoMethodError + raise ArgumentError.new("The parameter passed to #in? must respond to #include?") end end diff --git a/activesupport/test/core_ext/object/inclusion_test.rb b/activesupport/test/core_ext/object/inclusion_test.rb index 382271d42d..1de857d678 100644 --- a/activesupport/test/core_ext/object/inclusion_test.rb +++ b/activesupport/test/core_ext/object/inclusion_test.rb @@ -43,4 +43,8 @@ class InTest < Test::Unit::TestCase assert A.in?(C) assert !A.in?(A) end + + def test_no_method_catching + assert_raise(ArgumentError) { 1.in?(1) } + end end -- cgit v1.2.3