backports 2.6.7 → 2.7.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,4 +1,10 @@
1
- = Packable --- History
1
+ = Backports --- History
2
+
3
+ == Version 2.7.0 - January 14th, 2013
4
+
5
+ * Added some features of 2.0.0 (must be required explicitly until official release):
6
+ * Enumerable#lazy
7
+ * Enumerator::Lazy
2
8
 
3
9
  == Version 2.6.0 - May 29th, 2012
4
10
 
@@ -1,6 +1,6 @@
1
1
  = Backports Library
2
2
 
3
- * Yearning to use some of the new cool features in 1.9.3 while using 1.8.6?
3
+ * Yearning to use some of the new cool features in Ruby 2.0.0 while using 1.8.6?
4
4
  * One of your client is stuck with Ruby 1.8.6 but you want to use a gem using some features of 1.8.7?
5
5
  * Can't remember if you can use Array#sample or String#each_char on a friend's box?
6
6
 
@@ -25,7 +25,7 @@ Let's be a bit more precise about the breaking code business. It is of course en
25
25
 
26
26
  A real incompatibility is, for example, <tt>Module::instance_methods</tt> which returns strings in 1.8 and symbols in 1.9. No change can be made without the risk of breaking existing code. Such incompatibilities are left unchanged, although you can require some of these changes in addition (see below)
27
27
 
28
- All features of 1.8.7 are backported (well, almost all, see the exception list bellow), and many of 1.9 are also.
28
+ All features of 1.8.7 are backported (well, almost all, see the exception list bellow), and many of 1.9 are also. Some features of 2.0.0 are backported, but not loaded until a final version of Ruby 2.0.0 is released.
29
29
 
30
30
  Some generic and self-contained features of active-support are also included. By simple I mean that String#camelcase is there, but #pluralize isn't.
31
31
 
@@ -191,6 +191,16 @@ Some features of Ruby 1.9.3 have been backported:
191
191
 
192
192
  To include all Ruby backports but not those of Rails, <tt>require "backports/1.9.3"</tt> (or "backports/1.9")
193
193
 
194
+ == Ruby 2.0.0
195
+
196
+ Some features of Ruby 2.0.0 have been backported:
197
+
198
+ * Enumerable
199
+ * +lazy+
200
+ * Enumerator::Lazy
201
+
202
+ *Note*: Ruby 2.0.0 will not included by default until 2.0.0 is released (and specs are thus finalized). To play around with these today, <tt>require "backports/2.0"</tt>.
203
+
194
204
  == Rails
195
205
 
196
206
  Some generic methods from Rails methods have been copied:
@@ -66,8 +66,8 @@ class Array
66
66
  out.concat(array)
67
67
  else
68
68
  array.each do |o|
69
- if o.respond_to? :to_ary
70
- recursively_flatten_finite(o.to_ary, out, level - 1)
69
+ if ary = Backports.is_array?(o)
70
+ recursively_flatten_finite(ary, out, level - 1)
71
71
  ret = self
72
72
  else
73
73
  out << o
@@ -178,7 +178,7 @@ class Array
178
178
  result[n..size] = []
179
179
  result
180
180
  end unless method_defined? :sample
181
-
181
+
182
182
  # shift. Standard in Ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/Array.html]
183
183
  unless ([1].shift(1) rescue false)
184
184
  def shift_with_optional_argument(n = Backports::Undefined)
@@ -205,4 +205,4 @@ class Array
205
205
  self
206
206
  end unless method_defined? :shuffle!
207
207
 
208
- end
208
+ end
@@ -8,8 +8,7 @@ class Hash
8
8
  return constructor_without_key_value_pair_form(*args) unless args.length == 1 && args.first.is_a?(Array)
9
9
  h = {}
10
10
  args.first.each do |arr|
11
- next unless arr.respond_to? :to_ary
12
- arr = arr.to_ary
11
+ next unless arr == Backports.is_array?(arr)
13
12
  next unless (1..2).include? arr.size
14
13
  h[arr.at(0)] = arr.at(1)
15
14
  end
@@ -40,4 +39,4 @@ class Hash
40
39
 
41
40
  # Standard in Ruby 1.8.7+. See official documentation[http://ruby-doc.org/core-1.9/classes/Hash.html]
42
41
  Backports.alias_method self, :key, :index
