Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #9746: range_ext.patch

File range_ext.patch, 5.0 kB (added by brandon, 1 year ago)

add #overlaps?(range), #include?(range), and #step without a block

  • activesupport/test/core_ext/range_ext_test.rb

    old new  
    1010    date_range = Time.utc(2005, 12, 10, 15, 30)..Time.utc(2005, 12, 10, 17, 30) 
    1111    assert_equal "BETWEEN '2005-12-10 15:30:00' AND '2005-12-10 17:30:00'", date_range.to_s(:db) 
    1212  end 
     13   
     14  def test_overlaps_last_inclusive 
     15    assert (1..5).overlaps?(5..10) 
     16  end 
     17 
     18  def test_overlaps_last_exclusive 
     19    assert !(1...5).overlaps?(5..10) 
     20  end 
     21 
     22  def test_overlaps_first_inclusive 
     23    assert (5..10).overlaps?(1..5) 
     24  end 
     25   
     26  def test_overlaps_first_exclusive 
     27    assert !(5..10).overlaps?(1...5) 
     28  end 
     29   
     30  def test_should_include_identical_inclusive 
     31    assert (1..10).include?(1..10) 
     32  end 
     33 
     34  def test_should_include_identical_exclusive 
     35    assert (1...10).include?(1...10) 
     36  end 
     37 
     38  def test_should_include_other_with_exlusive_end 
     39    assert (1..10).include?(1...10) 
     40  end 
     41 
     42  def test_exclusive_end_should_not_include_identical_with_inclusive_end 
     43    assert !(1...10).include?(1..10) 
     44  end 
     45 
     46  def test_should_not_include_overlapping_first 
     47    assert !(2..8).include?(1..3) 
     48  end 
     49   
     50  def test_should_not_include_overlapping_last 
     51    assert !(2..8).include?(5..9) 
     52  end 
     53   
     54  def test_blockless_step 
     55    assert_equal [1,3,5,7,9], (1..10).step(2) 
     56  end 
     57 
     58  def test_original_step 
     59    array = [] 
     60    (1..10).step(2) {|i| array << i } 
     61    assert_equal [1,3,5,7,9], array 
     62  end 
     63   
    1364end 
  • activesupport/lib/active_support/core_ext/range/include_range.rb

    old new  
     1module ActiveSupport #:nodoc: 
     2  module CoreExtensions #:nodoc: 
     3    module Range #:nodoc: 
     4      # Check if a Range includes another Range 
     5      module IncludeRange 
     6 
     7        def self.included(klass) #:nodoc: 
     8          klass.send(:alias_method_chain, :include?, :range) 
     9        end         
     10         
     11        def include_with_range?(value) 
     12          if value.is_a?(::Range) 
     13            operator = exclude_end? ? :< : :<= 
     14            end_value = value.exclude_end? ? last.succ : last 
     15            include?(value.first) && (value.last <=> end_value).send(operator, 0) 
     16          else 
     17            include_without_range?(value) 
     18          end 
     19        end 
     20         
     21      end 
     22    end 
     23  end 
     24end 
  • activesupport/lib/active_support/core_ext/range/overlaps.rb

    old new  
     1module ActiveSupport #:nodoc: 
     2  module CoreExtensions #:nodoc: 
     3    module Range #:nodoc: 
     4      # Check if Ranges overlap 
     5      module Overlaps 
     6         
     7        def overlaps?(other) 
     8          include?(other.first) || other.include?(first) 
     9        end         
     10         
     11      end 
     12    end 
     13  end 
     14end 
  • activesupport/lib/active_support/core_ext/range/blockless_step.rb

    old new  
     1module ActiveSupport #:nodoc: 
     2  module CoreExtensions #:nodoc: 
     3    module Range #:nodoc: 
     4      # Return and array when step is called without a block 
     5      module BlocklessStep 
     6 
     7        def self.included(klass) #:nodoc: 
     8          klass.send(:alias_method, :step_with_block, :step) 
     9          klass.send(:alias_method, :step, :step_without_block) 
     10        end         
     11         
     12        def step_without_block(value, &block) 
     13          if block_given? 
     14            step_with_block(value, &block) 
     15          else 
     16            returning [] do |array| 
     17              step_with_block(value) {|step| array << step } 
     18            end 
     19          end 
     20        end 
     21 
     22      end 
     23    end 
     24  end 
     25end 
  • activesupport/lib/active_support/core_ext/range.rb

    old new  
    11require File.dirname(__FILE__) + '/range/conversions' 
     2require File.dirname(__FILE__) + '/range/overlaps' 
     3require File.dirname(__FILE__) + '/range/include_range' 
     4require File.dirname(__FILE__) + '/range/blockless_step' 
    25 
    36class Range #:nodoc: 
    47  include ActiveSupport::CoreExtensions::Range::Conversions 
     8  include ActiveSupport::CoreExtensions::Range::Overlaps 
     9  include ActiveSupport::CoreExtensions::Range::IncludeRange 
     10  include ActiveSupport::CoreExtensions::Range::BlocklessStep 
    511end