lite-ruby 1.0.14 → 1.0.15
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 -1
- data/Gemfile.lock +1 -1
- data/lib/lite/ruby/array.rb +221 -219
- data/lib/lite/ruby/boolean.rb +18 -16
- data/lib/lite/ruby/date.rb +18 -16
- data/lib/lite/ruby/enumerable.rb +169 -167
- data/lib/lite/ruby/hash.rb +309 -307
- data/lib/lite/ruby/helpers/date_helper.rb +97 -95
- data/lib/lite/ruby/helpers/time_helper.rb +79 -77
- data/lib/lite/ruby/integer.rb +57 -55
- data/lib/lite/ruby/kernel.rb +21 -19
- data/lib/lite/ruby/numeric.rb +189 -187
- data/lib/lite/ruby/object.rb +138 -136
- data/lib/lite/ruby/open_struct.rb +30 -28
- data/lib/lite/ruby/range.rb +23 -21
- data/lib/lite/ruby/string.rb +495 -493
- data/lib/lite/ruby/struct.rb +15 -13
- data/lib/lite/ruby/time.rb +27 -25
- data/lib/lite/ruby/version.rb +1 -1
- metadata +1 -1
@@ -1,105 +1,107 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
module
|
5
|
-
module
|
3
|
+
if Lite::Ruby.configuration.monkey_patches.include?('date')
|
4
|
+
module Lite
|
5
|
+
module Ruby
|
6
|
+
module DateHelper
|
6
7
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
8
|
+
DATE_STAMPS ||= {
|
9
|
+
weekday: '%d',
|
10
|
+
weekday_blank: '%_d',
|
11
|
+
weekday_padded: '%d',
|
12
|
+
weekday_unpadded: '%-d',
|
13
|
+
weekday_name: '%A',
|
14
|
+
weekday_name_abbr: '%a',
|
15
|
+
month: '%m',
|
16
|
+
month_blank: '%_m',
|
17
|
+
month_padded: '%m',
|
18
|
+
month_unpadded: '%-m',
|
19
|
+
month_name: '%B',
|
20
|
+
month_name_abbr: '%b',
|
21
|
+
week_iso: '%V',
|
22
|
+
week_monday: '%W',
|
23
|
+
week_sunday: '%U',
|
24
|
+
year: '%Y',
|
25
|
+
yr: '%y',
|
26
|
+
year_abbr: '%y',
|
27
|
+
day: '%B %-d',
|
28
|
+
day_abbr: '%b %-d',
|
29
|
+
day_iso: '%m-%d',
|
30
|
+
month_year: '%m %Y',
|
31
|
+
month_blank_year: '%_m %Y',
|
32
|
+
month_padded_year: '%m %Y',
|
33
|
+
month_unpadded_year: '%-m %Y',
|
34
|
+
month_name_year: '%B %Y',
|
35
|
+
month_name_abbr_year: '%b %Y',
|
36
|
+
week_year_iso: '%V-%G',
|
37
|
+
year_day: '%Y-%m-%d',
|
38
|
+
year_month: '%Y-%m',
|
39
|
+
year_week: '%G-%V',
|
40
|
+
date: '%B %-d, %Y',
|
41
|
+
date_abbr: '%b %-d, %Y',
|
42
|
+
date_iso: '%Y-%m-%d'
|
43
|
+
}.freeze
|
44
|
+
DATE_UNITS ||= {
|
45
|
+
w: 'u',
|
46
|
+
weekday: 'u',
|
47
|
+
ww: 'w',
|
48
|
+
weekday_offset: 'w',
|
49
|
+
www: 'A',
|
50
|
+
weekday_name: 'A',
|
51
|
+
wwww: 'a',
|
52
|
+
weekday_name_abbr: 'a',
|
53
|
+
d: 'd',
|
54
|
+
day: 'd',
|
55
|
+
day_padded: 'd',
|
56
|
+
dd: '-d',
|
57
|
+
Day: '-d',
|
58
|
+
day_unpadded: '-d',
|
59
|
+
ddd: '_d',
|
60
|
+
DAY: '_d',
|
61
|
+
day_blank: '_d',
|
62
|
+
dddd: 'j',
|
63
|
+
day_of_the_year: 'j',
|
64
|
+
m: 'm',
|
65
|
+
month: 'm',
|
66
|
+
month_padded: 'm',
|
67
|
+
mm: '-m',
|
68
|
+
Month: '-m',
|
69
|
+
month_unpadded: '-m',
|
70
|
+
mmm: '_m',
|
71
|
+
MONTH: '_m',
|
72
|
+
month_blank: '_m',
|
73
|
+
mmmm: 'B',
|
74
|
+
month_name: 'B',
|
75
|
+
mmmmm: 'b',
|
76
|
+
month_name_abbr: 'b',
|
77
|
+
swe: 'U',
|
78
|
+
sunday_week: 'U',
|
79
|
+
mwe: 'W',
|
80
|
+
monday_week: 'W',
|
81
|
+
we: 'V',
|
82
|
+
week: 'V',
|
83
|
+
yy: 'y',
|
84
|
+
yr: 'y',
|
85
|
+
year: 'Y',
|
86
|
+
yyyy: 'Y'
|
87
|
+
}.freeze
|
87
88
|
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
89
|
+
def format(string = nil)
|
90
|
+
string ||= default_format
|
91
|
+
delimiters = string.scan(/\W+/)
|
92
|
+
formatters = string.scan(/[a-z0-9_]+/i)
|
93
|
+
string = formatters.map { |key| "%#{format_for(key.to_sym)}#{delimiters.shift}" }
|
94
|
+
strftime(string.join)
|
95
|
+
end
|
95
96
|
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
97
|
+
def stamp(key = nil)
|
98
|
+
key = stamp_for(key&.to_sym || default_stamp)
|
99
|
+
strftime(key)
|
100
|
+
end
|
100
101
|
|
101
|
-
|
102
|
+
alias to_format stamp
|
102
103
|
|
104
|
+
end
|
103
105
|
end
|
104
106
|
end
|
105
107
|
end
|
@@ -1,84 +1,86 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
module
|
5
|
-
module
|
3
|
+
if Lite::Ruby.configuration.monkey_patches.include?('time')
|
4
|
+
module Lite
|
5
|
+
module Ruby
|
6
|
+
module TimeHelper
|
6
7
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
8
|
+
TIME_STAMPS ||= {
|
9
|
+
hour: '%H',
|
10
|
+
hour_blank: '%k',
|
11
|
+
hour_padded: '%H',
|
12
|
+
hour_12: '%I',
|
13
|
+
hour_12_padded: '%I',
|
14
|
+
hour_12_blank: '%l',
|
15
|
+
minute: '%M',
|
16
|
+
second: '%S',
|
17
|
+
ampm: '%P',
|
18
|
+
meridian: '%p',
|
19
|
+
time_zone: '%z',
|
20
|
+
time_zone_offset: '%:z',
|
21
|
+
time_zone_offset_full: '%::z',
|
22
|
+
time_zone_name: '%Z',
|
23
|
+
time: '%H:%M',
|
24
|
+
time_blank: '%k:%M',
|
25
|
+
time_padded: '%H:%M',
|
26
|
+
time_tz: '%H:%M %z',
|
27
|
+
time_tzn: '%H:%M %Z',
|
28
|
+
time_12: '%I:%M %P',
|
29
|
+
time_12_padded: '%I:%M %P',
|
30
|
+
time_12_blank: '%l:%M %P',
|
31
|
+
time_12_tz: '%I:%M %P %z',
|
32
|
+
time_12_tzn: '%I:%M %P %Z',
|
33
|
+
daytime: '%B %-d %H:%M',
|
34
|
+
daytime_abbr: '%b %-d %H:%M',
|
35
|
+
daytime_iso: '%m-%d %H:%M',
|
36
|
+
daytime_12: '%B %-d %I:%M %P',
|
37
|
+
daytime_12_abbr: '%b %-d %I:%M %P',
|
38
|
+
daytime_12_iso: '%m-%d %I:%M %P',
|
39
|
+
datetime: '%B %-d, %Y %H:%M',
|
40
|
+
datetime_abbr: '%b %-d, %Y %H:%M',
|
41
|
+
datetime_iso: '%Y-%m-%d %H:%M',
|
42
|
+
datetime_12: '%B %-d, %Y %I:%M %P',
|
43
|
+
datetime_12_abbr: '%b %-d, %Y %I:%M %P',
|
44
|
+
datetime_12_iso: '%Y-%m-%d %I:%M %P',
|
45
|
+
datetime_tzn: '%B %-d, %Y %H:%M %Z',
|
46
|
+
datetime_abbr_tzn: '%b %-d, %Y %H:%M %Z',
|
47
|
+
datetime_iso_tzn: '%Y-%m-%d %H:%M %z',
|
48
|
+
datetime_12_tzn: '%B %-d, %Y %I:%M %P %Z',
|
49
|
+
datetime_12_abbr_tzn: '%b %-d, %Y %I:%M %P %Z',
|
50
|
+
datetime_12_iso_tzn: '%Y-%m-%d %I:%M %P %z'
|
51
|
+
}.freeze
|
52
|
+
TIME_UNITS = {
|
53
|
+
h: 'H',
|
54
|
+
hour: 'H',
|
55
|
+
hour_padded: 'H',
|
56
|
+
hh: 'k',
|
57
|
+
HOUR: 'k',
|
58
|
+
hour_blank: 'k',
|
59
|
+
hhh: 'I',
|
60
|
+
hour_12: 'I',
|
61
|
+
hour_12_padded: 'I',
|
62
|
+
hhhh: 'l',
|
63
|
+
HOUR_12: 'l',
|
64
|
+
hour_12_blank: 'l',
|
65
|
+
n: 'M',
|
66
|
+
minute: 'M',
|
67
|
+
s: 'S',
|
68
|
+
second: 'S',
|
69
|
+
ampm: 'P',
|
70
|
+
meridian: 'P',
|
71
|
+
AMPM: 'p',
|
72
|
+
MERIDIAN: 'p',
|
73
|
+
z: 'z',
|
74
|
+
time_zone: 'z',
|
75
|
+
zz: ':z',
|
76
|
+
time_zone_offset: ':z',
|
77
|
+
zzz: '::z',
|
78
|
+
time_zone_offset_full: '::z',
|
79
|
+
zzzz: 'Z',
|
80
|
+
time_zone_name: 'Z'
|
81
|
+
}.freeze
|
81
82
|
|
83
|
+
end
|
82
84
|
end
|
83
85
|
end
|
84
86
|
end
|
data/lib/lite/ruby/integer.rb
CHANGED
@@ -1,77 +1,79 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
3
|
+
if Lite::Ruby.configuration.monkey_patches.include?('integer')
|
4
|
+
class Integer
|
5
|
+
|
6
|
+
ROMAN_NUMERALS ||= {
|
7
|
+
M: 1000, CM: 900, D: 500, CD: 400, C: 100, XC: 90, L: 50, XL: 40, X: 10, IX: 9, V: 5, IV: 4,
|
8
|
+
I: 1
|
9
|
+
}.freeze
|
10
|
+
|
11
|
+
def bit(bit)
|
12
|
+
if bit.negative?
|
13
|
+
mask = (1 << ~bit)
|
14
|
+
self & ~mask
|
15
|
+
else
|
16
|
+
mask = (1 << bit)
|
17
|
+
self | mask
|
18
|
+
end
|
19
|
+
end
|
9
20
|
|
10
|
-
|
11
|
-
if bit.negative?
|
12
|
-
mask = (1 << ~bit)
|
13
|
-
self & ~mask
|
14
|
-
else
|
21
|
+
def bit?(bit)
|
15
22
|
mask = (1 << bit)
|
16
|
-
self
|
23
|
+
(self & mask) != 0
|
17
24
|
end
|
18
|
-
end
|
19
|
-
|
20
|
-
def bit?(bit)
|
21
|
-
mask = (1 << bit)
|
22
|
-
(self & mask) != 0
|
23
|
-
end
|
24
25
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
26
|
+
def bit_clear(bit)
|
27
|
+
mask = (1 << bit)
|
28
|
+
self & ~mask
|
29
|
+
end
|
29
30
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
31
|
+
def bitmask(mask)
|
32
|
+
if mask.negative?
|
33
|
+
self & mask
|
34
|
+
else
|
35
|
+
self | mask
|
36
|
+
end
|
35
37
|
end
|
36
|
-
end
|
37
38
|
|
38
|
-
|
39
|
-
|
40
|
-
|
39
|
+
def bitmask?(mask)
|
40
|
+
(self & mask) != 0
|
41
|
+
end
|
41
42
|
|
42
|
-
|
43
|
-
|
43
|
+
def factorial
|
44
|
+
return 1 if zero?
|
44
45
|
|
45
|
-
|
46
|
-
|
46
|
+
2.upto(self).inject(1) { |acc, i| acc * i }
|
47
|
+
end
|
47
48
|
|
48
|
-
|
49
|
-
|
49
|
+
def factors
|
50
|
+
limit = Math.sqrt(self).floor
|
50
51
|
|
51
|
-
|
52
|
-
|
52
|
+
(1..limit).each_with_object([]) do |i, array|
|
53
|
+
next unless (self % i).zero?
|
53
54
|
|
54
|
-
|
55
|
-
|
56
|
-
|
55
|
+
sq = (self / i)
|
56
|
+
array.push(i)
|
57
|
+
array.push(sq) if sq != i
|
58
|
+
end
|
57
59
|
end
|
58
|
-
end
|
59
60
|
|
60
|
-
|
61
|
-
|
62
|
-
|
61
|
+
def of(&block)
|
62
|
+
Array.new(self, &block)
|
63
|
+
end
|
63
64
|
|
64
|
-
|
65
|
-
|
66
|
-
|
65
|
+
def roman_numeral
|
66
|
+
return '' if zero?
|
67
|
+
return "-#{(-self).roman_numeral}" if negative?
|
67
68
|
|
68
|
-
|
69
|
-
|
69
|
+
ROMAN_NUMERALS.each { |key, val| break "#{key}#{(self - val).roman_numeral}" if val <= self }
|
70
|
+
end
|
70
71
|
|
71
|
-
|
72
|
-
|
73
|
-
|
72
|
+
def to_time
|
73
|
+
Time.at(self)
|
74
|
+
end
|
74
75
|
|
75
|
-
|
76
|
+
alias to_t to_time unless defined?(to_t)
|
76
77
|
|
78
|
+
end
|
77
79
|
end
|
data/lib/lite/ruby/kernel.rb
CHANGED
@@ -1,29 +1,31 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
3
|
+
if Lite::Ruby.configuration.monkey_patches.include?('kernel')
|
4
|
+
module Kernel
|
4
5
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
6
|
+
# rubocop:disable Lint/RescueException, Security/Eval
|
7
|
+
def safe_eval
|
8
|
+
eval(self)
|
9
|
+
rescue Exception
|
10
|
+
self
|
11
|
+
end
|
11
12
|
|
12
|
-
|
13
|
-
|
14
|
-
|
13
|
+
def try_eval
|
14
|
+
val = /\[\d*,?\d*,?\d*\]/.match(to_s).to_s
|
15
|
+
return val if val.nil?
|
15
16
|
|
16
|
-
|
17
|
-
|
18
|
-
|
17
|
+
eval(val)
|
18
|
+
end
|
19
|
+
# rubocop:enable Lint/RescueException, Security/Eval
|
19
20
|
|
20
|
-
|
21
|
+
private
|
21
22
|
|
22
|
-
|
23
|
-
|
24
|
-
|
23
|
+
def caller_name(depth = 0)
|
24
|
+
val = caller[depth][/`([^']*)'/, 1]
|
25
|
+
return val if depth.zero? || !val.include?('<top (required)>')
|
25
26
|
|
26
|
-
|
27
|
-
|
27
|
+
caller[depth - 1][/`([^']*)'/, 1]
|
28
|
+
end
|
28
29
|
|
30
|
+
end
|
29
31
|
end
|