43
- end
42
+ end
@@ -1,4 +1,4 @@
1
- # require this file to load all the backports of Ruby
1
+ # require this file to load all the backports of Ruby 1.9.x
2
2
 
3
3
  require File.expand_path(File.dirname(__FILE__) + "/tools")
4
4
  Backports.require_relative "1.9.3"
@@ -0,0 +1,4 @@
1
+ # require this file to load all the backports up to Ruby 2.0.0
2
+
3
+ Backports.require_relative "1.9"
4
+ Backports.require_relative_dir "2.0.0"
@@ -0,0 +1,9 @@
1
+ module Enumerable
2
+ def lazy
3
+ Enumerator::Lazy.new(self){|yielder, *val| yielder.<<(*val)}
4
+ end unless method_defined? :lazy
5
+ end
6
+
7
+ class Enumerator
8
+ autoload :Lazy, File.expand_path(File.dirname(__FILE__) + "/enumerator/lazy")
9
+ end
@@ -0,0 +1,160 @@
1
+ class Enumerator
2
+ class Yielder
3
+ # Current API for Lazy Enumerator does not provide an easy way
4
+ # to handle internal state. We "cheat" and use yielder to hold it for us.
5
+ # A new yielder is created when generating or after a `rewind`.
6
+ # This way we avoid issues like http://bugs.ruby-lang.org/issues/7691
7
+ # or http://bugs.ruby-lang.org/issues/7696
8
+ attr_accessor :backports_memo
9
+ end
10
+
11
+ class Lazy < Enumerator
12
+ @@done = Object.new # used internally to bail out of an iteration
13
+
14
+ alias_method :non_lazy_cycle, :cycle # cycle must be handled in a tricky way
15
+ @@cycler = Struct.new(:object, :n)
16
+
17
+ def initialize(obj)
18
+ return super(obj.object, :non_lazy_cycle, obj.n) if obj.is_a?(@@cycler)
19
+ raise ArgumentError, "must supply a block" unless block_given?
20
+ super() do |yielder, *args|
21
+ catch @@done do
22
+ obj.each(*args) do |*x|
23
+ yield yielder, *x
24
+ end
25
+ end
26
+ end
27
+ end
28
+
29
+ alias_method :force, :to_a
30
+
31
+ def lazy
32
+ self
33
+ end
34
+
35
+ def map
36
+ raise ArgumentError, "tried to call lazy map without a block" unless block_given?
37
+ Lazy.new(self) do |yielder, *values|
38
+ yielder << yield(*values)
39
+ end
40
+ end
41
+ alias_method :collect, :map
42
+
43
+ def select
44
+ raise ArgumentError, "tried to call lazy select without a block" unless block_given?
45
+ Lazy.new(self) do |yielder, *values|
46
+ values = values.first unless values.size > 1
47
+ yielder.yield values if yield values
48
+ end
49
+ end
50
+ alias_method :find_all, :select
51
+
52
+ def reject
53
+ raise ArgumentError, "tried to call lazy reject without a block" unless block_given?
54
+ Lazy.new(self) do |yielder, *values|
55
+ values = values.first unless values.size > 1
56
+ yielder.yield(values) unless yield values
57
+ end
58
+ end
59
+
60
+ def grep(pattern)
61
+ if block_given?
62
+ # Split for performance
63
+ Lazy.new(self) do |yielder, *values|
64
+ values = values.first unless values.size > 1
65
+ yielder.yield(yield(values)) if pattern === values
66
+ end
67
+ else
68
+ Lazy.new(self) do |yielder, *values|
69
+ values = values.first unless values.size > 1
70
+ yielder.yield(values) if pattern === values
71
+ end
72
+ end
73
+ end
74
+
75
+ def drop(n)
76
+ n = Backports::coerce_to_int(n)
77
+ Lazy.new(self) do |yielder, *values|
78
+ data = yielder.backports_memo ||= {remain: n}
79
+ if data[:remain] > 0
80
+ data[:remain] -= 1
81
+ else
82
+ yielder.yield(*values)
83
+ end
84
+ end
85
+ end
86
+
87
+ def drop_while
88
+ raise ArgumentError, "tried to call lazy drop_while without a block" unless block_given?
89
+ Lazy.new(self) do |yielder, *values|
90
+ data = yielder.backports_memo ||= {dropping: true}
91
+ yielder.yield(*values) unless data[:dropping] &&= yield(*values)
92
+ end
93
+ end
94
+
95
+ def take(n)
96
+ n = Backports::coerce_to_int(n)
97
+ raise ArgumentError, 'attempt to take negative size' if n < 0
98
+ return Lazy.new([]){} if n == 0
99
+ Lazy.new(self) do |yielder, *values|
100
+ data = yielder.backports_memo ||= {remain: n}
101
+ yielder.yield(*values)
102
+ throw @@done if (data[:remain] -= 1) == 0
103
+ end
104
+ end
105
+
106
+ def take_while
107
+ raise ArgumentError, "tried to call lazy take_while without a block" unless block_given?
108
+ Lazy.new(self) do |yielder, *values|
109
+ throw @@done unless yield(*values)
110
+ yielder.yield(*values)
111
+ end
112
+ end
113
+
114
+ def flat_map
115
+ raise ArgumentError, "tried to call lazy flat_map without a block" unless block_given?
116
+ Lazy.new(self) do |yielder, *values|
117
+ result = yield(*values)
118
+ if ary = Backports.is_array?(result) || (result.respond_to?(:each) && result.respond_to?(:force))
119
+ ary.each{|x| yielder << x }
120
+ else
121
+ yielder << result
122
+ end
123
+ end
124
+ end
125
+ alias_method :collect_concat, :flat_map
126
+
127
+ def cycle(n = nil)
128
+ return super if block_given?
129
+ Lazy.new(@@cycler.new(self, n))
130
+ end
131
+
132
+ def zip(*args)
133
+ return super if block_given?
134
+ arys = args.map{ |arg| Backports.is_array?(arg) }
135
+ if arys.all?
136
+ # Handle trivial case of multiple array arguments separately
137
+ # by avoiding Enumerator#next for efficiency & compatibility
138
+ Lazy.new(self) do |yielder, *values|
139
+ data = yielder.backports_memo ||= {iter: 0}
140
+ values = values.first unless values.size > 1
141
+ yielder << arys.map{|ary| ary[data[:iter]]}.unshift(values)
142
+ data[:iter] += 1
143
+ end
144
+ else
145
+ Lazy.new(self) do |yielder, *values|
146
+ enums = yielder.backports_memo ||= args.map(&:each)
147
+ values = values.first unless values.size > 1
148
+ others = enums.map do |arg|
149
+ begin
150
+ arg.next
151
+ rescue StopIteration
152
+ nil
153
+ end
154
+ end
155
+ yielder << others.unshift(values)
156
+ end
157
+ end
158
+ end
159
+ end
160
+ end
@@ -0,0 +1,4 @@
1
+ # require this file to load all the backports of Ruby 2.0.x
2
+
3
+ require File.expand_path(File.dirname(__FILE__) + "/tools")
4
+ Backports.require_relative "2.0.0"
@@ -219,6 +219,10 @@ module Backports
219
219
  coerce_to(obj, String, :to_str)
