ruuuby-extensions 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -3
- data/Gemfile +1 -1
- data/lib/ruuuby/extensions.rb +166 -20
- data/lib/ruuuby/extensions/version.rb +4 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ebb8cfb655ee14c5bc77da717475557ffb107948
|
4
|
+
data.tar.gz: a66269f18ae8a11459a03bb774837a6391e57efe
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a83abc7ef9025837d1e73970a91fced98be816612988a64b5cdaab8762bbc6fc6ad101be036e6e39756beca19755121c826d181a5a62f496abefccbed9bae7c3
|
7
|
+
data.tar.gz: 461540a3cdc6e31dce7b334f02a26043aece0345a96990e8b0c12983d23821cf8a174367bfde680db475eeca391dd51705530839b761ac44d1caf0a7c64eeff1
|
data/CHANGELOG.md
CHANGED
@@ -1,6 +1,8 @@
|
|
1
|
+
#0.1.1
|
2
|
+
- Further extend classes(Array, String)
|
3
|
+
- Add additional RSpecs
|
4
|
+
- Start abstraction work for type checking and exception throwing
|
5
|
+
- Start tracking code coverage
|
1
6
|
#0.1.0
|
2
7
|
- GEM created, first configurations set
|
3
8
|
- Add function extensions to class(Array)
|
4
|
-
|
5
|
-
|
6
|
-
|
data/Gemfile
CHANGED
data/lib/ruuuby/extensions.rb
CHANGED
@@ -1,47 +1,111 @@
|
|
1
1
|
require "ruuuby/extensions/version"
|
2
2
|
|
3
|
+
# This module + gem simply aims to provide coding quality of life extensions to core classes, somewhat similar to Python's approach.
|
3
4
|
module Ruuuby
|
5
|
+
|
6
|
+
# Empty module. To be used for documenting Ruuuby.
|
4
7
|
module Extensions
|
8
|
+
# Placeholder error class for errors specifically caused by Ruuuby.
|
5
9
|
class Error < StandardError; end
|
10
|
+
end
|
11
|
+
|
12
|
+
# Utility functionalities for checking data types and throw exceptions.
|
13
|
+
module Params
|
14
|
+
# @param [String|*] str
|
15
|
+
#
|
16
|
+
# @return [Boolean] true, if provided param is not nil and inherits from String class.
|
17
|
+
def self.not_string?(str)
|
18
|
+
str == nil || !str.is_a?(String)
|
19
|
+
end
|
20
|
+
|
21
|
+
# @param [Array|*] str
|
22
|
+
#
|
23
|
+
# @return [Boolean] true, if provided param is not nil and inherits from Array class.
|
24
|
+
def self.not_array?(ary)
|
25
|
+
ary == nil || !ary.is_a?(Array)
|
26
|
+
end
|
27
|
+
|
28
|
+
# @param [String|nil|*] param
|
29
|
+
#
|
30
|
+
# @raise [ArgumentError]
|
31
|
+
#
|
32
|
+
# @return [Boolean] true only, exception raised otherwise
|
33
|
+
def self.check_string(param)
|
34
|
+
raise ArgumentError.new(self::bad_type(caller_locations.first.label.to_s, String, param)) if Ruuuby::Params::not_string?(param)
|
35
|
+
true
|
36
|
+
end
|
37
|
+
|
38
|
+
# @param [Array|nil|*] param
|
39
|
+
#
|
40
|
+
# @raise [ArgumentError]
|
41
|
+
#
|
42
|
+
# @return [Boolean] true only, exception raised otherwise
|
43
|
+
def self.check_array(param)
|
44
|
+
raise ArgumentError.new(self::bad_type(caller_locations.first.label.to_s, Array, param)) if Ruuuby::Params::not_array?(param)
|
45
|
+
true
|
46
|
+
end
|
6
47
|
|
7
|
-
|
8
|
-
|
9
|
-
|
48
|
+
# @param [Array|nil|*] param
|
49
|
+
#
|
50
|
+
# @raise [ArgumentError]
|
51
|
+
#
|
52
|
+
# @return [Boolean] true only, exception raised otherwise
|
53
|
+
def self.check_char(param)
|
54
|
+
if param == nil || !param.is_a?(String)
|
55
|
+
error_message = "a param of type{#{String.to_s}}, instead got{#{param.class.to_s}}!"
|
56
|
+
elsif param.chars.length != 1
|
57
|
+
error_message = "a String of length 1, instead got{#{param.length.to_s}}"
|
58
|
+
else
|
59
|
+
return true
|
10
60
|
end
|
61
|
+
raise ArgumentError.new("#{caller_locations.first.label.to_s} requires #{error_message}")
|
11
62
|
end
|
12
63
|
|
64
|
+
private
|
65
|
+
|
66
|
+
# @param [String] func | the name of the original function that triggered this exception
|
67
|
+
# @param [*] required_type | the required data type for the param
|
68
|
+
# @param [*] param | the variable with mis-mitached type
|
69
|
+
#
|
70
|
+
# @return [String]
|
71
|
+
def self.bad_type(func_name, required_type, param)
|
72
|
+
"#{func_name} requires param of type{#{required_type.to_s}}, instead got{#{param.class.to_s}}!"
|
73
|
+
end
|
13
74
|
end
|
14
75
|
end
|
15
76
|
|
16
|
-
unless
|
77
|
+
unless Array.respond_to?(:equal_contents?)
|
78
|
+
|
79
|
+
# Add extensions to the default class(*String*).
|
17
80
|
class Array
|
81
|
+
|
18
82
|
# @param [Array] them | another array to be compared against
|
19
83
|
#
|
84
|
+
# @raise [ArgumentError]
|
85
|
+
#
|
20
86
|
# @return [Array] a new array instance containing values only appearing in base array (and not in provided array)
|
21
87
|
def uniq_to_me(them)
|
22
|
-
|
23
|
-
raise ArgumentError.new("Array.uniq_to_me requires array param, not the provided{#{them.class.to_s}}!")
|
24
|
-
end
|
88
|
+
Ruuuby::Params::check_array(them)
|
25
89
|
self.uniq - them.uniq
|
26
90
|
end
|
27
91
|
|
28
92
|
# @param [Array] them | an array to be compared against
|
29
93
|
#
|
94
|
+
# @raise [ArgumentError]
|
95
|
+
#
|
30
96
|
# @return [Array] a new array instance containing values only appearing in provided array (and not in base array)
|
31
97
|
def uniq_to_them(them)
|
32
|
-
|
33
|
-
raise ArgumentError.new("Array.uniq_to_them requires array param, not the provided{#{them.class.to_s}}!")
|
34
|
-
end
|
98
|
+
Ruuuby::Params::check_array(them)
|
35
99
|
them.uniq - self.uniq
|
36
100
|
end
|
37
101
|
|
38
102
|
# @param [Array] them | an array to be compared against
|
39
103
|
#
|
104
|
+
# @raise [ArgumentError]
|
105
|
+
#
|
40
106
|
# @return [Array] a new array instance containing values only appearing in provided array (and not in base array)
|
41
107
|
def shared_between(them)
|
42
|
-
|
43
|
-
raise ArgumentError.new("Array.shared_between requires array param, not the provided{#{them.class.to_s}}!")
|
44
|
-
end
|
108
|
+
Ruuuby::Params::check_array(them)
|
45
109
|
self.uniq & them.uniq
|
46
110
|
end
|
47
111
|
|
@@ -53,15 +117,97 @@ unless Ruuuby::Extensions::Arrays::already_loaded?
|
|
53
117
|
|
54
118
|
# @param [Array] them
|
55
119
|
#
|
56
|
-
# @
|
120
|
+
# @raise [ArgumentError]
|
121
|
+
#
|
122
|
+
# @return [Boolean] true, if this array and provided array both have same content (*order-matters*: false, *duplicates-matter*: false)
|
57
123
|
def equal_contents?(them)
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
return true if (len_this == 0 && len_them == 0)
|
62
|
-
return false if (len_this != len_them)
|
124
|
+
Ruuuby::Params::check_array(them)
|
125
|
+
return true if (self.length == 0 && them.length == 0)
|
126
|
+
return false if (self.length != them.length)
|
63
127
|
uniq_to_me(them).empty? && uniq_to_them(them).empty?
|
64
128
|
end
|
129
|
+
|
130
|
+
# @param [Array] ending
|
131
|
+
#
|
132
|
+
# @raise [ArgumentError]
|
133
|
+
#
|
134
|
+
# @return [Boolean] true, if this array ends with the elements in the provided parameter array (*order-matters*: true, *duplicates-matter*: true)
|
135
|
+
def end_with?(ending)
|
136
|
+
Ruuuby::Params::check_array(ending)
|
137
|
+
if ending.length == 0
|
138
|
+
self.length == 0
|
139
|
+
else
|
140
|
+
self[(self.length-ending.length)..(self.length-1)] == ending[(0)..(ending.length-1)]
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
144
|
+
# @param [Array] ending
|
145
|
+
#
|
146
|
+
# @raise [ArgumentError]
|
147
|
+
#
|
148
|
+
# @return [Array]
|
149
|
+
def ensure_ending!(ending)
|
150
|
+
Ruuuby::Params::check_array(ending)
|
151
|
+
len_ending = ending.length
|
152
|
+
return self if len_ending == 0 || self.end_with?(ending)
|
153
|
+
len_this = self.length
|
154
|
+
return self << ending[0] if len_ending == 1
|
155
|
+
if len_this == 0
|
156
|
+
ending.each { |element| self << element }
|
157
|
+
return self
|
158
|
+
end
|
159
|
+
delta = 0
|
160
|
+
last_matched = nil
|
161
|
+
while delta <= len_this && delta <= len_ending
|
162
|
+
ending_of_this = self[(len_this-1-delta)..(len_this-1)]
|
163
|
+
starting_of_end = ending[0..delta]
|
164
|
+
last_matched = starting_of_end if ending_of_this == starting_of_end
|
165
|
+
delta += 1
|
166
|
+
end
|
167
|
+
if last_matched == nil
|
168
|
+
ending.each { |element| self << element }
|
169
|
+
else
|
170
|
+
ending[(last_matched.length)..(len_ending-1)].each { |element| self << element }
|
171
|
+
end
|
172
|
+
self
|
173
|
+
end
|
65
174
|
end
|
66
|
-
end
|
67
175
|
|
176
|
+
# Add extensions to the default class(*String*).
|
177
|
+
class String
|
178
|
+
|
179
|
+
# @param [String] ending
|
180
|
+
#
|
181
|
+
# @raise [ArgumentError]
|
182
|
+
#
|
183
|
+
# @return [String]
|
184
|
+
def ensure_ending!(ending)
|
185
|
+
Ruuuby::Params::check_string(ending)
|
186
|
+
return self if ending == '' || self.end_with?(ending)
|
187
|
+
len_this = self.length
|
188
|
+
return self << ending if len_this == 0
|
189
|
+
last_matched = ''
|
190
|
+
len_ending = ending.length
|
191
|
+
delta = 0
|
192
|
+
while delta <= len_this && delta <= len_ending
|
193
|
+
ending_of_this = self[(len_this-1-delta)..(len_this-1)]
|
194
|
+
starting_of_end = ending[0..delta]
|
195
|
+
last_matched = starting_of_end if ending_of_this == starting_of_end
|
196
|
+
delta += 1
|
197
|
+
end
|
198
|
+
self << (last_matched == '' ? ending : ending[last_matched.length..len_ending-1])
|
199
|
+
end
|
200
|
+
|
201
|
+
# @param [String] ending
|
202
|
+
#
|
203
|
+
# @raise [ArgumentError]
|
204
|
+
#
|
205
|
+
# @return [String]
|
206
|
+
def ensure_ending_char!(ending)
|
207
|
+
Ruuuby::Params::check_char(ending)
|
208
|
+
self << ending if self.length == 0 || self[-1] != ending
|
209
|
+
self
|
210
|
+
end
|
211
|
+
end
|
212
|
+
|
213
|
+
end
|
@@ -1,5 +1,8 @@
|
|
1
|
+
# This module + gem simply aims to provide coding quality of life extensions to core classes, somewhat similar to Python's approach.
|
1
2
|
module Ruuuby
|
3
|
+
# Empty module. To be used for documenting Ruuuby.
|
2
4
|
module Extensions
|
3
|
-
|
5
|
+
# Current version of *Ruuuby*.
|
6
|
+
VERSION = "0.1.1"
|
4
7
|
end
|
5
8
|
end
|