polyfill 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a78a97eb49d10c541939893c1df67dc431f56570
4
- data.tar.gz: b56aa89aaafa5cf0ae1238c7a5d6a7b1be9277d8
3
+ metadata.gz: 237acd76888542aca87f442fd066883269ddaee0
4
+ data.tar.gz: ee1bc441b77b3a6277bbcc4385703d945cc79828
5
5
  SHA512:
6
- metadata.gz: ca64b371d0e8287f87f67be903b9b48be65f318ad6ae991d4789114addc5500d785638efc65d3bfd464b5769cc80a07be033cdef17770752a1da55c1a984d2b8
7
- data.tar.gz: bdfce84cb85bb20c013584b259c0612468a103985007824a38b2f635fa4adc86a0f4182ea5aaa362d873eaef0f789a02cb38a4f47df3810d6f792b75ddedf081
6
+ metadata.gz: ccfcb0140e7d2d14da8678133dc7544984deedacac903699cbb71dbba6be6408b3560d9ee7424045a681887b409fd6da6245e53bd80becd74ae23cdf21d544ff
7
+ data.tar.gz: bc0626751a50adb7470bcd992a27b09aceb594f72b28f43833a06370e15b00b7811a5c000245b518fd11fe361e37c5527cd30f804cf276f360475089d64ad2c8
data/.rubocop.yml CHANGED
@@ -1,5 +1,5 @@
1
1
  AllCops:
2
- TargetRubyVersion: 2.3
2
+ TargetRubyVersion: 2.2
3
3
  Metrics/AbcSize:
4
4
  Enabled: false
5
5
  Metrics/BlockLength:
data/.travis.yml CHANGED
@@ -1,4 +1,5 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 2.4.0
3
+ - 2.4.1
4
4
  - 2.3.3
5
+ - 2.2.6
data/CHANGELOG.md CHANGED
@@ -1,3 +1,11 @@
1
+ # [0.5.0][] (2017-03-26)
2
+
3
+ ## Added
4
+ - Support for Ruby 2.2
5
+ - :version option to limit the max acceptable version for changes
6
+ - v2.3 String.new
7
+ - v2.3 Enumerable#chunk_while
8
+
1
9
  # [0.4.0][] (2017-03-24)
2
10
 
3
11
  ## Added
