lite-ruby 1.0.28 → 1.1.1
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/.rubocop.yml +6 -1
- data/.travis.yml +1 -0
- data/CHANGELOG.md +31 -2
- data/Gemfile.lock +46 -44
- data/README.md +1 -0
- data/docs/ARRAY.md +2 -10
- data/docs/HASH.md +4 -4
- data/docs/NUMERIC.md +0 -10
- data/docs/TIME.md +17 -17
- data/lib/lite/ruby.rb +4 -1
- data/lib/lite/ruby/array.rb +15 -100
- data/lib/lite/ruby/boolean.rb +4 -4
- data/lib/lite/ruby/enumerable.rb +15 -49
- data/lib/lite/ruby/hash.rb +34 -108
- data/lib/lite/ruby/helpers/time_helper.rb +21 -21
- data/lib/lite/ruby/numeric.rb +13 -36
- data/lib/lite/ruby/object.rb +4 -38
- data/lib/lite/ruby/open_struct.rb +2 -3
- data/lib/lite/ruby/range.rb +2 -4
- data/lib/lite/ruby/safe/array.rb +93 -0
- data/lib/lite/ruby/safe/enumerable.rb +42 -0
- data/lib/lite/ruby/safe/hash.rb +83 -0
- data/lib/lite/ruby/safe/object.rb +41 -0
- data/lib/lite/ruby/safe/range.rb +9 -0
- data/lib/lite/ruby/safe/string.rb +183 -0
- data/lib/lite/ruby/string.rb +47 -193
- data/lib/lite/ruby/version.rb +1 -1
- metadata +8 -2
data/lib/lite/ruby/numeric.rb
CHANGED
@@ -15,23 +15,6 @@ if Lite::Ruby.configuration.monkey_patches.include?('numeric')
|
|
15
15
|
self <= upper ? self : upper
|
16
16
|
end
|
17
17
|
|
18
|
-
# rubocop:disable Metrics/MethodLength
|
19
|
-
def clamp(minimum, maximum = nil)
|
20
|
-
if minimum.is_a?(Range)
|
21
|
-
maximum = minimum.max
|
22
|
-
minimum = minimum.min
|
23
|
-
end
|
24
|
-
|
25
|
-
if minimum > self
|
26
|
-
minimum
|
27
|
-
elsif maximum < self
|
28
|
-
maximum
|
29
|
-
else
|
30
|
-
self
|
31
|
-
end
|
32
|
-
end
|
33
|
-
# rubocop:enable Metrics/MethodLength
|
34
|
-
|
35
18
|
def close?(number, epsilon = 0.01)
|
36
19
|
return self == number if epsilon.zero?
|
37
20
|
|
@@ -79,28 +62,22 @@ if Lite::Ruby.configuration.monkey_patches.include?('numeric')
|
|
79
62
|
self == num
|
80
63
|
end
|
81
64
|
|
82
|
-
alias eq? equal_to?
|
83
|
-
|
84
65
|
def fraction
|
85
66
|
(self - truncate).abs
|
86
67
|
end
|
87
68
|
|
88
69
|
def fraction?
|
89
|
-
fraction != 0.0
|
70
|
+
fraction.to_d != 0.0.to_d
|
90
71
|
end
|
91
72
|
|
92
73
|
def greater_than?(num)
|
93
74
|
num < self
|
94
75
|
end
|
95
76
|
|
96
|
-
alias gt? greater_than?
|
97
|
-
|
98
77
|
def greater_than_or_equal_to?(num)
|
99
78
|
num <= self
|
100
79
|
end
|
101
80
|
|
102
|
-
alias gteq? greater_than_or_equal_to?
|
103
|
-
|
104
81
|
def increment(amount = 1.0)
|
105
82
|
self + amount
|
106
83
|
end
|
@@ -113,14 +90,10 @@ if Lite::Ruby.configuration.monkey_patches.include?('numeric')
|
|
113
90
|
num > self
|
114
91
|
end
|
115
92
|
|
116
|
-
alias lt? less_than?
|
117
|
-
|
118
93
|
def less_than_or_equal_to?(num)
|
119
94
|
num >= self
|
120
95
|
end
|
121
96
|
|
122
|
-
alias lteq? less_than_or_equal_to?
|
123
|
-
|
124
97
|
def markdown_percentage(percent)
|
125
98
|
to_f * ((100.0 - percent.to_f) / 100.0)
|
126
99
|
end
|
@@ -143,10 +116,6 @@ if Lite::Ruby.configuration.monkey_patches.include?('numeric')
|
|
143
116
|
self != num
|
144
117
|
end
|
145
118
|
|
146
|
-
alias not_eq? not_equal_to?
|
147
|
-
alias inequal_to? not_equal_to?
|
148
|
-
alias ineq? not_equal_to?
|
149
|
-
|
150
119
|
def ordinal
|
151
120
|
return 'th' if (11..13).cover?(abs % 100)
|
152
121
|
|
@@ -173,7 +142,7 @@ if Lite::Ruby.configuration.monkey_patches.include?('numeric')
|
|
173
142
|
to_s.rjust(precision, pad_number.to_s)
|
174
143
|
end
|
175
144
|
|
176
|
-
# rubocop:disable Metrics/AbcSize, Metrics/MethodLength
|
145
|
+
# rubocop:disable Metrics/AbcSize, Metrics/MethodLength
|
177
146
|
def pad_precision(options = {})
|
178
147
|
pad_number = options[:pad_number] || 0
|
179
148
|
precision = options[:precision] || 2
|
@@ -190,7 +159,7 @@ if Lite::Ruby.configuration.monkey_patches.include?('numeric')
|
|
190
159
|
string[0..(ljust_count - 1)]
|
191
160
|
end
|
192
161
|
end
|
193
|
-
# rubocop:enable Metrics/AbcSize, Metrics/MethodLength
|
162
|
+
# rubocop:enable Metrics/AbcSize, Metrics/MethodLength
|
194
163
|
|
195
164
|
def percentage_of(number)
|
196
165
|
return 0 if zero? || number.zero?
|
@@ -206,8 +175,6 @@ if Lite::Ruby.configuration.monkey_patches.include?('numeric')
|
|
206
175
|
(self - value)..(self + value)
|
207
176
|
end
|
208
177
|
|
209
|
-
alias plus_minus range
|
210
|
-
|
211
178
|
def root(num)
|
212
179
|
self**(1.0 / num)
|
213
180
|
end
|
@@ -263,5 +230,15 @@ if Lite::Ruby.configuration.monkey_patches.include?('numeric')
|
|
263
230
|
end
|
264
231
|
end
|
265
232
|
|
233
|
+
alias eq? equal_to?
|
234
|
+
alias gt? greater_than?
|
235
|
+
alias gteq? greater_than_or_equal_to?
|
236
|
+
alias inequal_to? not_equal_to?
|
237
|
+
alias ineq? not_equal_to?
|
238
|
+
alias lt? less_than?
|
239
|
+
alias lteq? less_than_or_equal_to?
|
240
|
+
alias not_eq? not_equal_to?
|
241
|
+
alias plus_minus range
|
242
|
+
|
266
243
|
end
|
267
244
|
end
|
data/lib/lite/ruby/object.rb
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
if Lite::Ruby.configuration.monkey_patches.include?('object')
|
4
|
+
require 'lite/ruby/safe/object' unless defined?(ActiveSupport)
|
5
|
+
|
4
6
|
class Object
|
5
7
|
|
6
8
|
FALSE_VALUES ||= %w[
|
@@ -14,14 +16,6 @@ if Lite::Ruby.configuration.monkey_patches.include?('object')
|
|
14
16
|
is_a?(Array)
|
15
17
|
end
|
16
18
|
|
17
|
-
def blank?
|
18
|
-
object = self
|
19
|
-
object = object.strip if respond_to?(:strip)
|
20
|
-
return object.empty? if respond_to?(:empty?)
|
21
|
-
|
22
|
-
!object
|
23
|
-
end
|
24
|
-
|
25
19
|
def bool?
|
26
20
|
true? || false?
|
27
21
|
end
|
@@ -35,14 +29,6 @@ if Lite::Ruby.configuration.monkey_patches.include?('object')
|
|
35
29
|
is_a?(Date)
|
36
30
|
end
|
37
31
|
|
38
|
-
def deep_dup
|
39
|
-
duplicable? ? dup : self
|
40
|
-
end
|
41
|
-
|
42
|
-
def duplicable?
|
43
|
-
true
|
44
|
-
end
|
45
|
-
|
46
32
|
# rubocop:disable Style/YodaCondition
|
47
33
|
def false?
|
48
34
|
false == self
|
@@ -87,14 +73,6 @@ if Lite::Ruby.configuration.monkey_patches.include?('object')
|
|
87
73
|
to_s == to_s.reverse
|
88
74
|
end
|
89
75
|
|
90
|
-
def present?
|
91
|
-
!blank?
|
92
|
-
end
|
93
|
-
|
94
|
-
def presence
|
95
|
-
self if present?
|
96
|
-
end
|
97
|
-
|
98
76
|
def range?
|
99
77
|
is_a?(Range)
|
100
78
|
end
|
@@ -160,8 +138,6 @@ if Lite::Ruby.configuration.monkey_patches.include?('object')
|
|
160
138
|
nil
|
161
139
|
end
|
162
140
|
|
163
|
-
alias to_b to_bool
|
164
|
-
|
165
141
|
# rubocop:disable Style/YodaCondition
|
166
142
|
def true?
|
167
143
|
true == self
|
@@ -172,18 +148,6 @@ if Lite::Ruby.configuration.monkey_patches.include?('object')
|
|
172
148
|
TRUE_VALUES.include?(to_s.downcase)
|
173
149
|
end
|
174
150
|
|
175
|
-
def try(*obj, &block)
|
176
|
-
try!(*obj, &block) if obj.empty? || respond_to?(obj.first)
|
177
|
-
end
|
178
|
-
|
179
|
-
def try!(*obj, &block)
|
180
|
-
if obj.empty? && block_given?
|
181
|
-
block.arity.zero? ? instance_eval(&block) : yield(self)
|
182
|
-
else
|
183
|
-
public_send(*obj, &block)
|
184
|
-
end
|
185
|
-
end
|
186
|
-
|
187
151
|
def try_call(*keys)
|
188
152
|
return unless respond_to?(:call)
|
189
153
|
|
@@ -196,5 +160,7 @@ if Lite::Ruby.configuration.monkey_patches.include?('object')
|
|
196
160
|
nil
|
197
161
|
end
|
198
162
|
|
163
|
+
alias to_b to_bool
|
164
|
+
|
199
165
|
end
|
200
166
|
end
|
@@ -14,7 +14,7 @@ if Lite::Ruby.configuration.monkey_patches.include?('open_struct')
|
|
14
14
|
|
15
15
|
hash&.each do |key, val|
|
16
16
|
@table[key.to_sym] = val
|
17
|
-
new_ostruct_member(key)
|
17
|
+
new_ostruct_member!(key)
|
18
18
|
end
|
19
19
|
|
20
20
|
yield self if block && block.arity == 1
|
@@ -46,13 +46,12 @@ if Lite::Ruby.configuration.monkey_patches.include?('open_struct')
|
|
46
46
|
{ table: attributes }
|
47
47
|
end
|
48
48
|
|
49
|
-
alias to_h to_hash
|
50
|
-
|
51
49
|
def to_json(table: true)
|
52
50
|
to_hash(table: table).to_json
|
53
51
|
end
|
54
52
|
|
55
53
|
alias as_json to_json
|
54
|
+
alias to_h to_hash
|
56
55
|
|
57
56
|
end
|
58
57
|
end
|
data/lib/lite/ruby/range.rb
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
if Lite::Ruby.configuration.monkey_patches.include?('range')
|
4
|
+
require 'lite/ruby/safe/range' unless defined?(ActiveSupport)
|
5
|
+
|
4
6
|
class Range
|
5
7
|
|
6
8
|
def combine(other)
|
@@ -14,10 +16,6 @@ if Lite::Ruby.configuration.monkey_patches.include?('range')
|
|
14
16
|
include?(other.first) && other.last.send(operator, last)
|
15
17
|
end
|
16
18
|
|
17
|
-
def overlaps?(other)
|
18
|
-
cover?(other.first) || other.cover?(first)
|
19
|
-
end
|
20
|
-
|
21
19
|
def sample
|
22
20
|
to_a.sample
|
23
21
|
end
|
@@ -0,0 +1,93 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class Array
|
4
|
+
|
5
|
+
def deep_dup
|
6
|
+
map(&:deep_dup)
|
7
|
+
end
|
8
|
+
|
9
|
+
def from(position)
|
10
|
+
self[position, size] || []
|
11
|
+
end
|
12
|
+
|
13
|
+
# rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength
|
14
|
+
def in_groups(number, fill_with = nil, &block)
|
15
|
+
collection_size = size
|
16
|
+
division = collection_size.div(number)
|
17
|
+
modulo = collection_size % number
|
18
|
+
collection = []
|
19
|
+
start = 0
|
20
|
+
|
21
|
+
number.times do |int|
|
22
|
+
mod_gt_zero = modulo.positive?
|
23
|
+
grouping = division + (mod_gt_zero && modulo > int ? 1 : 0)
|
24
|
+
collection << last_group = slice(start, grouping)
|
25
|
+
last_group << fill_with if (fill_with != false) && mod_gt_zero && (grouping == division)
|
26
|
+
start += grouping
|
27
|
+
end
|
28
|
+
|
29
|
+
return collection unless defined?(yield)
|
30
|
+
|
31
|
+
collection.each(&block)
|
32
|
+
end
|
33
|
+
# rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength
|
34
|
+
|
35
|
+
# rubocop:disable Metrics/MethodLength, Style/GuardClause
|
36
|
+
def in_groups_of(number, fill_with = nil, &block)
|
37
|
+
if number.to_i <= 0
|
38
|
+
raise ArgumentError, "Group size must be a positive integer, was #{number.inspect}"
|
39
|
+
elsif fill_with == false
|
40
|
+
collection = self
|
41
|
+
else
|
42
|
+
padding = (number - size % number) % number
|
43
|
+
collection = dup.concat(Array.new(padding, fill_with))
|
44
|
+
end
|
45
|
+
|
46
|
+
sliced_collection = collection.each_slice(number)
|
47
|
+
return sliced_collection.to_a unless defined?(yield)
|
48
|
+
|
49
|
+
sliced_collection(&block)
|
50
|
+
end
|
51
|
+
# rubocop:enable Metrics/MethodLength, Style/GuardClause
|
52
|
+
|
53
|
+
# rubocop:disable Metrics/MethodLength, Style/ExplicitBlockArgument
|
54
|
+
def split(value = nil)
|
55
|
+
arr = dup
|
56
|
+
result = []
|
57
|
+
|
58
|
+
if block_given?
|
59
|
+
while (idx = arr.index { |i| yield(i) })
|
60
|
+
result << arr.shift(idx)
|
61
|
+
arr.shift
|
62
|
+
end
|
63
|
+
else
|
64
|
+
while (idx = arr.index(value))
|
65
|
+
result << arr.shift(idx)
|
66
|
+
arr.shift
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
result << arr
|
71
|
+
end
|
72
|
+
# rubocop:enable Metrics/MethodLength, Style/ExplicitBlockArgument
|
73
|
+
|
74
|
+
def to(position)
|
75
|
+
return first(position + 1) if position >= 0
|
76
|
+
|
77
|
+
self[0..position]
|
78
|
+
end
|
79
|
+
|
80
|
+
def to_sentence(options = {})
|
81
|
+
words_connector = options[:words_connector] || ', '
|
82
|
+
two_words_connector = options[:two_words_connector] || ' and '
|
83
|
+
last_word_connector = options[:last_word_connector] || ', and '
|
84
|
+
|
85
|
+
case size
|
86
|
+
when 0 then ''
|
87
|
+
when 1 then self[0].to_s
|
88
|
+
when 2 then "#{self[0]}#{two_words_connector}#{self[1]}"
|
89
|
+
else "#{self[0...-1].join(words_connector)}#{last_word_connector}#{self[-1]}"
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Enumerable
|
4
|
+
|
5
|
+
def exclude?(object)
|
6
|
+
!include?(object)
|
7
|
+
end
|
8
|
+
|
9
|
+
def excluding(*elements)
|
10
|
+
elements.flatten!(1)
|
11
|
+
reject { |element| elements.include?(element) }
|
12
|
+
end
|
13
|
+
|
14
|
+
def including(*elements)
|
15
|
+
to_a.including(*elements)
|
16
|
+
end
|
17
|
+
|
18
|
+
def many?
|
19
|
+
found_count = 0
|
20
|
+
|
21
|
+
if defined?(yield)
|
22
|
+
any? do |val|
|
23
|
+
found_count += 1 if yield(val)
|
24
|
+
found_count > 1
|
25
|
+
end
|
26
|
+
else
|
27
|
+
any? { (found_count += 1) > 1 }
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def pluck(*keys)
|
32
|
+
if keys.many?
|
33
|
+
map { |element| keys.map { |key| element[key] } }
|
34
|
+
else
|
35
|
+
map { |element| element[keys.first] }
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
alias with including
|
40
|
+
alias without excluding
|
41
|
+
|
42
|
+
end
|
@@ -0,0 +1,83 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class Hash
|
4
|
+
|
5
|
+
# rubocop:disable Style/CaseEquality
|
6
|
+
def deep_dup
|
7
|
+
hash = dup
|
8
|
+
|
9
|
+
each_pair do |key, value|
|
10
|
+
if key.frozen? && ::String === key
|
11
|
+
hash[key] = value.deep_dup
|
12
|
+
else
|
13
|
+
hash.delete(key)
|
14
|
+
hash[key.deep_dup] = value.deep_dup
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
hash
|
19
|
+
end
|
20
|
+
# rubocop:enable Style/CaseEquality
|
21
|
+
|
22
|
+
def deep_merge(other_hash, &block)
|
23
|
+
dup.deep_merge!(other_hash, &block)
|
24
|
+
end
|
25
|
+
|
26
|
+
def deep_merge!(other_hash, &block)
|
27
|
+
merge!(other_hash) do |key, this_val, other_val|
|
28
|
+
if this_val.is_a?(Hash) && other_val.is_a?(Hash)
|
29
|
+
this_val.deep_merge(other_val, &block)
|
30
|
+
elsif defined?(yield)
|
31
|
+
yield(key, this_val, other_val)
|
32
|
+
else
|
33
|
+
other_val
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def except(*keys)
|
39
|
+
slice(*self.keys - keys)
|
40
|
+
end
|
41
|
+
|
42
|
+
def except!(*keys)
|
43
|
+
keys.each { |key| delete(key) }
|
44
|
+
self
|
45
|
+
end
|
46
|
+
|
47
|
+
def extract!(*keys)
|
48
|
+
keys.each_with_object({}) { |key, hash| hash[key] = delete(key) if key?(key) }
|
49
|
+
end
|
50
|
+
|
51
|
+
def reverse_merge(other_hash)
|
52
|
+
other_hash.merge(self)
|
53
|
+
end
|
54
|
+
|
55
|
+
def reverse_merge!(other_hash)
|
56
|
+
other_hash.merge!(self)
|
57
|
+
end
|
58
|
+
|
59
|
+
def stringify_keys
|
60
|
+
transform_keys(&:to_s)
|
61
|
+
end
|
62
|
+
|
63
|
+
def stringify_keys!
|
64
|
+
transform_keys!(&:to_s)
|
65
|
+
end
|
66
|
+
|
67
|
+
def symbolize_keys
|
68
|
+
transform_keys do |key|
|
69
|
+
key.to_sym
|
70
|
+
rescue StandardError
|
71
|
+
key
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
def symbolize_keys!
|
76
|
+
transform_keys! do |key|
|
77
|
+
key.to_sym
|
78
|
+
rescue StandardError
|
79
|
+
key
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
end
|