lite-ruby 1.0.27 → 1.1.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/.rubocop.yml +8 -1
- data/.travis.yml +1 -0
- data/CHANGELOG.md +33 -2
- data/Gemfile.lock +47 -45
- 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/STRING.md +11 -20
- data/docs/TIME.md +17 -17
- data/lib/lite/ruby.rb +4 -1
- data/lib/lite/ruby/array.rb +15 -102
- 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 -38
- 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 +187 -0
- data/lib/lite/ruby/string.rb +65 -218
- data/lib/lite/ruby/version.rb +1 -1
- metadata +8 -2
@@ -9,9 +9,9 @@ if Lite::Ruby.configuration.monkey_patches.include?('time')
|
|
9
9
|
hour: '%H',
|
10
10
|
hour_blank: '%k',
|
11
11
|
hour_padded: '%H',
|
12
|
-
|
13
|
-
|
14
|
-
|
12
|
+
hour12: '%I',
|
13
|
+
hour12_padded: '%I',
|
14
|
+
hour12_blank: '%l',
|
15
15
|
minute: '%M',
|
16
16
|
second: '%S',
|
17
17
|
ampm: '%P',
|
@@ -25,29 +25,29 @@ if Lite::Ruby.configuration.monkey_patches.include?('time')
|
|
25
25
|
time_padded: '%H:%M',
|
26
26
|
time_tz: '%H:%M %z',
|
27
27
|
time_tzn: '%H:%M %Z',
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
28
|
+
time12: '%I:%M %P',
|
29
|
+
time12_padded: '%I:%M %P',
|
30
|
+
time12_blank: '%l:%M %P',
|
31
|
+
time12_tz: '%I:%M %P %z',
|
32
|
+
time12_tzn: '%I:%M %P %Z',
|
33
33
|
daytime: '%B %-d %H:%M',
|
34
34
|
daytime_abbr: '%b %-d %H:%M',
|
35
35
|
daytime_iso: '%m-%d %H:%M',
|
36
|
-
|
37
|
-
|
38
|
-
|
36
|
+
daytime12: '%B %-d %I:%M %P',
|
37
|
+
daytime12_abbr: '%b %-d %I:%M %P',
|
38
|
+
daytime12_iso: '%m-%d %I:%M %P',
|
39
39
|
datetime: '%B %-d, %Y %H:%M',
|
40
40
|
datetime_abbr: '%b %-d, %Y %H:%M',
|
41
41
|
datetime_iso: '%Y-%m-%d %H:%M',
|
42
|
-
|
43
|
-
|
44
|
-
|
42
|
+
datetime12: '%B %-d, %Y %I:%M %P',
|
43
|
+
datetime12_abbr: '%b %-d, %Y %I:%M %P',
|
44
|
+
datetime12_iso: '%Y-%m-%d %I:%M %P',
|
45
45
|
datetime_tzn: '%B %-d, %Y %H:%M %Z',
|
46
46
|
datetime_abbr_tzn: '%b %-d, %Y %H:%M %Z',
|
47
47
|
datetime_iso_tzn: '%Y-%m-%d %H:%M %z',
|
48
|
-
|
49
|
-
|
50
|
-
|
48
|
+
datetime12_tzn: '%B %-d, %Y %I:%M %P %Z',
|
49
|
+
datetime12_abbr_tzn: '%b %-d, %Y %I:%M %P %Z',
|
50
|
+
datetime12_iso_tzn: '%Y-%m-%d %I:%M %P %z'
|
51
51
|
}.freeze
|
52
52
|
TIME_UNITS = {
|
53
53
|
h: 'H',
|
@@ -57,11 +57,11 @@ if Lite::Ruby.configuration.monkey_patches.include?('time')
|
|
57
57
|
HOUR: 'k',
|
58
58
|
hour_blank: 'k',
|
59
59
|
hhh: 'I',
|
60
|
-
|
61
|
-
|
60
|
+
hour12: 'I',
|
61
|
+
hour12_padded: 'I',
|
62
62
|
hhhh: 'l',
|
63
|
-
|
64
|
-
|
63
|
+
HOUR12: 'l',
|
64
|
+
hour12_blank: 'l',
|
65
65
|
n: 'M',
|
66
66
|
minute: 'M',
|
67
67
|
s: 'S',
|
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,8 +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/
|
177
|
-
# rubocop:disable Metrics/MethodLength, Metrics/PerceivedComplexity
|
145
|
+
# rubocop:disable Metrics/AbcSize, Metrics/MethodLength
|
178
146
|
def pad_precision(options = {})
|
179
147
|
pad_number = options[:pad_number] || 0
|
180
148
|
precision = options[:precision] || 2
|
@@ -191,8 +159,7 @@ if Lite::Ruby.configuration.monkey_patches.include?('numeric')
|
|
191
159
|
string[0..(ljust_count - 1)]
|
192
160
|
end
|
193
161
|
end
|
194
|
-
# rubocop:enable Metrics/AbcSize, Metrics/
|
195
|
-
# rubocop:enable Metrics/MethodLength, Metrics/PerceivedComplexity
|
162
|
+
# rubocop:enable Metrics/AbcSize, Metrics/MethodLength
|
196
163
|
|
197
164
|
def percentage_of(number)
|
198
165
|
return 0 if zero? || number.zero?
|
@@ -208,8 +175,6 @@ if Lite::Ruby.configuration.monkey_patches.include?('numeric')
|
|
208
175
|
(self - value)..(self + value)
|
209
176
|
end
|
210
177
|
|
211
|
-
alias plus_minus range
|
212
|
-
|
213
178
|
def root(num)
|
214
179
|
self**(1.0 / num)
|
215
180
|
end
|
@@ -265,5 +230,15 @@ if Lite::Ruby.configuration.monkey_patches.include?('numeric')
|
|
265
230
|
end
|
266
231
|
end
|
267
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
|
+
|
268
243
|
end
|
269
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
|