backports 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.rdoc ADDED
@@ -0,0 +1,14 @@
1
+ = Packable --- History
2
+
3
+ == Version 1.1 - April 11, 2009
4
+
5
+ * Array
6
+ * +flatten+, <tt>flatten!</tt>
7
+ * +find_index+, +index+
8
+
9
+ * Hash
10
+ * +reverse_merge+, <tt>reverse_merge!</tt>
11
+
12
+ == Version 1.0 - April 2nd, 2009
13
+
14
+ === Initial release.
data/README.rdoc CHANGED
@@ -10,10 +10,14 @@ Conditions for inclusion:
10
10
  The first and second rules avoids conflicts in future and the past respectively. Because of the second rule, incompatibilities between 1.8 and 1.9 methods are left alone.
11
11
  For example, <tt>Module::instance_methods</tt> returns strings in 1.8 and symbols in 1.9; no change can be made without the risk of breaking existing code.
12
12
 
13
- More complex features of activesupport (even things like String::pluralize), won't be included. <tt>require 'activesupport'</tt> if you need them!
13
+ More complex features of activesupport (even things like <tt>String::pluralize</tt>), won't be included. <tt>require 'activesupport'</tt> if you need them!
14
14
 
15
15
  I've added those as I need them; pull requests welcome (with tests for ruby 1.9 backports)
16
16
 
17
+ == Compatibility
18
+
19
+ Works with ruby 1.8 & 1.9
20
+
17
21
  = List of backports
18
22
 
19
23
  * Symbol
@@ -33,15 +37,21 @@ I've added those as I need them; pull requests welcome (with tests for ruby 1.9
33
37
  * Hash
34
38
  * <tt>Hash[[[:foo, :bar],[:hello, "world"]]] ==> {:foo => :bar, :hello => "world"}</tt>
35
39
  * +key+
36
- * +symbolize_keys+, +symbolize_keys!+
40
+ * +symbolize_keys+, <tt>symbolize_keys!</tt>
41
+ * +reverse_merge+, <tt>reverse_merge!</tt>
37
42
  * Enumerable
38
43
  * +sum+
39
44
  * +find_index+
40
45
  * +take+, +take_while+, +drop+, +drop_while+
41
46
  * +first+
47
+ * Array
48
+ * +flatten+, <tt>flatten!</tt>
49
+ * +find_index+, +index+
42
50
  * Fixnum
43
51
  * <tt>odd?</tt>, <tt>even?</tt>
44
52
 
53
+ Finally, there is no need to <tt>require 'enumerator'</tt> in older ruby, and +Enumerator+ can be accessed directly (instead of <tt>Enumerable::Enumerator</p>)
54
+
45
55
  = License
46
56
 
47
57
  +backports+ is released under the terms of the MIT License, see the included LICENSE file.
