lite-ruby 1.3.1 → 2.0.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/CHANGELOG.md +21 -1
- data/Gemfile.lock +1 -1
- data/README.md +26 -6
- data/docs/HASH.md +13 -0
- data/lib/generators/lite/ruby/install_generator.rb +0 -2
- data/lib/generators/lite/ruby/templates/install.rb +18 -6
- data/lib/lite/ruby.rb +2 -15
- data/lib/lite/ruby/all.rb +16 -0
- data/lib/lite/ruby/array.rb +218 -220
- data/lib/lite/ruby/boolean.rb +17 -19
- data/lib/lite/ruby/date.rb +13 -20
- data/lib/lite/ruby/enumerable.rb +153 -155
- data/lib/lite/ruby/formats/date_stamps.yml +34 -0
- data/lib/lite/ruby/formats/date_units.yml +42 -0
- data/lib/lite/ruby/formats/integer_roman_numerals.yml +13 -0
- data/lib/lite/ruby/formats/string_transliterations.yml +189 -0
- data/lib/lite/ruby/formats/time_stamps.yml +42 -0
- data/lib/lite/ruby/formats/time_units.yml +28 -0
- data/lib/lite/ruby/hash.rb +313 -301
- data/lib/lite/ruby/helpers/date_time_helper.rb +24 -0
- data/lib/lite/ruby/integer.rb +62 -63
- data/lib/lite/ruby/kernel.rb +19 -21
- data/lib/lite/ruby/numeric.rb +175 -177
- data/lib/lite/ruby/object.rb +128 -130
- data/lib/lite/ruby/open_struct.rb +17 -19
- data/lib/lite/ruby/range.rb +19 -21
- data/lib/lite/ruby/safe/object.rb +10 -8
- data/lib/lite/ruby/safe/string.rb +8 -7
- data/lib/lite/ruby/string.rb +348 -384
- data/lib/lite/ruby/struct.rb +7 -9
- data/lib/lite/ruby/time.rb +25 -30
- data/lib/lite/ruby/version.rb +1 -1
- metadata +10 -5
- data/lib/lite/ruby/configuration.rb +0 -38
- data/lib/lite/ruby/helpers/date_helper.rb +0 -107
- data/lib/lite/ruby/helpers/time_helper.rb +0 -86
data/lib/lite/ruby/boolean.rb
CHANGED
@@ -1,31 +1,29 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
class FalseClass
|
3
|
+
class FalseClass
|
5
4
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
def to_i
|
11
|
-
0
|
12
|
-
end
|
13
|
-
|
14
|
-
alias to_b to_bool
|
5
|
+
def to_bool
|
6
|
+
self
|
7
|
+
end
|
15
8
|
|
9
|
+
def to_i
|
10
|
+
0
|
16
11
|
end
|
17
12
|
|
18
|
-
|
13
|
+
alias to_b to_bool
|
19
14
|
|
20
|
-
|
21
|
-
self
|
22
|
-
end
|
15
|
+
end
|
23
16
|
|
24
|
-
|
25
|
-
1
|
26
|
-
end
|
17
|
+
class TrueClass
|
27
18
|
|
28
|
-
|
19
|
+
def to_bool
|
20
|
+
self
|
21
|
+
end
|
29
22
|
|
23
|
+
def to_i
|
24
|
+
1
|
30
25
|
end
|
26
|
+
|
27
|
+
alias to_b to_bool
|
28
|
+
|
31
29
|
end
|
data/lib/lite/ruby/date.rb
CHANGED
@@ -1,29 +1,22 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
|
3
|
+
require 'date' unless defined?(Date)
|
4
|
+
require 'yaml' unless defined?(YAML)
|
5
5
|
|
6
|
-
|
6
|
+
require 'lite/ruby/helpers/date_time_helper' unless defined?(Lite::Ruby::DateTimeHelper)
|
7
7
|
|
8
|
-
|
8
|
+
class Date
|
9
9
|
|
10
|
-
|
10
|
+
include Lite::Ruby::DateTimeHelper
|
11
11
|
|
12
|
-
|
13
|
-
|
14
|
-
end
|
12
|
+
DEFAULT_STAMP = 'date_iso'
|
13
|
+
DEFAULT_UNIT = 'year-month-day'
|
15
14
|
|
16
|
-
|
17
|
-
|
18
|
-
|
15
|
+
STAMPS = YAML.load_file(
|
16
|
+
File.expand_path('formats/date_stamps.yml', File.dirname(__FILE__))
|
17
|
+
).freeze
|
18
|
+
UNITS = YAML.load_file(
|
19
|
+
File.expand_path('formats/date_units.yml', File.dirname(__FILE__))
|
20
|
+
).freeze
|
19
21
|
|
20
|
-
def format_for(key)
|
21
|
-
DATE_UNITS[key]
|
22
|
-
end
|
23
|
-
|
24
|
-
def stamp_for(key)
|
25
|
-
DATE_STAMPS[key]
|
26
|
-
end
|
27
|
-
|
28
|
-
end
|
29
22
|
end
|
data/lib/lite/ruby/enumerable.rb
CHANGED
@@ -1,212 +1,210 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
module Enumerable
|
3
|
+
module Enumerable
|
5
4
|
|
6
|
-
|
7
|
-
|
8
|
-
|
5
|
+
def cluster
|
6
|
+
each_with_object([]) do |ele, results|
|
7
|
+
last_res = results.last
|
9
8
|
|
10
|
-
|
11
|
-
|
12
|
-
else
|
13
|
-
results << [ele]
|
14
|
-
end
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
def cluster_by(&block)
|
19
|
-
group_by(&block).sort.transpose.pop || []
|
20
|
-
end
|
21
|
-
|
22
|
-
def deduce(identity = 0, &block)
|
23
|
-
if defined?(yield)
|
24
|
-
map(&block).deduce(identity)
|
9
|
+
if last_res && (yield(ele) == yield(last_res.last))
|
10
|
+
last_res << ele
|
25
11
|
else
|
26
|
-
|
12
|
+
results << [ele]
|
27
13
|
end
|
28
14
|
end
|
15
|
+
end
|
29
16
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
self[0...(collection_size - num)]
|
35
|
-
end
|
36
|
-
|
37
|
-
def drop_last_if
|
38
|
-
dropping = true
|
39
|
-
reverse_each.with_object([]) do |val, arr|
|
40
|
-
next if dropping &&= yield(val)
|
17
|
+
def cluster_by(&block)
|
18
|
+
group_by(&block).sort.transpose.pop || []
|
19
|
+
end
|
41
20
|
|
42
|
-
|
43
|
-
|
21
|
+
def deduce(identity = 0, &block)
|
22
|
+
if defined?(yield)
|
23
|
+
map(&block).deduce(identity)
|
24
|
+
else
|
25
|
+
inject { |acc, val| acc - val } || identity
|
44
26
|
end
|
27
|
+
end
|
45
28
|
|
46
|
-
|
47
|
-
|
29
|
+
def drop_last(num)
|
30
|
+
collection_size = to_a.size
|
31
|
+
return self if num > collection_size
|
48
32
|
|
49
|
-
|
50
|
-
|
51
|
-
else
|
52
|
-
each { |opt| found_count += 1 if opt }
|
53
|
-
end
|
33
|
+
self[0...(collection_size - num)]
|
34
|
+
end
|
54
35
|
|
55
|
-
|
56
|
-
|
36
|
+
def drop_last_if
|
37
|
+
dropping = true
|
38
|
+
reverse_each.with_object([]) do |val, arr|
|
39
|
+
next if dropping &&= yield(val)
|
57
40
|
|
58
|
-
|
59
|
-
none?(object)
|
41
|
+
arr.unshift(val)
|
60
42
|
end
|
43
|
+
end
|
61
44
|
|
62
|
-
|
63
|
-
|
64
|
-
end
|
45
|
+
def exactly?(num)
|
46
|
+
found_count = 0
|
65
47
|
|
66
|
-
|
67
|
-
if
|
68
|
-
|
69
|
-
|
70
|
-
inject { |acc, val| acc**val } || identity
|
71
|
-
end
|
48
|
+
if defined?(yield)
|
49
|
+
each { |*opt| found_count += 1 if yield(*opt) }
|
50
|
+
else
|
51
|
+
each { |opt| found_count += 1 if opt }
|
72
52
|
end
|
73
53
|
|
74
|
-
|
75
|
-
|
76
|
-
end
|
54
|
+
found_count > num ? false : num == found_count
|
55
|
+
end
|
77
56
|
|
78
|
-
|
79
|
-
|
80
|
-
|
57
|
+
def excase?(object)
|
58
|
+
none?(object)
|
59
|
+
end
|
81
60
|
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
items = each
|
86
|
-
|
87
|
-
loop do
|
88
|
-
begin
|
89
|
-
val << items.next
|
90
|
-
rescue StopIteration
|
91
|
-
break
|
92
|
-
end
|
93
|
-
|
94
|
-
begin
|
95
|
-
items.peek
|
96
|
-
rescue StopIteration
|
97
|
-
break
|
98
|
-
else
|
99
|
-
val << sep
|
100
|
-
end
|
101
|
-
end
|
102
|
-
end
|
61
|
+
def expand
|
62
|
+
map { |val| val.is_a?(Enumerable) ? val.expand : val }
|
63
|
+
end
|
103
64
|
|
104
|
-
|
65
|
+
def exponential(identity = 0, &block)
|
66
|
+
if defined?(yield)
|
67
|
+
map(&block).exponential(identity)
|
68
|
+
else
|
69
|
+
inject { |acc, val| acc**val } || identity
|
105
70
|
end
|
106
|
-
|
71
|
+
end
|
107
72
|
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
elsif size % modulo != 0
|
112
|
-
raise ArgumentError, "Invalid modulo: #{modulo.inspect}"
|
113
|
-
else
|
114
|
-
(0...size).each_with_object(Array.new(modulo, [])) do |i, array|
|
115
|
-
array[i % modulo] += [self[i]]
|
116
|
-
end
|
117
|
-
end
|
118
|
-
end
|
73
|
+
def frequency
|
74
|
+
each_with_object(Hash.new(0)) { |val, hash| hash[val] += 1 }
|
75
|
+
end
|
119
76
|
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
result = Hash.new { |hash, key| hash[key] = [] }
|
77
|
+
def incase?(object)
|
78
|
+
any?(object)
|
79
|
+
end
|
124
80
|
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
81
|
+
# rubocop:disable Metrics/MethodLength
|
82
|
+
def interpose(sep, &block)
|
83
|
+
enum = Enumerator.new do |val|
|
84
|
+
items = each
|
129
85
|
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
86
|
+
loop do
|
87
|
+
begin
|
88
|
+
val << items.next
|
89
|
+
rescue StopIteration
|
90
|
+
break
|
91
|
+
end
|
134
92
|
|
135
|
-
|
136
|
-
|
93
|
+
begin
|
94
|
+
items.peek
|
95
|
+
rescue StopIteration
|
96
|
+
break
|
137
97
|
else
|
138
|
-
|
98
|
+
val << sep
|
139
99
|
end
|
140
100
|
end
|
141
|
-
|
142
|
-
result.values.flatten.uniq
|
143
101
|
end
|
144
|
-
# rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity
|
145
|
-
# rubocop:enable Metrics/MethodLength, Metrics/PerceivedComplexity
|
146
102
|
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
103
|
+
block ? enum.each(&block) : enum
|
104
|
+
end
|
105
|
+
# rubocop:enable Metrics/MethodLength
|
106
|
+
|
107
|
+
def modulate(modulo)
|
108
|
+
if modulo == 1
|
109
|
+
to_a
|
110
|
+
elsif size % modulo != 0
|
111
|
+
raise ArgumentError, "Invalid modulo: #{modulo.inspect}"
|
112
|
+
else
|
113
|
+
(0...size).each_with_object(Array.new(modulo, [])) do |i, array|
|
114
|
+
array[i % modulo] += [self[i]]
|
152
115
|
end
|
153
116
|
end
|
117
|
+
end
|
154
118
|
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
119
|
+
# rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity
|
120
|
+
# rubocop:disable Metrics/MethodLength, Metrics/PerceivedComplexity
|
121
|
+
def occur(amount = nil)
|
122
|
+
result = Hash.new { |hash, key| hash[key] = [] }
|
123
|
+
|
124
|
+
each do |item|
|
125
|
+
key = item
|
126
|
+
result[key] << item
|
161
127
|
end
|
162
128
|
|
163
|
-
|
164
|
-
|
129
|
+
if defined?(yield)
|
130
|
+
result.select! { |_key, values| yield(values.size) }
|
131
|
+
else
|
132
|
+
raise ArgumentError, 'Invalid occur amount' unless amount
|
165
133
|
|
166
|
-
if
|
167
|
-
|
134
|
+
if amount.is_a?(Range)
|
135
|
+
result.select! { |_key, values| amount.include?(values.size) }
|
168
136
|
else
|
169
|
-
|
137
|
+
result.select! { |_key, values| values.size == amount }
|
170
138
|
end
|
139
|
+
end
|
140
|
+
|
141
|
+
result.values.flatten.uniq
|
142
|
+
end
|
143
|
+
# rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity
|
144
|
+
# rubocop:enable Metrics/MethodLength, Metrics/PerceivedComplexity
|
171
145
|
|
172
|
-
|
146
|
+
def produce(identity = 0, &block)
|
147
|
+
if defined?(yield)
|
148
|
+
map(&block).produce(identity)
|
149
|
+
else
|
150
|
+
inject { |acc, val| acc * val } || identity
|
173
151
|
end
|
152
|
+
end
|
174
153
|
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
each_with_object([]) do |val, array|
|
181
|
-
if !limited_to.empty? && !limited_to.include?(val)
|
182
|
-
array << val
|
183
|
-
elsif first || current != val
|
184
|
-
array << val
|
185
|
-
first = false
|
186
|
-
current = val
|
187
|
-
end
|
188
|
-
end
|
154
|
+
def quotient(identity = 0, &block)
|
155
|
+
if defined?(yield)
|
156
|
+
map(&block).quotient(identity)
|
157
|
+
else
|
158
|
+
inject { |acc, val| acc / val } || identity
|
189
159
|
end
|
190
|
-
|
160
|
+
end
|
191
161
|
|
192
|
-
|
193
|
-
|
194
|
-
return self if num > collection_size
|
162
|
+
def several?
|
163
|
+
found_count = 0
|
195
164
|
|
196
|
-
|
165
|
+
if defined?(yield)
|
166
|
+
each { |*opt| found_count += 1 if yield(*opt) }
|
167
|
+
else
|
168
|
+
each { |opt| found_count += 1 if opt }
|
197
169
|
end
|
198
170
|
|
199
|
-
|
200
|
-
|
201
|
-
break arr unless yield(val)
|
171
|
+
found_count > 1
|
172
|
+
end
|
202
173
|
|
203
|
-
|
174
|
+
# rubocop:disable Metrics/MethodLength
|
175
|
+
def squeeze(*limited_to)
|
176
|
+
first = true
|
177
|
+
current = nil
|
178
|
+
|
179
|
+
each_with_object([]) do |val, array|
|
180
|
+
if !limited_to.empty? && !limited_to.include?(val)
|
181
|
+
array << val
|
182
|
+
elsif first || current != val
|
183
|
+
array << val
|
184
|
+
first = false
|
185
|
+
current = val
|
204
186
|
end
|
205
187
|
end
|
188
|
+
end
|
189
|
+
# rubocop:enable Metrics/MethodLength
|
206
190
|
|
207
|
-
|
191
|
+
def take_last(num)
|
192
|
+
collection_size = to_a.size
|
193
|
+
return self if num > collection_size
|
208
194
|
|
195
|
+
self[(collection_size - num)..-1]
|
209
196
|
end
|
210
197
|
|
211
|
-
|
198
|
+
def take_last_if
|
199
|
+
reverse_each.with_object([]) do |val, arr|
|
200
|
+
break arr unless yield(val)
|
201
|
+
|
202
|
+
arr.unshift(val)
|
203
|
+
end
|
204
|
+
end
|
205
|
+
|
206
|
+
alias occurrences frequency
|
207
|
+
|
212
208
|
end
|
209
|
+
|
210
|
+
require 'lite/ruby/safe/enumerable' unless defined?(ActiveSupport)
|