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.
Files changed (84) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +23 -1
  3. data/README.md +30 -44
  4. data/lib/polyfill.rb +63 -0
  5. data/lib/polyfill/v2_4.rb +8 -0
  6. data/lib/polyfill/v2_4/array.rb +2 -2
  7. data/lib/polyfill/v2_4/array/instance.rb +11 -0
  8. data/lib/polyfill/v2_4/array/instance/concat.rb +32 -0
  9. data/lib/polyfill/v2_4/comparable.rb +2 -2
  10. data/lib/polyfill/v2_4/comparable/instance.rb +11 -0
  11. data/lib/polyfill/v2_4/comparable/instance/clamp.rb +37 -0
  12. data/lib/polyfill/v2_4/dir.rb +9 -0
  13. data/lib/polyfill/v2_4/dir/class.rb +11 -0
  14. data/lib/polyfill/v2_4/dir/class/empty_q.rb +25 -0
  15. data/lib/polyfill/v2_4/file.rb +9 -0
  16. data/lib/polyfill/v2_4/file/class.rb +11 -0
  17. data/lib/polyfill/v2_4/file/class/empty_q.rb +25 -0
  18. data/lib/polyfill/v2_4/float.rb +2 -6
  19. data/lib/polyfill/v2_4/float/instance.rb +15 -0
  20. data/lib/polyfill/v2_4/float/instance/ceil.rb +34 -0
  21. data/lib/polyfill/v2_4/float/instance/floor.rb +34 -0
  22. data/lib/polyfill/v2_4/float/instance/truncate.rb +34 -0
  23. data/lib/polyfill/v2_4/hash.rb +2 -8
  24. data/lib/polyfill/v2_4/hash/instance.rb +17 -0
  25. data/lib/polyfill/v2_4/hash/instance/compact.rb +25 -0
  26. data/lib/polyfill/v2_4/hash/instance/compact_e.rb +25 -0
  27. data/lib/polyfill/v2_4/hash/instance/transform_values.rb +35 -0
  28. data/lib/polyfill/v2_4/hash/instance/transform_values_e.rb +35 -0
  29. data/lib/polyfill/v2_4/integer.rb +2 -10
  30. data/lib/polyfill/v2_4/integer/instance.rb +19 -0
  31. data/lib/polyfill/v2_4/integer/instance/ceil.rb +30 -0
  32. data/lib/polyfill/v2_4/integer/instance/digits.rb +36 -0
  33. data/lib/polyfill/v2_4/integer/instance/floor.rb +30 -0
  34. data/lib/polyfill/v2_4/integer/instance/round.rb +33 -0
  35. data/lib/polyfill/v2_4/integer/instance/truncate.rb +30 -0
  36. data/lib/polyfill/v2_4/io.rb +11 -0
  37. data/lib/polyfill/v2_4/io/class.rb +13 -0
  38. data/lib/polyfill/v2_4/io/class/foreach.rb +67 -0
  39. data/lib/polyfill/v2_4/io/class/readlines.rb +38 -0
  40. data/lib/polyfill/v2_4/io/instance.rb +17 -0
  41. data/lib/polyfill/v2_4/io/instance/each_line.rb +67 -0
  42. data/lib/polyfill/v2_4/io/instance/gets.rb +38 -0
  43. data/lib/polyfill/v2_4/io/instance/readline.rb +38 -0
  44. data/lib/polyfill/v2_4/io/instance/readlines.rb +38 -0
  45. data/lib/polyfill/v2_4/match_data.rb +2 -4
  46. data/lib/polyfill/v2_4/match_data/instance.rb +13 -0
  47. data/lib/polyfill/v2_4/match_data/instance/named_captures.rb +27 -0
  48. data/lib/polyfill/v2_4/match_data/instance/values_at.rb +27 -0
  49. data/lib/polyfill/v2_4/numeric.rb +2 -4
  50. data/lib/polyfill/v2_4/numeric/instance.rb +13 -0
  51. data/lib/polyfill/v2_4/numeric/instance/finite_q.rb +25 -0
  52. data/lib/polyfill/v2_4/numeric/instance/infinite_q.rb +25 -0
  53. data/lib/polyfill/v2_4/string.rb +2 -4
  54. data/lib/polyfill/v2_4/string/instance.rb +13 -0
  55. data/lib/polyfill/v2_4/string/instance/concat.rb +32 -0
  56. data/lib/polyfill/v2_4/string/instance/prepend.rb +32 -0
  57. data/lib/polyfill/v2_4/string_io.rb +9 -0
  58. data/lib/polyfill/v2_4/string_io/instance.rb +17 -0
  59. data/lib/polyfill/v2_4/string_io/instance/each_line.rb +19 -0
  60. data/lib/polyfill/v2_4/string_io/instance/gets.rb +19 -0
  61. data/lib/polyfill/v2_4/string_io/instance/readline.rb +19 -0
  62. data/lib/polyfill/v2_4/string_io/instance/readlines.rb +19 -0
  63. data/lib/polyfill/version.rb +1 -1
  64. metadata +51 -22
  65. data/lib/polyfill/v2_4/array/concat.rb +0 -26
  66. data/lib/polyfill/v2_4/comparable/clamp.rb +0 -31
  67. data/lib/polyfill/v2_4/float/ceil.rb +0 -28
  68. data/lib/polyfill/v2_4/float/floor.rb +0 -28
  69. data/lib/polyfill/v2_4/float/truncate.rb +0 -28
  70. data/lib/polyfill/v2_4/hash/compact.rb +0 -19
  71. data/lib/polyfill/v2_4/hash/compact_e.rb +0 -19
  72. data/lib/polyfill/v2_4/hash/transform_values.rb +0 -29
  73. data/lib/polyfill/v2_4/hash/transform_values_e.rb +0 -29
  74. data/lib/polyfill/v2_4/integer/ceil.rb +0 -24
  75. data/lib/polyfill/v2_4/integer/digits.rb +0 -30
  76. data/lib/polyfill/v2_4/integer/floor.rb +0 -24
  77. data/lib/polyfill/v2_4/integer/round.rb +0 -27
  78. data/lib/polyfill/v2_4/integer/truncate.rb +0 -24
  79. data/lib/polyfill/v2_4/match_data/named_captures.rb +0 -21
  80. data/lib/polyfill/v2_4/match_data/values_at.rb +0 -21
  81. data/lib/polyfill/v2_4/numeric/finite_q.rb +0 -19
  82. data/lib/polyfill/v2_4/numeric/infinite_q.rb +0 -19
  83. data/lib/polyfill/v2_4/string/concat.rb +0 -26
  84. 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: 9d536c32a25db0ccb7bbbe70e7e921c35a97d95f
