aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--activesupport/CHANGELOG2
-rw-r--r--activesupport/lib/active_support/core_ext/range.rb6
-rw-r--r--activesupport/lib/active_support/core_ext/range/blockless_step.rb25
-rw-r--r--activesupport/lib/active_support/core_ext/range/include_range.rb24
-rw-r--r--activesupport/lib/active_support/core_ext/range/overlaps.rb14
-rw-r--r--activesupport/test/core_ext/range_ext_test.rb51
6 files changed, 122 insertions, 0 deletions
diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG
index 77432a7720..b2bf1735fc 100644
--- a/activesupport/CHANGELOG
+++ b/activesupport/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Add Range#overlaps?(range), Range#include?(range), and Range#step without a block. [brandon]
+
* Correct BufferedLogger#level? checks. #9806 [wildchild, Johan Sorensen]
* String#to_xs uses Eric Wong's fast_xs extension, if available, for Builder speedup. http://bogomips.org/fast_xs/ [Jeremy Kemper]
diff --git a/activesupport/lib/active_support/core_ext/range.rb b/activesupport/lib/active_support/core_ext/range.rb
index ee7868a2f8..c3ef624617 100644
--- a/activesupport/lib/active_support/core_ext/range.rb
+++ b/activesupport/lib/active_support/core_ext/range.rb
@@ -1,5 +1,11 @@
require 'active_support/core_ext/range/conversions'
+require File.dirname(__FILE__) + '/range/overlaps'
+require File.dirname(__FILE__) + '/range/include_range'
+require File.dirname(__FILE__) + '/range/blockless_step'
class Range #:nodoc:
include ActiveSupport::CoreExtensions::Range::Conversions
+ include ActiveSupport::CoreExtensions::Range::Overlaps
+ include ActiveSupport::CoreExtensions::Range::IncludeRange
+ include ActiveSupport::CoreExtensions::Range::BlocklessStep
end
diff --git a/activesupport/lib/active_support/core_ext/range/blockless_step.rb b/activesupport/lib/active_support/core_ext/range/blockless_step.rb
new file mode 100644
index 0000000000..4d5e27457b
--- /dev/null
+++ b/activesupport/lib/active_support/core_ext/range/blockless_step.rb
@@ -0,0 +1,25 @@
+module ActiveSupport #:nodoc:
+ module CoreExtensions #:nodoc:
+ module Range #:nodoc:
+ # Return and array when step is called without a block
+ module BlocklessStep
+
+ def self.included(klass) #:nodoc:
+ klass.send(:alias_method, :step_with_block, :step)
+ klass.send(:alias_method, :step, :step_without_block)
+ end
+
+ def step_without_block(value, &block)
+ if block_given?
+ step_with_block(value, &block)
+ else
+ returning [] do |array|
+ step_with_block(value) {|step| array << step }
+ end
+ end
+ end
+
+ end
+ end
+ end
+end \ No newline at end of file
diff --git a/activesupport/lib/active_support/core_ext/range/include_range.rb b/activesupport/lib/active_support/core_ext/range/include_range.rb
new file mode 100644
index 0000000000..a6b4e9ff64
--- /dev/null
+++ b/activesupport/lib/active_support/core_ext/range/include_range.rb
@@ -0,0 +1,24 @@
+module ActiveSupport #:nodoc:
+ module CoreExtensions #:nodoc:
+ module Range #:nodoc:
+ # Check if a Range includes another Range
+ module IncludeRange
+
+ def self.included(klass) #:nodoc:
+ klass.send(:alias_method_chain, :include?, :range)
+ end
+
+ def include_with_range?(value)
+ if value.is_a?(::Range)
+ operator = exclude_end? ? :< : :<=
+ end_value = value.exclude_end? ? last.succ : last
+ include?(value.first) && (value.last <=> end_value).send(operator, 0)
+ else
+ include_without_range?(value)
+ end
+ end
+
+ end
+ end
+ end
+end \ No newline at end of file
diff --git a/activesupport/lib/active_support/core_ext/range/overlaps.rb b/activesupport/lib/active_support/core_ext/range/overlaps.rb
new file mode 100644
index 0000000000..672e97fca4
--- /dev/null
+++ b/activesupport/lib/active_support/core_ext/range/overlaps.rb
@@ -0,0 +1,14 @@
+module ActiveSupport #:nodoc:
+ module CoreExtensions #:nodoc:
+ module Range #:nodoc:
+ # Check if Ranges overlap
+ module Overlaps
+
+ def overlaps?(other)
+ include?(other.first) || other.include?(first)
+ end
+
+ end
+ end
+ end
+end \ No newline at end of file
diff --git a/activesupport/test/core_ext/range_ext_test.rb b/activesupport/test/core_ext/range_ext_test.rb
index e8890ac835..c4945ee985 100644
--- a/activesupport/test/core_ext/range_ext_test.rb
+++ b/activesupport/test/core_ext/range_ext_test.rb
@@ -10,4 +10,55 @@ class RangeTest < Test::Unit::TestCase
date_range = Time.utc(2005, 12, 10, 15, 30)..Time.utc(2005, 12, 10, 17, 30)
assert_equal "BETWEEN '2005-12-10 15:30:00' AND '2005-12-10 17:30:00'", date_range.to_s(:db)
end
+
+ def test_overlaps_last_inclusive
+ assert((1..5).overlaps?(5..10))
+ end
+
+ def test_overlaps_last_exclusive
+ assert !(1...5).overlaps?(5..10)
+ end
+
+ def test_overlaps_first_inclusive
+ assert((5..10).overlaps?(1..5))
+ end
+
+ def test_overlaps_first_exclusive
+ assert !(5..10).overlaps?(1...5)
+ end
+
+ def test_should_include_identical_inclusive
+ assert((1..10).include?(1..10))
+ end
+
+ def test_should_include_identical_exclusive
+ assert((1...10).include?(1...10))
+ end
+
+ def test_should_include_other_with_exlusive_end
+ assert((1..10).include?(1...10))
+ end
+
+ def test_exclusive_end_should_not_include_identical_with_inclusive_end
+ assert !(1...10).include?(1..10)
+ end
+
+ def test_should_not_include_overlapping_first
+ assert !(2..8).include?(1..3)
+ end
+
+ def test_should_not_include_overlapping_last
+ assert !(2..8).include?(5..9)
+ end
+
+ def test_blockless_step
+ assert_equal [1,3,5,7,9], (1..10).step(2)
+ end
+
+ def test_original_step
+ array = []
+ (1..10).step(2) {|i| array << i }
+ assert_equal [1,3,5,7,9], array
+ end
+
end