220
220
  end
221
221
 
222
+ def self.is_array?(obj)
223
+ coerce_to(obj, Array, :to_ary) if obj.respond_to? :to_ary
224
+ end
225
+
222
226
  def self.try_convert(obj, cls, meth)
223
227
  return obj if obj.kind_of?(cls)
224
228
  return nil unless obj.respond_to?(meth)
@@ -1,3 +1,3 @@
1
1
  module Backports
2
- VERSION = "2.6.7"
2
+ VERSION = "2.7.0"
3
3
  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: 2.6.7
4
+ version: 2.7.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-01-15 00:00:00.000000000 Z
12
+ date: 2013-01-18 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Essential backports that enable some of the really nice features of ruby
15
15
  1.8.7, ruby 1.9 and rails from ruby 1.8.6 and earlier.
@@ -101,6 +101,10 @@ files:
101
101
  - lib/backports/1.9.3/io.rb
102
102
  - lib/backports/1.9.3/string.rb
103
103
  - lib/backports/1.9.rb
104
+ - lib/backports/2.0.0.rb
105
+ - lib/backports/2.0.0/enumerable.rb
106
+ - lib/backports/2.0.0/enumerator/lazy.rb
107
+ - lib/backports/2.0.rb
104
108
  - lib/backports/basic_object.rb
105
109
  - lib/backports/force/array_map.rb
106
110
  - lib/backports/force/enumerable_map.rb