lite-ruby 1.0.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.
@@ -0,0 +1,86 @@
1
+ # Time
2
+
3
+ `format`
4
+ ------
5
+ Converts a `time` object to `strftime` it using a human readable string.
6
+
7
+ **Note:** The following values are to be used in combination with one's available in
8
+ [Date](https://github.com/drexed/lite-ruby/blob/master/docs/DATE.md#format) `format`.
9
+
10
+ | Directive | Type | Key | `strftime` | Result |
11
+ | --- | --- | --- | --- | --- |
12
+ | Hour | 24h zero-padded | `h`, `hour`, `hour_padded` | %H | (00..23) |
13
+ | Hour | 24h blank-padded | `hh`, `HOUR`, `hour_blank` | %k | ( 0..23) |
14
+ | Hour | 12h zero-padded | `hhh`, `hour_12`, `hour_12_padded` | %I | (01..12) |
15
+ | Hour | 12h blank-padded | `hhhh`, `HOUR_12`, `hour_12_blank` | %l | ( 1..12) |
16
+ | Minute | Minute | `n`, `minute` | %M | (00..59) |
17
+ | Second | Second | `s`, `second` | %S | (00..59) |
18
+ | Meridian | Lowercase | `ampm`, `meridian` | %P | am..pm |
19
+ | Meridian | Uppercase | `AMPM`, `MERIDIAN` | %p | AM..PM |
20
+ | Time zone | Zone | `z`, `time_zone` | %z | +0900 |
21
+ | Time zone | Offset | `zz`, `time_zone_offset` | %:z | +09:00 |
22
+ | Time zone | Full-offset | `zzz`, `time_zone_offset_full` | %::z | +09:00:00 |
23
+ | Time zone | Name | `zzzz`, `time_zone_name` | %Z | UTC |
24
+
25
+ ```ruby
26
+ Time.now.format('month_name day, year hour:minute ampm') #=> 'January 09, 2014 02:31 pm'
27
+ ```
28
+
29
+ `stamp` aka `to_format`
30
+ ------
31
+ Converts a `time` object to a predefined format.
32
+
33
+ **Note:** The following values are to be used in combination with one's available in
34
+ [Date](https://github.com/drexed/lite-ruby/blob/master/docs/DATE.md#stamp-aka-to_format) `stamp`.
35
+
36
+ #### Base
37
+
38
+ | Directive | Type | Key | `strftime` | Result |
39
+ | --- | --- | --- | --- | --- |
40
+ | Hour | 24h zero-padded | `:hour`, `:hour_padded` | %H | (00..23) |
41
+ | Hour | 24h blank-padded | `:hour_blank` | %k | ( 0..23) |
42
+ | Hour | 12h zero-padded | `hour_12`, `:hour_12_padded` | %I | (01..12) |
43
+ | Hour | 12h blank-padded | `:hour_12_blank` | %l | ( 1..12) |
44
+ | Minute | Minute | `:minute` | %M | (00..59) |
45
+ | Second | Second | `:second` | %S | (00..59) |
46
+ | Meridian | Lowercase | `:ampm` | %P | am..pm |
47
+ | Meridian | Uppercase | `:meridian` | %p | AM..PM |
48
+ | Time zone | Zone | `:time_zone` | %z | +0900 |
49
+ | Time zone | Offset | `:time_zone_offset` | %:z | +09:00 |
50
+ | Time zone | Full-offset | `:time_zone_offset_full` | %::z | +09:00:00 |
51
+ | Time zone | Name | `:time_zone_name` | %Z | UTC |
52
+
53
+ #### Combinations
54
+
55
+ | Directive | Type | Key | `strftime` | Result |
56
+ | --- | --- | --- | --- | --- |
57
+ | Combo | 24h time | `:time`, `:time_padded` | %H:%M | 00:31 |
58
+ | Combo | 24h time | `:time_blank` | %k:%M %z | 0:31 |
59
+ | Combo | 24h time | `:time_tz` | %H:%M %z | 00:31 +0000 |
60
+ | Combo | 24h time | `:time_tzn` | %H:%M %Z | 00:31 UTC |
61
+ | Combo | 12h time | `:time_12`, `:time_12_padded` | %I:%M %P | 07:31 |
62
+ | Combo | 12h time | `:time_12_blank` | %l:%M %P | 7:31 |
63
+ | Combo | 12h Time | `:time_12_tz` | %I:%M %z | 07:31 am +0000 |
64
+ | Combo | 12h Time | `:time_12_tzn` | %I:%M %Z | 07:31 am UTC |
65
+ | Combo | 24h daytime | `:daytime` | %B %-d %H:%M | January 9 00:31 |
66
+ | Combo | 24h daytime | `:daytime_abbr` | %b %-d %H:%M | Jan 9 00:31 |
67
+ | Combo | 24h daytime | `:daytime_iso` | %m-%d %H:%M | 01-09 00:31 |
68
+ | Combo | 12h daytime | `:daytime_12` | %B %-d %H:%M | January 9 12:31 am |
69
+ | Combo | 12h daytime | `:daytime_12_abbr` | %b %-d %H:%M | Jan 9 12:31 am |
70
+ | Combo | 12h daytime | `:daytime_12_iso` | %m-%d %H:%M | 01-09 12:31 am |
71
+ | Combo | 24h datetime | `:datetime` | %B %-d, %Y %H:%M | January 9, 2014 00:31 |
72
+ | Combo | 24h datetime | `:datetime_abbr` | %b %-d, %Y %H:%M | Jan 9, 2014 00:31 |
73
+ | Combo | 24h datetime | `:datetime_iso` | %Y-%m-%d %H:%M | 2014-01-09 00:31 |
74
+ | Combo | 24h datetime | `:datetime_tzn` | %B %-d, %Y %H:%M %Z | January 9, 2014 00:31 UTC |
75
+ | Combo | 24h datetime | `:datetime_abbr_tzn` | %b %-d, %Y %H:%M %Z | Jan 9, 2014 00:31 UTC |
76
+ | Combo | 24h datetime | `:datetime_iso_tzn` | %Y-%m-%d %H:%M %z | 2014-01-09 00:31 +0000 |
77
+ | Combo | 12h datetime | `:datetime_12` | %B %-d, %Y %H:%M | January 9, 2014 12:31 am |
78
+ | Combo | 12h datetime | `:datetime_12_abbr` | %b %-d, %Y %H:%M | Jan 9, 2014 12:31 am |
79
+ | Combo | 12h datetime | `:datetime_12_iso` | %Y-%m-%d %H:%M | 2014-01-09 12:31 am |
80
+ | Combo | 12h datetime | `:datetime_12_tzn` | %B %-d, %Y %H:%M %Z | January 9, 2014 12:31 am UTC |
81
+ | Combo | 12h datetime | `:datetime_12_abbr_tzn` | %b %-d, %Y %H:%M %Z | Jan 9, 2014 12:31 am UTC |
82
+ | Combo | 12h datetime | `:datetime_12_iso_tzn` | %Y-%m-%d %H:%M %z | 2014-01-09 12:31 am +0000 |
83
+
84
+ ```ruby
85
+ Time.now.stamp(:datetime) #=> 'January 09, 2014 02:31 pm'
86
+ ```
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails/generators'
4
+
5
+ module Lite
6
+ module Ruby
7
+ class InstallGenerator < Rails::Generators::Base
8
+
9
+ source_root File.expand_path('../templates', __FILE__)
10
+
11
+ def copy_initializer_file
12
+ copy_file('install.rb', 'config/initializers/lite-ruby.rb')
13
+ end
14
+
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ Lite::Ruby.configure do |config|
4
+ config.array = true
5
+ config.date = true
6
+ config.enumerable = true
7
+ config.hash = true
8
+ config.integer = true
9
+ config.kernel = true
10
+ config.numeric = true
11
+ config.object = true
12
+ config.range = true
13
+ config.string = true
14
+ config.time = true
15
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'lite/ruby/version'
4
+ require 'lite/ruby/configuration'
5
+
6
+ %w[date time].each do |filename|
7
+ require "lite/ruby/helpers/#{filename}_helper"
8
+ end
9
+
10
+ %w[array date enumerable hash integer kernel numeric object range string time].each do |filename|
11
+ next unless Lite::Ruby.configuration.send(filename)
12
+
13
+ require "lite/ruby/#{filename}"
14
+ end
15
+
16
+ require 'generators/lite/ruby/install_generator'
@@ -0,0 +1,290 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Array
4
+
5
+ def assert_valid_values!(*valid_values)
6
+ each do |value|
7
+ next if valid_values.include?(value)
8
+
9
+ raise ArgumentError,
10
+ "Invalid value: #{value.inspect}." \
11
+ "Allowed values are: #{valid_values.map(&:inspect).join(', ')}"
12
+ end
13
+ end
14
+
15
+ def assert_all_valid_values!(*valid_values)
16
+ return assert_valid_values!(*valid_values) unless empty?
17
+
18
+ raise ArgumentError, 'An empty array is not allowed'
19
+ end
20
+
21
+ def after(value)
22
+ return unless include?(value)
23
+
24
+ self[(index(value) + 1) % size]
25
+ end
26
+
27
+ def before(value)
28
+ return unless include?(value)
29
+
30
+ self[(index(value) - 1) % size]
31
+ end
32
+
33
+ # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
34
+ # rubocop:disable Metrics/PerceivedComplexity, Style/GuardClause, Style/IfInsideElse
35
+ def bury(*args)
36
+ if args.count < 2
37
+ raise ArgumentError, '2 or more arguments required'
38
+ elsif args.count == 2
39
+ if args[0].is_a?(Integer)
40
+ self[args[0]] = args[1]
41
+ else
42
+ self << { args[0] => args[1] }
43
+ end
44
+ else
45
+ if args[0].is_a?(Integer)
46
+ arg = args.shift
47
+ self[arg] = [] unless self[arg]
48
+ self[arg].bury(*args)
49
+ else
50
+ self << {}.bury(*args)
51
+ end
52
+ end
53
+
54
+ self
55
+ end
56
+ # rubocop:enable Metrics/PerceivedComplexity, Style/GuardClause, Style/IfInsideElse
57
+ # rubocop:enable Metrics/AbcSize, Metrics/MethodLength
58
+
59
+ def delete_first
60
+ self[1..-1]
61
+ end
62
+
63
+ def delete_first!
64
+ replace(delete_first)
65
+ end
66
+
67
+ def delete_last
68
+ self[0...-1]
69
+ end
70
+
71
+ def delete_last!
72
+ replace(delete_last)
73
+ end
74
+
75
+ def delete_values(*args)
76
+ args.each_with_object([]) { |val, array| array << delete(val) }
77
+ end
78
+
79
+ def demote(value)
80
+ sort_by { |val| val == value ? 0 : -1 }
81
+ end
82
+
83
+ def demote!(value)
84
+ replace(demote(value))
85
+ end
86
+
87
+ def denillify(identity = 0)
88
+ map { |val| val.nil? ? identity : val }
89
+ end
90
+
91
+ def denillify!(identity = 0)
92
+ replace(denillify(identity))
93
+ end
94
+
95
+ def duplicates(minimum = 2)
96
+ hash = Hash.new(0)
97
+ each { |val| hash[val] += 1 }
98
+ hash.delete_if { |_, val| val < minimum }.keys
99
+ end
100
+
101
+ def from(position)
102
+ self[position, size] || []
103
+ end
104
+
105
+ def fulfill(value, amount)
106
+ return self if amount <= size
107
+
108
+ fill(value, size..(amount - 1))
109
+ end
110
+
111
+ def groups(number)
112
+ return [] if number <= 0
113
+
114
+ num, rem = size.divmod(number)
115
+ collection = (0..(num - 1)).collect { |val| self[(val * number), number] }
116
+ return collection unless rem.positive?
117
+
118
+ collection << self[-rem, rem]
119
+ end
120
+
121
+ # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength
122
+ def in_groups(number, fill_with = nil)
123
+ collection_size = size
124
+ division = collection_size.div(number)
125
+ modulo = collection_size % number
126
+
127
+ collection = []
128
+ start = 0
129
+ number.times do |int|
130
+ mod_gt_zero = modulo.positive?
131
+ grouping = division + (mod_gt_zero && modulo > int ? 1 : 0)
132
+ collection << last_group = slice(start, grouping)
133
+ last_group << fill_with if (fill_with != false) && mod_gt_zero && (grouping == division)
134
+ start += grouping
135
+ end
136
+
137
+ return collection unless block_given?
138
+
139
+ collection.each { |val| yield(val) }
140
+ end
141
+ # rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength
142
+
143
+ # rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Style/GuardClause
144
+ def in_groups_of(number, fill_with = nil)
145
+ if number.to_i <= 0
146
+ raise ArgumentError, "Group size must be a positive integer, was #{number.inspect}"
147
+ elsif fill_with == false
148
+ collection = self
149
+ else
150
+ padding = (number - size % number) % number
151
+ collection = dup.concat(Array.new(padding, fill_with))
152
+ end
153
+
154
+ sliced_collection = collection.each_slice(number)
155
+ return sliced_collection.to_a unless block_given?
156
+
157
+ sliced_collection { |val| yield(val) }
158
+ end
159
+ # rubocop:enable Metrics/AbcSize, Metrics/MethodLength, Style/GuardClause
160
+
161
+ def indexes(value)
162
+ array = []
163
+ each_with_index { |val, i| array << i if value == val }
164
+ array
165
+ end
166
+
167
+ alias indices indexes
168
+
169
+ def merge(*values)
170
+ dup.merge!(*values)
171
+ end
172
+
173
+ def merge!(*values)
174
+ values.each_with_object(self) { |val, arr| arr.concat(val) }
175
+ end
176
+
177
+ def nillify
178
+ map { |val| !val.nil? && (val.try(:blank?) || val.try(:to_s).blank?) ? nil : val }
179
+ end
180
+
181
+ def nillify!
182
+ replace(nillify)
183
+ end
184
+
185
+ def position(value)
186
+ idx = index(value)
187
+ return idx if idx.nil?
188
+
189
+ idx + 1
190
+ end
191
+
192
+ def positions(value)
193
+ indexes(value).map { |val| val + 1 }
194
+ end
195
+
196
+ def probability
197
+ hash = Hash.new(0.0)
198
+ differ = 0.0
199
+
200
+ each do |val|
201
+ hash[val] += 1.0
202
+ differ += 1.0
203
+ end
204
+
205
+ hash.each_key { |val| hash[val] /= differ }
206
+ hash
207
+ end
208
+
209
+ def promote(value)
210
+ sort_by { |val| val == value ? -1 : 0 }
211
+ end
212
+
213
+ def promote!(value)
214
+ replace(promote(value))
215
+ end
216
+
217
+ def reject_values(*args)
218
+ reject { |val| args.include?(val) }
219
+ end
220
+
221
+ def rposition(value)
222
+ idx = rindex(value)
223
+ return idx if idx.nil?
224
+
225
+ idx + 1
226
+ end
227
+
228
+ def sample!
229
+ delete_at(Random.rand(size - 1))
230
+ end
231
+
232
+ # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
233
+ def split(number = nil)
234
+ array = [[]]
235
+
236
+ if block_given?
237
+ each { |val| yield(val) ? (array << []) : (array .last << val) }
238
+ else
239
+ dup_arr = dup
240
+
241
+ until dup_arr.empty?
242
+ if (idx = dup_arr.index(number))
243
+ array.last << dup_arr.shift(idx)
244
+ dup_arr.shift
245
+ array << []
246
+ else
247
+ array.last << arr.shift(dup_arr.size)
248
+ end
249
+ end
250
+ end
251
+
252
+ array
253
+ end
254
+ # rubocop:enable Metrics/AbcSize, Metrics/MethodLength
255
+
256
+ def strip
257
+ reject(&:blank?)
258
+ end
259
+
260
+ def strip!
261
+ replace(strip)
262
+ end
263
+
264
+ def swap(from, to)
265
+ self[from], self[to] = self[to], self[from]
266
+ self
267
+ end
268
+
269
+ def to(position)
270
+ return first(position + 1) if position >= 0
271
+
272
+ self[0..position]
273
+ end
274
+
275
+ # rubocop:disable Metrics/CyclomaticComplexity
276
+ def to_sentence(options = {})
277
+ words_connector = options[:words_connector] || ', '
278
+ two_words_connector = options[:two_words_connector] || ' and '
279
+ last_word_connector = options[:last_word_connector] || ', and '
280
+
281
+ case size
282
+ when 0 then ''
283
+ when 1 then self[0].to_s.dup
284
+ when 2 then "#{self[0]}#{two_words_connector}#{self[1]}"
285
+ else "#{self[0...-1].join(words_connector)}#{last_word_connector}#{self[-1]}"
286
+ end
287
+ end
288
+ # rubocop:enable Metrics/CyclomaticComplexity
289
+
290
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lite
4
+ module Ruby
5
+
6
+ class Configuration
7
+
8
+ attr_accessor :array, :date, :enumerable, :hash, :integer, :kernel, :numeric, :object, :range,
9
+ :string, :time
10
+
11
+ # rubocop:disable Metrics/MethodLength
12
+ def initialize
13
+ @array = true
14
+ @date = true
15
+ @enumerable = true
16
+ @hash = true
17
+ @integer = true
18
+ @kernel = true
19
+ @numeric = true
20
+ @object = true
21
+ @range = true
22
+ @string = true
23
+ @time = true
24
+ end
25
+ # rubocop:enable Metrics/MethodLength
26
+
27
+ end
28
+
29
+ def self.configuration
30
+ @configuration ||= Configuration.new
31
+ end
32
+
33
+ def self.configuration=(config)
34
+ @configuration = config
35
+ end
36
+
37
+ def self.configure
38
+ yield(configuration)
39
+ end
40
+
41
+ end
42
+ end