data/Rakefile ADDED
@@ -0,0 +1,91 @@
1
+ # encoding: utf-8
2
+ require 'rubygems'
3
+ require 'rake'
4
+
5
+ begin
6
+ require 'jeweler'
7
+ Jeweler::Tasks.new do |gem|
8
+ gem.name = "backports"
9
+ gem.summary = "Backports or ruby 1.8.7+ & rails for older ruby."
10
+ gem.email = "github@marc-andre.ca"
11
+ gem.homepage = "http://github.com/marcandre/backports"
12
+ gem.authors = ["Marc-André Lafortune"]
13
+ gem.rubyforge_project = "backports"
14
+
15
+ gem.description = <<-EOS
16
+ Essential backports that enable some of the really nice features of ruby 1.8.7, ruby 1.9 and rails from ruby 1.8.6 and earlier.
17
+ EOS
18
+ gem.has_rdoc = true
19
+ gem.rdoc_options << '--title' << 'Backports library' <<
20
+ '--main' << 'README.rdoc' <<
21
+ '--line-numbers' << '--inline-source'
22
+
23
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
24
+ end
25
+ rescue LoadError
26
+ puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
27
+ end
28
+
29
+ require 'rake/testtask'
30
+ Rake::TestTask.new(:test) do |test|
31
+ test.libs << 'lib' << 'test'
32
+ test.pattern = 'test/**/*_test.rb'
33
+ test.verbose = false
34
+ end
35
+
36
+ begin
37
+ require 'rcov/rcovtask'
38
+ Rcov::RcovTask.new do |test|
39
+ test.libs << 'test'
40
+ test.pattern = 'test/**/*_test.rb'
41
+ test.verbose = true
42
+ end
43
+ rescue LoadError
44
+ task :rcov do
45
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
46
+ end
47
+ end
48
+
49
+
50
+ task :default => :test
51
+
52
+ require 'rake/rdoctask'
53
+ Rake::RDocTask.new do |rdoc|
54
+ if File.exist?('VERSION.yml')
55
+ config = YAML.load(File.read('VERSION.yml'))
56
+ version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
57
+ else
58
+ version = ""
59
+ end
60
+
61
+ rdoc.rdoc_dir = 'rdoc'
62
+ rdoc.title = "backports #{version}"
63
+ rdoc.rdoc_files.include('README*')
64
+ rdoc.rdoc_files.include('lib/**/*.rb')
65
+ end
66
+
67
+ begin
68
+ require 'rake/contrib/sshpublisher'
69
+ namespace :rubyforge do
70
+
71
+ desc "Release gem and RDoc documentation to RubyForge"
72
+ task :release => ["rubyforge:release:gem", "rubyforge:release:docs"]
73
+
74
+ namespace :release do
75
+ desc "Publish RDoc to RubyForge."
76
+ task :docs => [:rdoc] do
77
+ config = YAML.load(
78
+ File.read(File.expand_path('~/.rubyforge/user-config.yml'))
79
+ )
80
+
81
+ host = "#{config['username']}@rubyforge.org"
82
+ remote_dir = "/var/www/gforge-projects/backports/"
83
+ local_dir = 'rdoc'
84
+
85
+ Rake::SshDirPublisher.new(host, remote_dir, local_dir).upload
86
+ end
87
+ end
88
+ end
89
+ rescue LoadError
90
+ puts "Rake SshDirPublisher is unavailable or your rubyforge environment is not configured."
91
+ end
data/VERSION.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  ---
2
2
  :major: 1
3
- :minor: 0
3
+ :minor: 1
4
4
  :patch: 0
data/lib/backports.rb CHANGED
@@ -10,6 +10,6 @@ module Kernel
10
10
  end unless method_defined? :require_relative
11
11
  end
12
12
 
13
- %w(object module enumerable string symbol fixnum hash).each do |lib|
13
+ %w(object module enumerable array string symbol fixnum hash).each do |lib|
14
14
  require_relative "backports/#{lib}"
15
15
  end
@@ -0,0 +1,60 @@
1
+ class Array
2
+ # flatten & flatten!, standard in ruby 1.9. See official documentation[http://ruby-doc.org/core-1.9/classes/Array.html]
3
+ unless ([[]].flatten(1) rescue false)
4
+
5
+ # Recursively flatten any contained Arrays into an one-dimensional result.
6
+ # Adapted from rubinius'
7
+ def flatten_with_optional_argument(level=nil)
8
+ return flatten_without_optional_argument unless level || level == (1/0.0)
9
+ dup.flatten!(level) || self
10
+ end
11
+
12
+ # Flattens self in place as #flatten. If no changes are
13
+ # made, returns nil, otherwise self.
14
+ # Adapted from rubinius'
15
+ def flatten_with_optional_argument!(level=nil)
16
+ return flatten_without_optional_argument! unless level || level == (1/0.0)
17
+
18
+ ret, out = nil, []
19
+ ret = recursively_flatten_finite(self, out, level)
20
+ replace(out) if ret
21
+ ret
22
+ end
23
+
24
+ alias_method_chain :flatten, :optional_argument
25
+ alias_method_chain :flatten!, :optional_argument
26
+
27
+ # Helper to recurse through flattening since the method
28
+ # is not allowed to recurse itself. Detects recursive structures.
29
+ # Adapted from rubinius'; recursion guards are not needed because level is finite
30
+ def recursively_flatten_finite(array, out, level)
31
+ ret = nil
32
+ if level <= 0
33
+ out.concat(array)
34
+ else
35
+ array.each do |o|
36
+ if o.respond_to? :to_ary
37
+ recursively_flatten_finite(o.to_ary, out, level - 1)
38
+ ret = self
39
+ else
40
+ out << o
41
+ end
42
+ end
43
+ end
44
+ ret
45
+ end
46
+ private :recursively_flatten_finite
47
+ end # flatten & flatten!
48
+
49
+ # index
50
+ unless ([1].index{true} rescue false)
51
+ def index_with_block(*arg)
52
+ return index_without_block(*arg) unless block_given? && arg.empty?
53
+ each_with_index{|o,i| return i if yield o}
54
+ return nil
55
+ end
56
+
57
+ alias_method_chain :index, :block
58
+ alias_method :find_index, :index
59
+ end
60
+ end
@@ -23,4 +23,15 @@ class Hash
23
23
 
