From 42723e3a0c74e25c45c0f763740a3115302da4ac Mon Sep 17 00:00:00 2001 From: Nicholas Seckar Date: Tue, 19 Jul 2005 12:58:01 +0000 Subject: Factor Fixnum and Bignum extensions into Integer class git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1863 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activesupport/CHANGELOG | 2 ++ .../lib/active_support/core_ext/fixnum.rb | 12 ------- .../lib/active_support/core_ext/fixnum/even_odd.rb | 24 -------------- .../active_support/core_ext/fixnum/inflections.rb | 15 --------- .../lib/active_support/core_ext/integer.rb | 7 ++++ .../active_support/core_ext/integer/even_odd.rb | 24 ++++++++++++++ .../active_support/core_ext/integer/inflections.rb | 15 +++++++++ activesupport/test/core_ext/fixnum_ext_test.rb | 36 -------------------- activesupport/test/core_ext/integer_ext_test.rb | 38 ++++++++++++++++++++++ 9 files changed, 86 insertions(+), 87 deletions(-) delete mode 100644 activesupport/lib/active_support/core_ext/fixnum.rb delete mode 100644 activesupport/lib/active_support/core_ext/fixnum/even_odd.rb delete mode 100644 activesupport/lib/active_support/core_ext/fixnum/inflections.rb create mode 100644 activesupport/lib/active_support/core_ext/integer.rb create mode 100644 activesupport/lib/active_support/core_ext/integer/even_odd.rb create mode 100644 activesupport/lib/active_support/core_ext/integer/inflections.rb delete mode 100644 activesupport/test/core_ext/fixnum_ext_test.rb create mode 100644 activesupport/test/core_ext/integer_ext_test.rb (limited to 'activesupport') diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG index a2402c9187..51abed18d9 100644 --- a/activesupport/CHANGELOG +++ b/activesupport/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Factor Fixnum and Bignum extensions into Integer extensions [Nicholas Seckar] + * Hooked #ordinalize into Fixnum and Bignum classes. [Nicholas Seckar, danp] * Added stripping of _id to humanize, so "employee_id" becomes "Employee" #1574 [Justin French] diff --git a/activesupport/lib/active_support/core_ext/fixnum.rb b/activesupport/lib/active_support/core_ext/fixnum.rb deleted file mode 100644 index 519408fb0d..0000000000 --- a/activesupport/lib/active_support/core_ext/fixnum.rb +++ /dev/null @@ -1,12 +0,0 @@ -require File.dirname(__FILE__) + '/fixnum/even_odd' -require File.dirname(__FILE__) + '/fixnum/inflections' - -class Fixnum #:nodoc: - include ActiveSupport::CoreExtensions::Fixnum::EvenOdd - include ActiveSupport::CoreExtensions::Fixnum::Inflections -end - -class Bignum #:nodoc: - include ActiveSupport::CoreExtensions::Fixnum::EvenOdd - include ActiveSupport::CoreExtensions::Fixnum::Inflections -end \ No newline at end of file diff --git a/activesupport/lib/active_support/core_ext/fixnum/even_odd.rb b/activesupport/lib/active_support/core_ext/fixnum/even_odd.rb deleted file mode 100644 index c0e9da18fa..0000000000 --- a/activesupport/lib/active_support/core_ext/fixnum/even_odd.rb +++ /dev/null @@ -1,24 +0,0 @@ -module ActiveSupport #:nodoc: - module CoreExtensions #:nodoc: - module Fixnum #:nodoc: - # For checking if a fixnum is even or odd. - # * 1.even? # => false - # * 1.odd? # => true - # * 2.even? # => true - # * 2.odd? # => false - module EvenOdd - def multiple_of?(number) - self % number == 0 - end - - def even? - multiple_of? 2 - end - - def odd? - !even? - end - end - end - end -end diff --git a/activesupport/lib/active_support/core_ext/fixnum/inflections.rb b/activesupport/lib/active_support/core_ext/fixnum/inflections.rb deleted file mode 100644 index 13c4f1130a..0000000000 --- a/activesupport/lib/active_support/core_ext/fixnum/inflections.rb +++ /dev/null @@ -1,15 +0,0 @@ -require File.dirname(__FILE__) + '/../../inflector' -module ActiveSupport #:nodoc: - module CoreExtensions #:nodoc: - module Fixnum #:nodoc: - module Inflections - # 1.ordinalize # => "1st" - # 3.ordinalize # => "3rd" - # 10.ordinalize # => "10th" - def ordinalize - Inflector.ordinalize(self) - end - end - end - end -end diff --git a/activesupport/lib/active_support/core_ext/integer.rb b/activesupport/lib/active_support/core_ext/integer.rb new file mode 100644 index 0000000000..9346b88fd5 --- /dev/null +++ b/activesupport/lib/active_support/core_ext/integer.rb @@ -0,0 +1,7 @@ +require File.dirname(__FILE__) + '/integer/even_odd' +require File.dirname(__FILE__) + '/integer/inflections' + +class Integer #:nodoc: + include ActiveSupport::CoreExtensions::Integer::EvenOdd + include ActiveSupport::CoreExtensions::Integer::Inflections +end diff --git a/activesupport/lib/active_support/core_ext/integer/even_odd.rb b/activesupport/lib/active_support/core_ext/integer/even_odd.rb new file mode 100644 index 0000000000..3762308cc3 --- /dev/null +++ b/activesupport/lib/active_support/core_ext/integer/even_odd.rb @@ -0,0 +1,24 @@ +module ActiveSupport #:nodoc: + module CoreExtensions #:nodoc: + module Integer #:nodoc: + # For checking if a fixnum is even or odd. + # * 1.even? # => false + # * 1.odd? # => true + # * 2.even? # => true + # * 2.odd? # => false + module EvenOdd + def multiple_of?(number) + self % number == 0 + end + + def even? + multiple_of? 2 + end + + def odd? + !even? + end + end + end + end +end diff --git a/activesupport/lib/active_support/core_ext/integer/inflections.rb b/activesupport/lib/active_support/core_ext/integer/inflections.rb new file mode 100644 index 0000000000..a86c4cc26c --- /dev/null +++ b/activesupport/lib/active_support/core_ext/integer/inflections.rb @@ -0,0 +1,15 @@ +require File.dirname(__FILE__) + '/../../inflector' +module ActiveSupport #:nodoc: + module CoreExtensions #:nodoc: + module Integer #:nodoc: + module Inflections + # 1.ordinalize # => "1st" + # 3.ordinalize # => "3rd" + # 10.ordinalize # => "10th" + def ordinalize + Inflector.ordinalize(self) + end + end + end + end +end diff --git a/activesupport/test/core_ext/fixnum_ext_test.rb b/activesupport/test/core_ext/fixnum_ext_test.rb deleted file mode 100644 index 66297fc378..0000000000 --- a/activesupport/test/core_ext/fixnum_ext_test.rb +++ /dev/null @@ -1,36 +0,0 @@ -require 'test/unit' -require File.dirname(__FILE__) + '/../../lib/active_support/core_ext/fixnum' - -class FixnumExtTest < Test::Unit::TestCase - def test_even - assert [ -2, 0, 2, 4 ].all? { |i| i.even? } - assert ![ -1, 1, 3 ].all? { |i| i.even? } - - assert 22953686867719691230002707821868552601124472329079.odd? - assert !22953686867719691230002707821868552601124472329079.even? - assert 22953686867719691230002707821868552601124472329080.even? - assert !22953686867719691230002707821868552601124472329080.odd? - end - - def test_odd - assert ![ -2, 0, 2, 4 ].all? { |i| i.odd? } - assert [ -1, 1, 3 ].all? { |i| i.odd? } - end - - def test_multiple_of - assert [ -7, 0, 7, 14 ].all? { |i| i.multiple_of? 7 } - assert ![ -7, 0, 7, 14 ].all? { |i| i.multiple_of? 6 } - # test with a prime - assert !22953686867719691230002707821868552601124472329079.multiple_of?(2) - assert !22953686867719691230002707821868552601124472329079.multiple_of?(3) - assert !22953686867719691230002707821868552601124472329079.multiple_of?(5) - assert !22953686867719691230002707821868552601124472329079.multiple_of?(7) - end - - def test_ordinalize - # These tests are mostly just to ensure that the ordinalize method exists - # It's results are tested comprehensively in the inflector test cases. - assert_equal '1st', 1.ordinalize - assert_equal '8th', 8.ordinalize - end -end diff --git a/activesupport/test/core_ext/integer_ext_test.rb b/activesupport/test/core_ext/integer_ext_test.rb new file mode 100644 index 0000000000..4435bc79d3 --- /dev/null +++ b/activesupport/test/core_ext/integer_ext_test.rb @@ -0,0 +1,38 @@ +require 'test/unit' +require File.dirname(__FILE__) + '/../../lib/active_support/core_ext/integer' + +class IntegerExtTest < Test::Unit::TestCase + def test_even + assert [ -2, 0, 2, 4 ].all? { |i| i.even? } + assert ![ -1, 1, 3 ].all? { |i| i.even? } + + assert 22953686867719691230002707821868552601124472329079.odd? + assert !22953686867719691230002707821868552601124472329079.even? + assert 22953686867719691230002707821868552601124472329080.even? + assert !22953686867719691230002707821868552601124472329080.odd? + end + + def test_odd + assert ![ -2, 0, 2, 4 ].all? { |i| i.odd? } + assert [ -1, 1, 3 ].all? { |i| i.odd? } + assert 1000000000000000000000000000000000000000000000000000000001.odd? + end + + def test_multiple_of + [ -7, 0, 7, 14 ].each { |i| assert i.multiple_of?(7) } + [ -7, 7, 14 ].each { |i| assert ! i.multiple_of?(6) } + # test with a prime + assert !22953686867719691230002707821868552601124472329079.multiple_of?(2) + assert !22953686867719691230002707821868552601124472329079.multiple_of?(3) + assert !22953686867719691230002707821868552601124472329079.multiple_of?(5) + assert !22953686867719691230002707821868552601124472329079.multiple_of?(7) + end + + def test_ordinalize + # These tests are mostly just to ensure that the ordinalize method exists + # It's results are tested comprehensively in the inflector test cases. + assert_equal '1st', 1.ordinalize + assert_equal '8th', 8.ordinalize + 1000000000000000000000000000000000000000000000000000000000000000000000.ordinalize + end +end -- cgit v1.2.3