From 99c64829ce71ecf07b576960ea8ff01dfee61be7 Mon Sep 17 00:00:00 2001 From: Michael Koziarski Date: Mon, 8 Oct 2007 06:05:44 +0000 Subject: * 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 --- activesupport/lib/active_support/core_ext/range.rb | 6 ++++++ .../core_ext/range/blockless_step.rb | 25 ++++++++++++++++++++++ .../active_support/core_ext/range/include_range.rb | 24 +++++++++++++++++++++ .../lib/active_support/core_ext/range/overlaps.rb | 14 ++++++++++++ 4 files changed, 69 insertions(+) create mode 100644 activesupport/lib/active_support/core_ext/range/blockless_step.rb create mode 100644 activesupport/lib/active_support/core_ext/range/include_range.rb create mode 100644 activesupport/lib/active_support/core_ext/range/overlaps.rb (limited to 'activesupport/lib/active_support') 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 -- cgit v1.2.3