24
24
  # Standard in ruby 1.9. See official documentation[http://ruby-doc.org/core-1.9/classes/Hash.html]
25
25
  alias_method :key, :index unless method_defined? :key
26
+
27
+ # Standard in rails. See official documentation[http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/Hash/Keys.html]
28
+ def reverse_merge(other_hash)
29
+ other_hash.merge(self)
30
+ end
31
+
32
+ # Standard in rails. See official documentation[http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/Hash/Keys.html]
33
+ def reverse_merge!(other_hash)
34
+ replace(reverse_merge(other_hash))
35
+ end
36
+
26
37
  end
@@ -62,4 +62,32 @@ class BackportsTest < Test::Unit::TestCase
62
62
  assert_equal({1 => 2, 3 => 4}, Hash[[[1,2],[3,4]]])
63
63
  end
64
64
  end
65
+
66
+ context "flatten" do
67
+ should "conform to doc" do
68
+ s = [ 1, 2, 3 ] #=> [1, 2, 3]
69
+ t = [ 4, 5, 6, [7, 8] ] #=> [4, 5, 6, [7, 8]]
70
+ a = [ s, t, 9, 10 ] #=> [[1, 2, 3], [4, 5, 6, [7, 8]], 9, 10]
71
+ assert_equal [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], a.flatten
72
+ a = [ 1, 2, [3, [4, 5] ] ]
73
+ assert_equal [1, 2, 3, [4, 5]], a.flatten(1)
74
+ end
75
+
76
+ should "conform work for recursive arrays" do
77
+ x=[]
78
+ x.push(x,x)
79
+ 4.times {|n| assert_equal 2 << n, x.flatten(n).length}
80
+ assert_raises(ArgumentError) {x.flatten}
81
+ end
82
+ end
83
+
84
+ context "index" do
85
+ should "conform to doc" do
86
+ a = [ "a", "b", "c" ]
87
+ assert_equal 1, a.index("b")
88
+ assert_equal nil, a.index("z")
89
+ assert_equal 1, a.index{|x|x=="b"}
90
+ end
91
+ end
92
+
65
93
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: backports
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - "Marc-Andr\xC3\xA9 Lafortune"
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-04-01 00:00:00 -04:00
12
+ date: 2009-04-11 00:00:00 -04:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -20,12 +20,16 @@ executables: []
20
20
  extensions: []
21
21
 
22
22
  extra_rdoc_files:
23
- - README.rdoc
24
23
  - LICENSE
24
+ - README.rdoc
25
25
  files:
26
+ - CHANGELOG.rdoc
27
+ - LICENSE
26
28
  - README.rdoc
29
+ - Rakefile
27
30
  - VERSION.yml
28
- - lib/backports
31
+ - lib/backports.rb
32
+ - lib/backports/array.rb
29
33
  - lib/backports/enumerable.rb
30
34
  - lib/backports/fixnum.rb
31
35
  - lib/backports/hash.rb
@@ -33,22 +37,19 @@ files:
33
37
  - lib/backports/object.rb
34
38
  - lib/backports/string.rb
35
39
  - lib/backports/symbol.rb
36
- - lib/backports.rb
37
40
  - test/backports_test.rb
38
41
  - test/test_helper.rb
39
- - LICENSE
40
42
  has_rdoc: true
41
43
  homepage: http://github.com/marcandre/backports
42
44
  post_install_message:
43
45
  rdoc_options:
46
+ - --charset=UTF-8
44
47
  - --title
45
48
  - Backports library
46
49
  - --main
47
50
  - README.rdoc
48
51
  - --line-numbers
49
52
  - --inline-source
50
- - --inline-source
51
- - --charset=UTF-8
52
53
  require_paths:
53
54
  - lib
54
55
  required_ruby_version: !ruby/object:Gem::Requirement
@@ -65,10 +66,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
65
66
  version:
66
67
  requirements: []
67
68
 
68
- rubyforge_project: marcandre
69
+ rubyforge_project: backports
69
70
  rubygems_version: 1.3.1
70
71
  signing_key:
71
72
  specification_version: 2
72
73
  summary: Backports or ruby 1.8.7+ & rails for older ruby.
73
- test_files: []
74
-
74
+ test_files:
75
+ - test/backports_test.rb
76
+ - test/test_helper.rb