polyfill 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +23 -1
- data/README.md +30 -44
- data/lib/polyfill.rb +63 -0
- data/lib/polyfill/v2_4.rb +8 -0
- data/lib/polyfill/v2_4/array.rb +2 -2
- data/lib/polyfill/v2_4/array/instance.rb +11 -0
- data/lib/polyfill/v2_4/array/instance/concat.rb +32 -0
- data/lib/polyfill/v2_4/comparable.rb +2 -2
- data/lib/polyfill/v2_4/comparable/instance.rb +11 -0
- data/lib/polyfill/v2_4/comparable/instance/clamp.rb +37 -0
- data/lib/polyfill/v2_4/dir.rb +9 -0
- data/lib/polyfill/v2_4/dir/class.rb +11 -0
- data/lib/polyfill/v2_4/dir/class/empty_q.rb +25 -0
- data/lib/polyfill/v2_4/file.rb +9 -0
- data/lib/polyfill/v2_4/file/class.rb +11 -0
- data/lib/polyfill/v2_4/file/class/empty_q.rb +25 -0
- data/lib/polyfill/v2_4/float.rb +2 -6
- data/lib/polyfill/v2_4/float/instance.rb +15 -0
- data/lib/polyfill/v2_4/float/instance/ceil.rb +34 -0
- data/lib/polyfill/v2_4/float/instance/floor.rb +34 -0
- data/lib/polyfill/v2_4/float/instance/truncate.rb +34 -0
- data/lib/polyfill/v2_4/hash.rb +2 -8
- data/lib/polyfill/v2_4/hash/instance.rb +17 -0
- data/lib/polyfill/v2_4/hash/instance/compact.rb +25 -0
- data/lib/polyfill/v2_4/hash/instance/compact_e.rb +25 -0
- data/lib/polyfill/v2_4/hash/instance/transform_values.rb +35 -0
- data/lib/polyfill/v2_4/hash/instance/transform_values_e.rb +35 -0
- data/lib/polyfill/v2_4/integer.rb +2 -10
- data/lib/polyfill/v2_4/integer/instance.rb +19 -0
- data/lib/polyfill/v2_4/integer/instance/ceil.rb +30 -0
- data/lib/polyfill/v2_4/integer/instance/digits.rb +36 -0
- data/lib/polyfill/v2_4/integer/instance/floor.rb +30 -0
- data/lib/polyfill/v2_4/integer/instance/round.rb +33 -0
- data/lib/polyfill/v2_4/integer/instance/truncate.rb +30 -0
- data/lib/polyfill/v2_4/io.rb +11 -0
- data/lib/polyfill/v2_4/io/class.rb +13 -0
- data/lib/polyfill/v2_4/io/class/foreach.rb +67 -0
- data/lib/polyfill/v2_4/io/class/readlines.rb +38 -0
- data/lib/polyfill/v2_4/io/instance.rb +17 -0
- data/lib/polyfill/v2_4/io/instance/each_line.rb +67 -0
- data/lib/polyfill/v2_4/io/instance/gets.rb +38 -0
- data/lib/polyfill/v2_4/io/instance/readline.rb +38 -0
- data/lib/polyfill/v2_4/io/instance/readlines.rb +38 -0
- data/lib/polyfill/v2_4/match_data.rb +2 -4
- data/lib/polyfill/v2_4/match_data/instance.rb +13 -0
- data/lib/polyfill/v2_4/match_data/instance/named_captures.rb +27 -0
- data/lib/polyfill/v2_4/match_data/instance/values_at.rb +27 -0
- data/lib/polyfill/v2_4/numeric.rb +2 -4
- data/lib/polyfill/v2_4/numeric/instance.rb +13 -0
- data/lib/polyfill/v2_4/numeric/instance/finite_q.rb +25 -0
- data/lib/polyfill/v2_4/numeric/instance/infinite_q.rb +25 -0
- data/lib/polyfill/v2_4/string.rb +2 -4
- data/lib/polyfill/v2_4/string/instance.rb +13 -0
- data/lib/polyfill/v2_4/string/instance/concat.rb +32 -0
- data/lib/polyfill/v2_4/string/instance/prepend.rb +32 -0
- data/lib/polyfill/v2_4/string_io.rb +9 -0
- data/lib/polyfill/v2_4/string_io/instance.rb +17 -0
- data/lib/polyfill/v2_4/string_io/instance/each_line.rb +19 -0
- data/lib/polyfill/v2_4/string_io/instance/gets.rb +19 -0
- data/lib/polyfill/v2_4/string_io/instance/readline.rb +19 -0
- data/lib/polyfill/v2_4/string_io/instance/readlines.rb +19 -0
- data/lib/polyfill/version.rb +1 -1
- metadata +51 -22
- data/lib/polyfill/v2_4/array/concat.rb +0 -26
- data/lib/polyfill/v2_4/comparable/clamp.rb +0 -31
- data/lib/polyfill/v2_4/float/ceil.rb +0 -28
- data/lib/polyfill/v2_4/float/floor.rb +0 -28
- data/lib/polyfill/v2_4/float/truncate.rb +0 -28
- data/lib/polyfill/v2_4/hash/compact.rb +0 -19
- data/lib/polyfill/v2_4/hash/compact_e.rb +0 -19
- data/lib/polyfill/v2_4/hash/transform_values.rb +0 -29
- data/lib/polyfill/v2_4/hash/transform_values_e.rb +0 -29
- data/lib/polyfill/v2_4/integer/ceil.rb +0 -24
- data/lib/polyfill/v2_4/integer/digits.rb +0 -30
- data/lib/polyfill/v2_4/integer/floor.rb +0 -24
- data/lib/polyfill/v2_4/integer/round.rb +0 -27
- data/lib/polyfill/v2_4/integer/truncate.rb +0 -24
- data/lib/polyfill/v2_4/match_data/named_captures.rb +0 -21
- data/lib/polyfill/v2_4/match_data/values_at.rb +0 -21
- data/lib/polyfill/v2_4/numeric/finite_q.rb +0 -19
- data/lib/polyfill/v2_4/numeric/infinite_q.rb +0 -19
- data/lib/polyfill/v2_4/string/concat.rb +0 -26
- data/lib/polyfill/v2_4/string/prepend.rb +0 -26
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c8ffdd9cc5cb6a688ba0bcedc79a627c63f1916f
|
4
|
+
data.tar.gz: 3028c8eed9c64fd43d65d049c4aadeb7843aac48
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8fc2cf9a4bc8f49268c396d7a7caf911941f8a4c9b05ecf4c84f93bb60e99c2fb6e1becf7d824fa34a89534f653a66586f6bfd826b788bd0cf387c5f24711bec
|
7
|
+
data.tar.gz: '082b8f86fe28e66a3aedd6bcd809c09720ed37bf5745fbc7d1b9f1a23de7ac9fd5cb6808ff5f6da4dcea57b909866da0dbefc4c06c2ecf2376fa4d090187280d'
|
data/CHANGELOG.md
CHANGED
@@ -1,4 +1,25 @@
|
|
1
|
-
# [0.
|
1
|
+
# [0.3.0][]
|
2
|
+
|
3
|
+
## Added
|
4
|
+
|
5
|
+
- v2.4 Dir.empty?
|
6
|
+
- v2.4 File.empty?
|
7
|
+
- v2.4 IO#each_line
|
8
|
+
- v2.4 IO.foreach
|
9
|
+
- v2.4 IO#gets
|
10
|
+
- v2.4 IO#readline
|
11
|
+
- v2.4 IO#readlines
|
12
|
+
- v2.4 IO.readlines
|
13
|
+
- v2.4 StringIO#each_line
|
14
|
+
- v2.4 StringIO#gets
|
15
|
+
- v2.4 StringIO#readline
|
16
|
+
- v2.4 StringIO#readlines
|
17
|
+
|
18
|
+
## Changes
|
19
|
+
|
20
|
+
- New way to select methods that doesn't rely on knowing the module structure
|
21
|
+
|
22
|
+
# [0.2.0][] (2017-03-16)
|
2
23
|
|
3
24
|
## Added
|
4
25
|
|
@@ -33,5 +54,6 @@
|
|
33
54
|
- v2.4 String#concat?
|
34
55
|
- v2.4 String#prepend?
|
35
56
|
|
57
|
+
[0.3.0]: https://github.com/AaronLasseigne/polyfill/compare/v0.2.0...v0.3.0
|
36
58
|
[0.2.0]: https://github.com/AaronLasseigne/polyfill/compare/v0.1.0...v0.2.0
|
37
59
|
[0.1.0]: https://github.com/AaronLasseigne/polyfill/compare/v0.0.0...v0.1.0
|
data/README.md
CHANGED
@@ -32,7 +32,7 @@ See the [implementation table](#implementation-table) for specifics about what h
|
|
32
32
|
Add it to your Gemfile:
|
33
33
|
|
34
34
|
```ruby
|
35
|
-
gem 'polyfill', '0.
|
35
|
+
gem 'polyfill', '0.3.0'
|
36
36
|
```
|
37
37
|
|
38
38
|
Or install it manually:
|
@@ -57,46 +57,31 @@ To use all updates:
|
|
57
57
|
using Polyfill
|
58
58
|
```
|
59
59
|
|
60
|
-
To use
|
60
|
+
To specify methods from a particular object use it's class name and pass an
|
61
|
+
array of strings containing the methods you'd like to use. Instance methods
|
62
|
+
need to start with "#" and class methods need to start with ".".
|
61
63
|
|
62
64
|
```ruby
|
63
|
-
using Polyfill
|
65
|
+
using Polyfill(
|
66
|
+
Array: %w[#concat],
|
67
|
+
Dir: %w[.empty?],
|
68
|
+
Hash: %w[#compact! #transform_values],
|
69
|
+
)
|
64
70
|
```
|
65
71
|
|
66
|
-
|
72
|
+
Methods can be included in the same way. Prior to Ruby 2.4, refinements did
|
73
|
+
not work on modules. In order to get methods you'll need to include them after
|
74
|
+
the module. Calling `using` on a module will add it to all core Ruby classes
|
75
|
+
that include it. The methods will only be included if they are needed by the
|
76
|
+
Ruby version running the code.
|
67
77
|
|
68
78
|
```ruby
|
69
|
-
|
70
|
-
|
79
|
+
class Foo
|
80
|
+
include Comparable
|
81
|
+
include Polyfill(Comparable: %w[#clamp])
|
82
|
+
end
|
71
83
|
```
|
72
84
|
|
73
|
-
To use a particular method, we can add it after the object. The method is
|
74
|
-
converted to camel case. Predicate methods (ending with a question mark)
|
75
|
-
have their question converted to a `Q`. Dangerous methods (ending with an
|
76
|
-
exclamation mark) have their exclamation replaced with `E`.
|
77
|
-
|
78
|
-
```ruby
|
79
|
-
using Polyfill::V2_4::Array::Concat
|
80
|
-
using Polyfill::V2_4::Dir::EmptyQ # :empty?
|
81
|
-
using Polyfill::V2_4::Hash::CompactE # :compact!
|
82
|
-
using Polyfill::V2_4::Hash::TransformValues # :transform_values!
|
83
|
-
```
|
84
|
-
|
85
|
-
Any method can be accessed as a stand-alone module by adding `Method` to
|
86
|
-
the end:
|
87
|
-
|
88
|
-
```ruby
|
89
|
-
include Polyfill::V2_4::Comparable::Clamp::Method
|
90
|
-
```
|
91
|
-
|
92
|
-
**A note about modules:** Prior to 2.4, refinements do not work on modules.
|
93
|
-
This means modules like `Comparable` will apply the refinement to all child
|
94
|
-
classes. Anything custom classes that inherit from `Comparable` will be
|
95
|
-
unaffected by the refinement. In cases like this you can use `include` as
|
96
|
-
demonstrated above to pull in the needed method. Just like always, the
|
97
|
-
method is only defined if the Ruby version requires it.
|
98
|
-
|
99
|
-
|
100
85
|
## Implementation Table
|
101
86
|
|
102
87
|
### 2.3 to 2.4
|
@@ -109,13 +94,13 @@ method is only defined if the Ruby version requires it.
|
|
109
94
|
| | #pack | No |
|
110
95
|
| | #sum | No |
|
111
96
|
| Comparable | #clamp | Yes |
|
112
|
-
| Dir |
|
97
|
+
| Dir | .empty? | Yes |
|
113
98
|
| Enumerable | #chunk | No |
|
114
99
|
| | #sum | No |
|
115
100
|
| | #uniq | No |
|
116
101
|
| Enumerator::Lazy | #chunk_while | No |
|
117
102
|
| | #uniq | No |
|
118
|
-
| File |
|
103
|
+
| File | .empty? | Yes |
|
119
104
|
| Float | #ceil | Yes |
|
120
105
|
| | #floor | Yes |
|
121
106
|
| | #round | No |
|
@@ -129,11 +114,12 @@ method is only defined if the Ruby version requires it.
|
|
129
114
|
| | #floor | Yes |
|
130
115
|
| | #round | Yes |
|
131
116
|
| | #truncate | Yes |
|
132
|
-
| IO | #each_line |
|
133
|
-
| | .foreach |
|
134
|
-
| | #gets |
|
135
|
-
| | #readline |
|
136
|
-
| | #readlines |
|
117
|
+
| IO | #each_line | Yes |
|
118
|
+
| | .foreach | Yes |
|
119
|
+
| | #gets | Yes |
|
120
|
+
| | #readline | Yes |
|
121
|
+
| | #readlines | Yes |
|
122
|
+
| | .readlines | Yes |
|
137
123
|
| Kernel | #clone | No |
|
138
124
|
| MatchData | #named_captures | Yes |
|
139
125
|
| | #values_at | Yes |
|
@@ -159,10 +145,10 @@ method is only defined if the Ruby version requires it.
|
|
159
145
|
| | #unpack1 | No |
|
160
146
|
| | #upcase | No |
|
161
147
|
| | #upcase! | No |
|
162
|
-
| StringIO | #each_line |
|
163
|
-
| | #gets |
|
164
|
-
| | #readline |
|
165
|
-
| | #readlines |
|
148
|
+
| StringIO | #each_line | Yes |
|
149
|
+
| | #gets | Yes |
|
150
|
+
| | #readline | Yes |
|
151
|
+
| | #readlines | Yes |
|
166
152
|
| Symbol | #capitalize | No |
|
167
153
|
| | #capitalize! | No |
|
168
154
|
| | #casecmp? | No |
|
data/lib/polyfill.rb
CHANGED
@@ -4,3 +4,66 @@ require 'polyfill/v2_4'
|
|
4
4
|
module Polyfill
|
5
5
|
include V2_4
|
6
6
|
end
|
7
|
+
|
8
|
+
def Polyfill(options)
|
9
|
+
mod = Module.new
|
10
|
+
|
11
|
+
klasses, others = options.partition { |key,| key[/\A[A-Z]/] }
|
12
|
+
|
13
|
+
unless others.empty?
|
14
|
+
raise ArgumentError, "unknown keyword: #{others.first[0]}"
|
15
|
+
end
|
16
|
+
|
17
|
+
klasses.each do |name, methods|
|
18
|
+
class_or_module_mod =
|
19
|
+
begin
|
20
|
+
Polyfill::V2_4.const_get(name, false)
|
21
|
+
rescue NameError
|
22
|
+
raise ArgumentError, %Q("#{name}" is not a valid class or has no updates)
|
23
|
+
end
|
24
|
+
|
25
|
+
if methods == :all
|
26
|
+
mod.module_eval do
|
27
|
+
include class_or_module_mod
|
28
|
+
end
|
29
|
+
else
|
30
|
+
methods.each do |method|
|
31
|
+
type =
|
32
|
+
case method[0]
|
33
|
+
when '.'
|
34
|
+
:Class
|
35
|
+
when '#'
|
36
|
+
:Instance
|
37
|
+
else
|
38
|
+
raise ArgumentError, %Q("#{method}" must start with a "." if it's a class method or "#" if it's an instance method)
|
39
|
+
end
|
40
|
+
method_name =
|
41
|
+
case method[-1]
|
42
|
+
when '?'
|
43
|
+
"#{method[1..-2]}_q"
|
44
|
+
when '!'
|
45
|
+
"#{method[1..-2]}_e"
|
46
|
+
else
|
47
|
+
method[1..-1]
|
48
|
+
end
|
49
|
+
method_name.capitalize!
|
50
|
+
method_name.gsub!(/_(.)/) { $1.capitalize }
|
51
|
+
|
52
|
+
method_mod =
|
53
|
+
begin
|
54
|
+
class_or_module_mod
|
55
|
+
.const_get(type, false)
|
56
|
+
.const_get(method_name, false)
|
57
|
+
rescue NameError
|
58
|
+
raise ArgumentError, %Q("#{method}" is not a valid method on #{name} or has no updates)
|
59
|
+
end
|
60
|
+
|
61
|
+
mod.module_eval do
|
62
|
+
include class_or_module_mod.const_get(type, false).const_get(method_name, false)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
mod
|
69
|
+
end
|
data/lib/polyfill/v2_4.rb
CHANGED
@@ -1,21 +1,29 @@
|
|
1
1
|
require 'polyfill/v2_4/array'
|
2
2
|
require 'polyfill/v2_4/comparable'
|
3
|
+
require 'polyfill/v2_4/dir'
|
4
|
+
require 'polyfill/v2_4/file'
|
3
5
|
require 'polyfill/v2_4/float'
|
4
6
|
require 'polyfill/v2_4/hash'
|
5
7
|
require 'polyfill/v2_4/integer'
|
8
|
+
require 'polyfill/v2_4/io'
|
6
9
|
require 'polyfill/v2_4/match_data'
|
7
10
|
require 'polyfill/v2_4/numeric'
|
8
11
|
require 'polyfill/v2_4/string'
|
12
|
+
require 'polyfill/v2_4/string_io'
|
9
13
|
|
10
14
|
module Polyfill
|
11
15
|
module V2_4
|
12
16
|
include Array
|
13
17
|
include Comparable
|
18
|
+
include Dir
|
19
|
+
include File
|
14
20
|
include Float
|
15
21
|
include Hash
|
16
22
|
include Integer
|
23
|
+
include IO
|
17
24
|
include MatchData
|
18
25
|
include Numeric
|
19
26
|
include String
|
27
|
+
include StringIO
|
20
28
|
end
|
21
29
|
end
|
data/lib/polyfill/v2_4/array.rb
CHANGED
@@ -0,0 +1,32 @@
|
|
1
|
+
module Polyfill
|
2
|
+
module V2_4
|
3
|
+
module Array
|
4
|
+
module Instance
|
5
|
+
module Concat
|
6
|
+
module Method
|
7
|
+
def concat(*others)
|
8
|
+
return super if others.length == 1
|
9
|
+
|
10
|
+
acc = [].concat(self)
|
11
|
+
others.each do |other|
|
12
|
+
acc.concat(other)
|
13
|
+
end
|
14
|
+
|
15
|
+
replace(acc)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
if RUBY_VERSION < '2.4.0'
|
20
|
+
refine ::Array do
|
21
|
+
include Method
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.included(base)
|
25
|
+
base.include Method
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module Polyfill
|
2
|
+
module V2_4
|
3
|
+
module Comparable
|
4
|
+
module Instance
|
5
|
+
module Clamp
|
6
|
+
module Method
|
7
|
+
def clamp(min, max)
|
8
|
+
if min > max
|
9
|
+
raise ArgumentError, 'min argument must be smaller than max argument'
|
10
|
+
end
|
11
|
+
|
12
|
+
return min if min > self
|
13
|
+
return max if max < self
|
14
|
+
self
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
if RUBY_VERSION < '2.4.0'
|
19
|
+
refine ::Numeric do
|
20
|
+
include Method
|
21
|
+
end
|
22
|
+
refine ::String do
|
23
|
+
include Method
|
24
|
+
end
|
25
|
+
refine ::Time do
|
26
|
+
include Method
|
27
|
+
end
|
28
|
+
|
29
|
+
def self.included(base)
|
30
|
+
base.include Method
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module Polyfill
|
2
|
+
module V2_4
|
3
|
+
module Dir
|
4
|
+
module Class
|
5
|
+
module EmptyQ
|
6
|
+
module Method
|
7
|
+
def empty?(path_name)
|
8
|
+
exist?(path_name) && (entries(path_name) - ['.', '..']).empty?
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
if RUBY_VERSION < '2.4.0'
|
13
|
+
refine ::Dir.singleton_class do
|
14
|
+
include Method
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.included(base)
|
18
|
+
base.include Method
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|