mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
* Add Range#overlaps?(range), Range#include?(range), and Range#step without a block. [brandon] Closes #9746
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7800 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
parent
c2602354d1
commit
99c64829ce
6 changed files with 122 additions and 0 deletions
|
@ -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]
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
|
@ -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
|
14
activesupport/lib/active_support/core_ext/range/overlaps.rb
Normal file
14
activesupport/lib/active_support/core_ext/range/overlaps.rb
Normal file
|
@ -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
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue