lite-ruby 1.0.31 → 1.1.4

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.
@@ -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[
@@ -10,16 +12,13 @@ if Lite::Ruby.configuration.monkey_patches.include?('object')
10
12
  1 t true y yes on
11
13
  ].freeze
12
14
 
13
- def array?
14
- is_a?(Array)
15
- end
16
-
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
15
+ # NOTE: There is a class between the PG gem and the `array?` method.
16
+ # We only need to skip this on migrations since that action
17
+ # happens on a seperate runtime.
18
+ unless defined?(PG) && ARGV.first.to_s.start_with?('db:')
19
+ def array?
20
+ is_a?(Array)
21
+ end
23
22
  end
24
23
 
25
24
  def bool?
@@ -35,14 +34,6 @@ if Lite::Ruby.configuration.monkey_patches.include?('object')
35
34
  is_a?(Date)
36
35
  end
37
36
 
38
- def deep_dup
39
- duplicable? ? dup : self
40
- end
41
-
42
- def duplicable?
43
- true
44
- end
45
-
46
37
  # rubocop:disable Style/YodaCondition
47
38
  def false?
48
39
  false == self
@@ -87,14 +78,6 @@ if Lite::Ruby.configuration.monkey_patches.include?('object')
87
78
  to_s == to_s.reverse
88
79
  end
89
80
 
90
- def present?
91
- !blank?
92
- end
93
-
94
- def presence
95
- self if present?
96
- end
97
-
98
81
  def range?
99
82
  is_a?(Range)
100
83
  end
@@ -160,8 +143,6 @@ if Lite::Ruby.configuration.monkey_patches.include?('object')
160
143
  nil
161
144
  end
162
145
 
163
- alias to_b to_bool
164
-
165
146
  # rubocop:disable Style/YodaCondition
166
147
  def true?
167
148
  true == self
@@ -172,18 +153,6 @@ if Lite::Ruby.configuration.monkey_patches.include?('object')
172
153
  TRUE_VALUES.include?(to_s.downcase)
173
154
  end
174
155
 
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? && defined?(yield)
181
- block.arity.zero? ? instance_eval(&block) : yield(self)
182
- else
183
- public_send(*obj, &block)
184
- end
185
- end
186
-
187
156
  def try_call(*keys)
188
157
  return unless respond_to?(:call)
189
158
 
@@ -196,5 +165,7 @@ if Lite::Ruby.configuration.monkey_patches.include?('object')
196
165
  nil
197
166
  end
198
167
 
168
+ alias to_b to_bool
169
+
199
170
  end
200
171
  end