data/README.md CHANGED
@@ -17,6 +17,8 @@ Right now the only update is from 2.3 to 2.4 however the goal is to go all the w
17
17
  - [Goals](#goals)
18
18
  - [Usage](#usage)
19
19
  - [Implementation Table](#implementation-table)
20
+ - [2.3 to 2.4](#23-to-24)
21
+ - [2.2 to 2.3](#22-to-23)
20
22
 
21
23
  ## Caveat Emptor
22
24
 
@@ -32,7 +34,7 @@ See the [implementation table](#implementation-table) for specifics about what h
32
34
  Add it to your Gemfile:
33
35
 
34
36
  ```ruby
35
- gem 'polyfill', '0.4.0'
37
+ gem 'polyfill', '0.5.0'
36
38
  ```
37
39
 
38
40
  Or install it manually:
@@ -75,6 +77,13 @@ If you want all of the methods for a particular class you can use `:all`.
75
77
  using Polyfill(Numeric: :all)
76
78
  ```
77
79
 
80
+ Updates can be stopped at a specific version by pass it via `:version`. The
81
+ version selected must be formatted as "MAJOR.MINOR".
82
+
83
+ ```ruby
84
+ using Polyfill(version: '2.3', Numeric: :all)
85
+ ```
86
+
78
87
  Methods can be included in the same way. Prior to Ruby 2.4, refinements did
79
88
  not work on modules. In order to get methods you'll need to include them after
80
89
  the module. Calling `using` on a module will add it to all core Ruby classes
@@ -195,6 +204,41 @@ end
195
204
  | TracePoint | #callee_id | No |
196
205
  | Warning | #warn | No |
197
206
 
207
+ ### 2.2 to 2.3
208
+
209
+ | Object | Method | Implemented | Notes |
210
+ | ---------------- | ------------------------ | ----------- | ----- |
211
+ | ARGF | #read_nonblock | No |
212
+ | Array | #bsearch_index | No |
213
+ | | #dig | No |
214
+ | Comparable | #== | No |
215
+ | Enumerable | #chunk | No |
216
+ | | #chunk_while | Yes |
217
+ | | #grep_v | No |
218
+ | | #slice_before | No |
219
+ | Enumerator::Lazy | #grep_v | No |
220
+ | File | .mkfifo | No |
221
+ | Hash | #< | No |
222
+ | | #<= | No |
223
+ | | #> | No |
224
+ | | #>= | No |
225
+ | | #dig | No |
226
+ | | #fetch_values | No |
227
+ | | #to_proc | No |
228
+ | IO | #advise | No |
229
+ | Kernel | #loop | No |
230
+ | Module | #deprecate_constant | No |
231
+ | NameError | #receiver | No |
232
+ | Numeric | #negative? | No |
233
+ | | #positive? | No |
234
+ | Queue | #close | No |
235
+ | String | #+@ | No |
236
+ | | #-@ | No |
237
+ | | .new | Yes |
238
+ | Struct | #dig | No |
239
+ | Thread | #name | No |
240
+ | | #name= | No |
241
+
198
242
  ## Contributing
199
243
 
200
244
  Bug reports and pull requests are welcome on GitHub at https://github.com/AaronLasseigne/polyfill.
data/lib/polyfill.rb CHANGED
@@ -1,38 +1,64 @@
1
1
  require 'polyfill/version'
2
+ require 'polyfill/utils'
3
+ require 'polyfill/v2_3'
2
4
  require 'polyfill/v2_4'
3
5
 
4
6
  module Polyfill
7
+ include V2_3
5
8
  include V2_4
6
9
  end
7
10
 
8
11
  def Polyfill(options) # rubocop:disable Style/MethodName
9
12
  mod = Module.new
10
13
 
11
- klasses, others = options.partition { |key,| key[/\A[A-Z]/] }
14
+ objects, others = options.partition { |key,| key[/\A[A-Z]/] }
15
+ others = others.to_h
16
+
17
+ versions = {
18
+ '2.3' => Polyfill::V2_3,
19
+ '2.4' => Polyfill::V2_4
20
+ }
21
+ desired_version = others.delete(:version) || versions.keys.max
22
+ unless versions.keys.include?(desired_version)
23
+ raise ArgumentError, "invalid value for keyword version: #{desired_version}"
24
+ end
25
+ versions.reject! do |version_number, _|
26
+ version_number > desired_version
27
+ end
12
28
 
13
29
  unless others.empty?
14
30
  raise ArgumentError, "unknown keyword: #{others.first[0]}"
15
31
  end
16
32
 
17
- needs_update = RUBY_VERSION[/\A(\d+\.\d+)/, 1] < '2.4'
33
+ current_ruby_version = RUBY_VERSION[/\A(\d+\.\d+)/, 1]
18
34
 
19
- klasses.each do |names, methods|
20
- class_or_module_mod = names
21
- .to_s
22
- .split('::')
23
- .reduce(Polyfill::V2_4) do |current_mod, name|
35
+ objects.each do |full_name, methods|
36
+ object_module_names = full_name.to_s.split('::')
37
+
38
+ object_modules = versions
39
+ .map do |version_number, version_module|
24
40
  begin
25
- current_mod.const_get(name, false)
41
+ final_module = object_module_names
42
+ .reduce(version_module) do |current_mod, name|
43
+ current_mod.const_get(name, false)
44
+ end
45
+
46
+ [version_number, final_module]
26
47
  rescue NameError
27
- raise ArgumentError, %Q("#{names}" is not a valid class or has no updates)
48
+ nil
28
49
  end
29
50
  end
51
+ .compact
30
52
 
31
- if methods == :all
32
- next unless needs_update
53
+ if object_modules.empty?
54
+ raise ArgumentError, %Q("#{full_name}" is not a valid class or has no updates)
55
+ end
33
56
 
57
+ if methods == :all
34
58
  mod.module_eval do
35
- include class_or_module_mod
59
+ object_modules.each do |(version_number, object_module)|
60
+ include object_module if version_number > current_ruby_version
61
+ end
36
62
  end
37
63
  else
38
64
  methods.each do |method|
@@ -58,19 +84,24 @@ def Polyfill(options) # rubocop:disable Style/MethodName
58
84
  method_name.capitalize!
59
85
  method_name.gsub!(/_(.)/) { |match| match[1].capitalize }
60
86
 
61
- method_mod =
62
- begin
63
- class_or_module_mod
64
- .const_get(type, false)
65
- .const_get(method_name, false)
66
- rescue NameError
67
- raise ArgumentError, %Q("#{method}" is not a valid method on #{names} or has no updates)
87
+ method_modules = object_modules
88
+ .map do |(version_number, object_module)|
89
+ begin
90
+ [version_number, object_module.const_get(type, false).const_get(method_name, false)]
91
+ rescue NameError
92
+ nil
93
+ end
68
94
  end
95
+ .compact
69
96
 
70
- next unless needs_update
97
+ if method_modules.empty?
98
+ raise ArgumentError, %Q("#{method}" is not a valid method on #{full_name} or has no updates)
99
+ end
71
100
 
72
101
  mod.module_eval do
73
- include method_mod
102
+ method_modules.each do |(version_number, method_module)|
103
+ include method_module if version_number > current_ruby_version
104
+ end
74
105
  end
75
106
  end
76
107
  end
@@ -0,0 +1,8 @@
1
+ module Polyfill
2
+ module Utils
3
+ def when_ruby_below(version)
4
+ yield if RUBY_VERSION[/\A(\d+\.\d+)/, 1] < version
5
+ end
6
+ module_function :when_ruby_below
7
+ end
8
+ end
@@ -0,0 +1,9 @@
1
+ require_relative 'v2_3/enumerable'
2
+ require_relative 'v2_3/string'
3
+
4
+ module Polyfill
5
+ module V2_3
6
+ include Enumerable
7
+ include String
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ require_relative 'enumerable/instance'
2
+
3
+ module Polyfill
4
+ module V2_3
5
+ module Enumerable
6
+ include Instance
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,11 @@
1
+ require_relative 'instance/chunk_while'
2
+
3
+ module Polyfill
4
+ module V2_3
5
+ module Enumerable
6
+ module Instance
7
+ include ChunkWhile
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,70 @@
1
+ require 'stringio'
2
+
3
+ module Polyfill
4
+ module V2_3
5
+ module Enumerable
6
+ module Instance
7
+ module ChunkWhile
8
+ module Method
9
+ def chunk_while
10
+ block = Proc.new
11
+
12
+ return [self] if size == 1
13
+
14
+ Enumerator.new do |yielder|
15
+ output = []
16
+ each_cons(2).with_index(1) do |(a, b), run|
17
+ if run == size - 1
18
+ if block.call(a, b)
19
+ output.push(a, b)
20
+ yielder << output
21
+ else
22
+ output.push(a)
23
+ yielder << output
24
+ yielder << [b]
25
+ end
26
+ else
27
+ output.push(a)
28
+ unless block.call(a, b)
29
+ yielder << output
30
+ output = []
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
37
+
38
+ refine ::Array do
39
+ include Method
40
+ end
41
+ refine ::Dir do
42
+ include Method
43
+ end
44
+ refine ::Enumerator do
45
+ include Method
46
+ end
47
+ refine ::Hash do
48
+ include Method
49
+ end
50
+ refine ::IO do
51
+ include Method
52
+ end
53
+ refine ::Range do
54
+ include Method
55
+ end
56
+ refine ::StringIO do
57
+ include Method
58
+ end
59
+ refine ::Struct do
60
+ include Method
61
+ end
62
+
63
+ def self.included(base)
64
+ base.include Method
65
+ end
66
+ end
67
+ end
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,9 @@
1
+ require_relative 'string/class'
2
+
3
+ module Polyfill
4
+ module V2_3
5
+ module String
6
+ include Class
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,11 @@
1
+ require_relative 'class/new'
2
+
3
+ module Polyfill
4
+ module V2_3
5
+ module String
6
+ module Class
7
+ include New
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,33 @@
1
+ module Polyfill
2
+ module V2_3
3
+ module String
4
+ module Class
5
+ module New
6
+ module Method
7
+ def new(*args)
8
+ hash, others = args.partition { |arg| arg.is_a?(::Hash) }
9
+ hash = hash.first
10
+ encoding = hash && hash.delete(:encoding)
11
+
12
+ if hash && !hash.keys.empty?
13
+ raise ArgumentError, "unknown keyword: #{hash.keys.first}"
14
+ end
15
+
16
+ str = super(*others)
17
+ str.encode!(encoding) if encoding
18
+ str
19
+ end
20
+ end
21
+
22
+ refine ::String.singleton_class do
23
+ include Method
24
+ end
25
+
26
+ def self.included(base)
27
+ base.include Method
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -1,3 +1,5 @@
1
+ require 'stringio'
2
+
1
3
  module Polyfill
2
4
  module V2_4
3
5
  module Enumerable
@@ -5,6 +5,10 @@ module Polyfill
5
5
  module Instance
6
6
  module ChunkWhile
7
7
  module Method
8
+ Utils.when_ruby_below('2.3') do
9
+ using V2_3::Enumerable::Instance::ChunkWhile
10
+ end
11
+
8
12
  def chunk_while
9
13
  super.lazy
10
14
  end
@@ -1,3 +1,3 @@
1
1
  module Polyfill
2
- VERSION = '0.4.0'.freeze
2
+ VERSION = '0.5.0'.freeze
3
3
  end
data/polyfill.gemspec CHANGED
@@ -13,7 +13,7 @@ Gem::Specification.new do |spec|
13
13
  spec.homepage = ''
14
14
  spec.license = 'MIT'
15
15
 
16
- spec.required_ruby_version = '>= 2.3'
16
+ spec.required_ruby_version = '>= 2.2'
17
17
 
18
18
  spec.files = `git ls-files -z`.split("\x0").reject do |f|
19
19
  f.match(%r{^(test|spec|features)/})
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: polyfill
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aaron Lasseigne
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-03-24 00:00:00.000000000 Z
11
+ date: 2017-03-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -86,6 +86,14 @@ files:
86
86
  - bin/console
87
87
  - bin/setup
88
88
  - lib/polyfill.rb
89
+ - lib/polyfill/utils.rb
90
+ - lib/polyfill/v2_3.rb
91
+ - lib/polyfill/v2_3/enumerable.rb
92
+ - lib/polyfill/v2_3/enumerable/instance.rb
93
+ - lib/polyfill/v2_3/enumerable/instance/chunk_while.rb
94
+ - lib/polyfill/v2_3/string.rb
95
+ - lib/polyfill/v2_3/string/class.rb
96
+ - lib/polyfill/v2_3/string/class/new.rb
89
97
  - lib/polyfill/v2_4.rb
90
98
  - lib/polyfill/v2_4/array.rb
91
99
  - lib/polyfill/v2_4/array/instance.rb
@@ -196,7 +204,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
196
204
  requirements:
197
205
  - - ">="
198
206
  - !ruby/object:Gem::Version
199
- version: '2.3'
207
+ version: '2.2'
200
208
  required_rubygems_version: !ruby/object:Gem::Requirement
201
209
  requirements:
202
210
  - - ">="