lite-ruby 1.3.3 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,31 +1,29 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- if Lite::Ruby.configuration.monkey_patches.include?('boolean')
4
- class FalseClass
3
+ class FalseClass
5
4
 
6
- def to_bool
7
- self
8
- end
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
- class TrueClass
13
+ alias to_b to_bool
19
14
 
20
- def to_bool
21
- self
22
- end
15
+ end
23
16
 
24
- def to_i
25
- 1
26
- end
17
+ class TrueClass
27
18
 
28
- alias to_b to_bool
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
@@ -1,29 +1,22 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- if Lite::Ruby.configuration.monkey_patches.include?('date')
4
- require 'date'
3
+ require 'date' unless defined?(Date)
4
+ require 'yaml' unless defined?(YAML)
5
5
 
6
- class Date
6
+ require 'lite/ruby/helpers/date_time_helper' unless defined?(Lite::Ruby::DateTimeHelper)
7
7
 
8
- include Lite::Ruby::DateHelper
8
+ class Date
9
9
 
10
- private
10
+ include Lite::Ruby::DateTimeHelper
11
11
 
12
- def default_format
13
- 'year-month-day'
14
- end
12
+ DEFAULT_STAMP = 'date_iso'
13
+ DEFAULT_UNIT = 'year-month-day'
15
14
 
16
- def default_stamp
17
- :date_iso
18
- end
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
@@ -1,212 +1,210 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- if Lite::Ruby.configuration.monkey_patches.include?('enumerable')
4
- module Enumerable
3
+ module Enumerable
5
4
 
6
- def cluster
7
- each_with_object([]) do |ele, results|
8
- last_res = results.last
5
+ def cluster
6
+ each_with_object([]) do |ele, results|
7
+ last_res = results.last
9
8
 
10
- if last_res && (yield(ele) == yield(last_res.last))
11
- last_res << ele
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
- inject { |acc, val| acc - val } || identity
12
+ results << [ele]
27
13
  end
28
14
  end
15
+ end
29
16
 
30
- def drop_last(num)
31
- collection_size = to_a.size
32
- return self if num > collection_size
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
- arr.unshift(val)
43
- end
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
- def exactly?(num)
47
- found_count = 0
29
+ def drop_last(num)
30
+ collection_size = to_a.size
31
+ return self if num > collection_size
48
32
 
49
- if defined?(yield)
50
- each { |*opt| found_count += 1 if yield(*opt) }
51
- else
52
- each { |opt| found_count += 1 if opt }
53
- end
33
+ self[0...(collection_size - num)]
34
+ end
54
35
 
55
- found_count > num ? false : num == found_count
56
- end
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
- def excase?(object)
59
- none?(object)
41
+ arr.unshift(val)
60
42
  end
43
+ end
61
44
 
62
- def expand
63
- map { |val| val.is_a?(Enumerable) ? val.expand : val }
64
- end
45
+ def exactly?(num)
46
+ found_count = 0
65
47
 
66
- def exponential(identity = 0, &block)
67
- if defined?(yield)
68
- map(&block).exponential(identity)
69
- else
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
- def frequency
75
- each_with_object(Hash.new(0)) { |val, hash| hash[val] += 1 }
76
- end
54
+ found_count > num ? false : num == found_count
55
+ end
77
56
 
78
- def incase?(object)
79
- any?(object)
80
- end
57
+ def excase?(object)
58
+ none?(object)
59
+ end
81
60
 
82
- # rubocop:disable Metrics/MethodLength
83
- def interpose(sep, &block)
84
- enum = Enumerator.new do |val|
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
- block ? enum.each(&block) : enum
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
- # rubocop:enable Metrics/MethodLength
71
+ end
107
72
 
108
- def modulate(modulo)
109
- if modulo == 1
110
- to_a
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
- # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity
121
- # rubocop:disable Metrics/MethodLength, Metrics/PerceivedComplexity
122
- def occur(amount = nil)
123
- result = Hash.new { |hash, key| hash[key] = [] }
77
+ def incase?(object)
78
+ any?(object)
79
+ end
124
80
 
125
- each do |item|
126
- key = item
127
- result[key] << item
128
- end
81
+ # rubocop:disable Metrics/MethodLength
82
+ def interpose(sep, &block)
83
+ enum = Enumerator.new do |val|
84
+ items = each
129
85
 
130
- if defined?(yield)
131
- result.select! { |_key, values| yield(values.size) }
132
- else
133
- raise ArgumentError, 'Invalid occur amount' unless amount
86
+ loop do
87
+ begin
88
+ val << items.next
89
+ rescue StopIteration
90
+ break
91
+ end
134
92
 
135
- if amount.is_a?(Range)
136
- result.select! { |_key, values| amount.include?(values.size) }
93
+ begin
94
+ items.peek
95
+ rescue StopIteration
96
+ break
137
97
  else
138
- result.select! { |_key, values| values.size == amount }
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
- def produce(identity = 0, &block)
148
- if defined?(yield)
149
- map(&block).produce(identity)
150
- else
151
- inject { |acc, val| acc * val } || identity
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
- def quotient(identity = 0, &block)
156
- if defined?(yield)
157
- map(&block).quotient(identity)
158
- else
159
- inject { |acc, val| acc / val } || identity
160
- end
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
- def several?
164
- found_count = 0
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 defined?(yield)
167
- each { |*opt| found_count += 1 if yield(*opt) }
134
+ if amount.is_a?(Range)
135
+ result.select! { |_key, values| amount.include?(values.size) }
168
136
  else
169
- each { |opt| found_count += 1 if opt }
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
- found_count > 1
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
- # rubocop:disable Metrics/MethodLength
176
- def squeeze(*limited_to)
177
- first = true
178
- current = nil
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
- # rubocop:enable Metrics/MethodLength
160
+ end
191
161
 
192
- def take_last(num)
193
- collection_size = to_a.size
194
- return self if num > collection_size
162
+ def several?
163
+ found_count = 0
195
164
 
196
- self[(collection_size - num)..-1]
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
- def take_last_if
200
- reverse_each.with_object([]) do |val, arr|
201
- break arr unless yield(val)
171
+ found_count > 1
172
+ end
202
173
 
203
- arr.unshift(val)
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
- alias occurrences frequency
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
- require 'lite/ruby/safe/enumerable' unless defined?(ActiveSupport)
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)