@@ -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
@@ -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,101 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Array
4
+
5
+ def deep_dup
6
+ map(&:deep_dup)
7
+ end
8
+
9
+ def extract!
10
+ return to_enum(:extract!) { size } unless block_given?
11
+
12
+ extracted_elements = []
13
+ reject! { |element| extracted_elements << element if yield(element) }
14
+ extracted_elements
15
+ end
16
+
17
+ def from(position)
18
+ self[position, size] || []
19
+ end
20
+
21
+ # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength
22
+ def in_groups(number, fill_with = nil, &block)
23
+ collection_size = size
24
+ division = collection_size.div(number)
25
+ modulo = collection_size % number
26
+ collection = []
27
+ start = 0
28
+
29
+ number.times do |int|
30
+ mod_gt_zero = modulo.positive?
31
+ grouping = division + (mod_gt_zero && modulo > int ? 1 : 0)
32
+ collection << last_group = slice(start, grouping)
33
+ last_group << fill_with if (fill_with != false) && mod_gt_zero && (grouping == division)
34
+ start += grouping
35
+ end
36
+
37
+ return collection unless defined?(yield)
38
+
39
+ collection.each(&block)
40
+ end
41
+ # rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength
42
+
43
+ # rubocop:disable Metrics/MethodLength, Style/GuardClause
44
+ def in_groups_of(number, fill_with = nil, &block)
45
+ if number.to_i <= 0
46
+ raise ArgumentError, "Group size must be a positive integer, was #{number.inspect}"
47
+ elsif fill_with == false
48
+ collection = self
49
+ else
50
+ padding = (number - size % number) % number
51
+ collection = dup.concat(Array.new(padding, fill_with))
52
+ end
53
+
54
+ sliced_collection = collection.each_slice(number)
55
+ return sliced_collection.to_a unless defined?(yield)
56
+
57
+ sliced_collection(&block)
58
+ end
59
+ # rubocop:enable Metrics/MethodLength, Style/GuardClause
60
+
61
+ # rubocop:disable Metrics/MethodLength, Style/ExplicitBlockArgument
62
+ def split(value = nil)
63
+ arr = dup
64
+ result = []
65
+
66
+ if block_given?
67
+ while (idx = arr.index { |i| yield(i) })
68
+ result << arr.shift(idx)
69
+ arr.shift
70
+ end
71
+ else
72
+ while (idx = arr.index(value))
73
+ result << arr.shift(idx)
74
+ arr.shift
75
+ end
76
+ end
77
+
78
+ result << arr
79
+ end
80
+ # rubocop:enable Metrics/MethodLength, Style/ExplicitBlockArgument
81
+
82
+ def to(position)
83
+ return first(position + 1) if position >= 0
84
+
85
+ self[0..position]
86
+ end
87
+
88
+ def to_sentence(options = {})
89
+ words_connector = options[:words_connector] || ', '
90
+ two_words_connector = options[:two_words_connector] || ' and '
91
+ last_word_connector = options[:last_word_connector] || ', and '
92
+
93
+ case size
94
+ when 0 then ''
95
+ when 1 then self[0].to_s
96
+ when 2 then "#{self[0]}#{two_words_connector}#{self[1]}"
97
+ else "#{self[0...-1].join(words_connector)}#{last_word_connector}#{self[-1]}"
98
+ end
99
+ end
100
+
101
+ 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
@@ -0,0 +1,83 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Hash
4
+
5
+ # rubocop:disable Style/CaseEquality
6
+ def deep_dup
7
+ hash = dup
8
+
9
+ each_pair do |key, value|
10
+ if key.frozen? && ::String === key
11
+ hash[key] = value.deep_dup
12
+ else
13
+ hash.delete(key)
14
+ hash[key.deep_dup] = value.deep_dup
15
+ end
16
+ end
17
+
18
+ hash
19
+ end
20
+ # rubocop:enable Style/CaseEquality
21
+
22
+ def deep_merge(other_hash, &block)
23
+ dup.deep_merge!(other_hash, &block)
24
+ end
25
+
26
+ def deep_merge!(other_hash, &block)
27
+ merge!(other_hash) do |key, this_val, other_val|
28
+ if this_val.is_a?(Hash) && other_val.is_a?(Hash)
29
+ this_val.deep_merge(other_val, &block)
30
+ elsif defined?(yield)
31
+ yield(key, this_val, other_val)
32
+ else
33
+ other_val
34
+ end
35
+ end
36
+ end
37
+
38
+ def except(*keys)
39
+ slice(*self.keys - keys)
40
+ end
41
+
42
+ def except!(*keys)
43
+ keys.each { |key| delete(key) }
44
+ self
45
+ end
46
+
47
+ def extract!(*keys)
48
+ keys.each_with_object({}) { |key, hash| hash[key] = delete(key) if key?(key) }
49
+ end
50
+
51
+ def reverse_merge(other_hash)
52
+ other_hash.merge(self)
53
+ end
54
+
55
+ def reverse_merge!(other_hash)
56
+ other_hash.merge!(self)
57
+ end
58
+
59
+ def stringify_keys
60
+ transform_keys(&:to_s)
61
+ end
62
+
63
+ def stringify_keys!
64
+ transform_keys!(&:to_s)
65
+ end
66
+
67
+ def symbolize_keys
68
+ transform_keys do |key|
69
+ key.to_sym
70
+ rescue StandardError
71
+ key
72
+ end
73
+ end
74
+
75
+ def symbolize_keys!
76
+ transform_keys! do |key|
77
+ key.to_sym
78
+ rescue StandardError
79
+ key
80
+ end
81
+ end
82
+
83
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Object
4
+
5
+ def blank?
6
+ object = self
7
+ object = object.strip if respond_to?(:strip)
8
+ return object.empty? if respond_to?(:empty?)
9
+
10
+ !object
11
+ end
12
+
13
+ def deep_dup
14
+ duplicable? ? dup : self
15
+ end
16
+
17
+ def duplicable?
18
+ true
19
+ end
20
+
21
+ def present?
22
+ !blank?
23
+ end
24
+
25
+ def presence
26
+ self if present?
27
+ end
28
+
29
+ def try(*obj, &block)
30
+ try!(*obj, &block) if obj.empty? || respond_to?(obj.first)
31
+ end
32
+
33
+ def try!(*obj, &block)
34
+ if obj.empty? && defined?(yield)
35
+ block.arity.zero? ? instance_eval(&block) : yield(self)
36
+ else
37
+ public_send(*obj, &block)
38
+ end
39
+ end
40
+
41
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Range
4
+
5
+ def overlaps?(other)
6
+ cover?(other.first) || other.cover?(first)
7
+ end
8
+
9
+ end
@@ -0,0 +1,184 @@
1
+ # frozen_string_literal: true
2
+
3
+ class String
4
+
5
+ def at(position)
6
+ self[position]
7
+ end
8
+
9
+ def camelize(first_letter = :upper)
10
+ case first_letter
11
+ when :upper, true then modulize.gsub(/(\A|\s)([a-z])/) { $1 + $2.upcase }
12
+ when :lower, false then modulize.gsub(/(\A|\s)([A-Z])/) { $1 + $2.downcase }
13
+ else modulize
14
+ end
15
+ end
16
+
17
+ def classify
18
+ str = dup
19
+ str.sub!(/.*\./, '')
20
+ str.camelize!
21
+ str || self
22
+ end
23
+
24
+ def constantize
25
+ Object.const_get(camelize)
26
+ end
27
+
28
+ def dasherize
29
+ underscore.tr('_', '-')
30
+ end
31
+
32
+ def deconstantize
33
+ [0, rindex('::') || 0]
34
+ end
35
+
36
+ def demodulize
37
+ gsub(/^.*::/, '')
38
+ end
39
+
40
+ def first(limit = 1)
41
+ if limit.zero?
42
+ ''
43
+ elsif limit >= length
44
+ self
45
+ else
46
+ to(limit - 1)
47
+ end
48
+ end
49
+
50
+ def from(position)
51
+ self[position..-1]
52
+ end
53
+
54
+ def humanize(capitalize: true)
55
+ str = dup
56
+ str.underscore!
57
+ str.delete_suffix!('_id')
58
+ str.tr!('_', ' ')
59
+ str.squish!
60
+ str.gsub!(/([a-z\d]*)/i, &:downcase)
61
+ str.gsub!(/\A\w/) { |s| capitalize ? s.upcase : s }
62
+ str || self
63
+ end
64
+
65
+ def indent(amount, seperator = ' ')
66
+ dup.indent!(amount, seperator)
67
+ end
68
+
69
+ def indent!(amount, seperator = ' ')
70
+ if amount >= 0
71
+ gsub!(/^/, seperator * amount)
72
+ else
73
+ gsub!(/^#{Regexp.escape(seperator)}{0,#{-amount}}/, '')
74
+ end
75
+ end
76
+
77
+ def last(limit = 1)
78
+ if limit.zero?
79
+ ''
80
+ elsif limit >= length
81
+ self
82
+ else
83
+ from(-limit)
84
+ end
85
+ end
86
+
87
+ def parameterize(separator: '-')
88
+ str = dup
89
+ str.underscore!
90
+ str.gsub!(/\s+/, separator)
91
+ str.downcase!
92
+ str || self
93
+ end
94
+
95
+ def remove(*patterns)
96
+ dup.remove!(*patterns)
97
+ end
98
+
99
+ def remove!(*patterns)
100
+ patterns.each_with_object(self) do |pat, str|
101
+ pat.is_a?(Range) ? str.slice!(pat) : str.gsub!(pat, '')
102
+ end
103
+ end
104
+
105
+ def squish
106
+ dup.squish!
107
+ end
108
+
109
+ def squish!
110
+ strip!
111
+ gsub!(/\s+/, ' ') || self
112
+ end
113
+
114
+ def titleize
115
+ str = dup
116
+ str.underscore!
117
+ str.humanize!
118
+ str.gsub!(/\b(?<!['’`])[a-z]/) { $&.capitalize }
119
+ str || self
120
+ end
121
+
122
+ def to(position)
123
+ self[0..position]
124
+ end
125
+
126
+ # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity
127
+ # rubocop:disable Metrics/MethodLength, Metrics/PerceivedComplexity
128
+ def to_time(form = :local)
129
+ parts = Date.parse(self, false)
130
+ used_keys = %i[year mon mday hour min sec sec_fraction offset]
131
+ return if (parts.keys & used_keys).empty?
132
+
133
+ now = Time.now
134
+ time = Time.new(
135
+ parts[:year] || now.year,
136
+ parts[:mon] || now.month,
137
+ parts[:mday] || now.day,
138
+ parts[:hour] || 0,
139
+ parts[:min] || 0,
140
+ (parts[:sec] || 0) + (parts[:sec_fraction] || 0),
141
+ parts[:offset] || (form == :utc ? 0 : nil)
142
+ )
143
+
144
+ form == :utc ? time.utc : time.to_time
145
+ end
146
+ # rubocop:enable Metrics/MethodLength, Metrics/PerceivedComplexity
147
+ # rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity
148
+
149
+ def truncate(truncate_at, options = {})
150
+ return self unless length > truncate_at
151
+
152
+ omission = options[:omission] || '...'
153
+ seperator = options[:separator]
154
+
155
+ size_with_room_for_omission = truncate_at - omission.length
156
+
157
+ stop = if seperator
158
+ rindex(seperator || '', size_with_room_for_omission) || size_with_room_for_omission
159
+ else
160
+ size_with_room_for_omission
161
+ end
162
+
163
+ "#{self[0, stop]}#{omission}"
164
+ end
165
+
166
+ def underscore
167
+ str = dup
168
+ str.camelize!
169
+ str.gsub!(/::/, '/')
170
+ str.gsub!(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2')
171
+ str.gsub!(/([a-z\d])([A-Z])/, '\1_\2')
172
+ str.tr!('-', '_')
173
+ str.downcase!
174
+ str || self
175
+ end
176
+
177
+ alias camelcase camelize
178
+ alias ends_with? end_with?
179
+ alias snakecase underscore
180
+ alias starts_with? start_with?
181
+ alias titlecase titleize
182
+ alias to_t to_time unless defined?(to_t)
183
+
184
+ end