提交 99c64829 编写于 作者: M Michael Koziarski

* Add Range#overlaps?(range), Range#include?(range), and Range#step without a...

* 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
上级 c2602354
*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]
......
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
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
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
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
......@@ -10,4 +10,55 @@ def test_to_s_from_times
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
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册