4
- data.tar.gz: e8b0d4bf4c7ece75a844065d2407ce761e093564
3
+ metadata.gz: c8ffdd9cc5cb6a688ba0bcedc79a627c63f1916f
4
+ data.tar.gz: 3028c8eed9c64fd43d65d049c4aadeb7843aac48
5
5
  SHA512:
6
- metadata.gz: 4eb22154fb993d183068e2f8a7345b05a298b040d6911c2c4dbf71fbe5c524b505a2074067012d8d87b8afb90e83c50673a59b094a4c437058509c49fc98dca6
7
- data.tar.gz: f2bacc5168680b5fa4a3b4a4c29d7e83cc8467d45fc4e78b9f551161ea0f898f72c8aa8a6e97f0e2951cf3516a6cf5dbafe5105bdea638c4fec2a71981171ad3
6
+ metadata.gz: 8fc2cf9a4bc8f49268c396d7a7caf911941f8a4c9b05ecf4c84f93bb60e99c2fb6e1becf7d824fa34a89534f653a66586f6bfd826b788bd0cf387c5f24711bec
7
+ data.tar.gz: '082b8f86fe28e66a3aedd6bcd809c09720ed37bf5745fbc7d1b9f1a23de7ac9fd5cb6808ff5f6da4dcea57b909866da0dbefc4c06c2ecf2376fa4d090187280d'
data/CHANGELOG.md CHANGED
@@ -1,4 +1,25 @@
1
- # [0.2.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.2.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 all updates up to V2_4:
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::V2_4
65
+ using Polyfill(
66
+ Array: %w[#concat],
67
+ Dir: %w[.empty?],
68
+ Hash: %w[#compact! #transform_values],
69
+ )
64
70
  ```
65
71
 
66
- To use all updates for a particular object, add it to the end:
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
- using Polyfill::V2_4::Array
70
- using Polyfill::V2_4::String
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 | #empty? | No |
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 | #empty? | No |
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 | No |
133
- | | .foreach | No |
134
- | | #gets | No |
135
- | | #readline | No |
136
- | | #readlines | No |
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 | No |
163
- | | #gets | No |
164
- | | #readline | No |
165
- | | #readlines | No |
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
@@ -1,9 +1,9 @@
1
- require 'polyfill/v2_4/array/concat'
1
+ require 'polyfill/v2_4/array/instance'
2
2
 
3
3
  module Polyfill
4
4
  module V2_4
5
5
  module Array
6
- include Concat
6
+ include Instance
7
7
  end
8
8
  end
9
9
  end
@@ -0,0 +1,11 @@
1
+ require 'polyfill/v2_4/array/instance/concat'
2
+
3
+ module Polyfill
4
+ module V2_4
5
+ module Array
6
+ module Instance
7
+ include Concat
8
+ end
9
+ end
10
+ end
11
+ end
@@ -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
@@ -1,9 +1,9 @@
1
- require 'polyfill/v2_4/comparable/clamp'
1
+ require 'polyfill/v2_4/comparable/instance'
2
2
 
3
3
  module Polyfill
4
4
  module V2_4
5
5
  module Comparable
6
- include Clamp
6
+ include Instance
7
7
  end
8
8
  end
9
9
  end
@@ -0,0 +1,11 @@
1
+ require 'polyfill/v2_4/comparable/instance/clamp'
2
+
3
+ module Polyfill
4
+ module V2_4
5
+ module Comparable
6
+ module Instance
7
+ include Clamp
8
+ end
9
+ end
10
+ end
11
+ 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,9 @@
1
+ require 'polyfill/v2_4/dir/class'
2
+
3
+ module Polyfill
4
+ module V2_4
5
+ module Dir
6
+ include Class
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,11 @@
1
+ require 'polyfill/v2_4/dir/class/empty_q'
2
+
3
+ module Polyfill
4
+ module V2_4
5
+ module Dir
6
+ module Class
7
+ include EmptyQ
8
+ end
9
+ end
10
+ end
11
+ 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
@@ -0,0 +1,9 @@
1
+ require 'polyfill/v2_4/file/class'
2
+
3
+ module Polyfill
4
+ module V2_4
5
+ module File
6
+ include Class
7
+ end
8
+ end
9
+ end