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.
Files changed (37) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +21 -1
  3. data/Gemfile.lock +1 -1
  4. data/README.md +26 -6
  5. data/docs/HASH.md +13 -0
  6. data/lib/generators/lite/ruby/install_generator.rb +0 -2
  7. data/lib/generators/lite/ruby/templates/install.rb +18 -6
  8. data/lib/lite/ruby.rb +2 -15
  9. data/lib/lite/ruby/all.rb +16 -0
  10. data/lib/lite/ruby/array.rb +218 -220
  11. data/lib/lite/ruby/boolean.rb +17 -19
  12. data/lib/lite/ruby/date.rb +13 -20
  13. data/lib/lite/ruby/enumerable.rb +153 -155
  14. data/lib/lite/ruby/formats/date_stamps.yml +34 -0
  15. data/lib/lite/ruby/formats/date_units.yml +42 -0
  16. data/lib/lite/ruby/formats/integer_roman_numerals.yml +13 -0
  17. data/lib/lite/ruby/formats/string_transliterations.yml +189 -0
  18. data/lib/lite/ruby/formats/time_stamps.yml +42 -0
  19. data/lib/lite/ruby/formats/time_units.yml +28 -0
  20. data/lib/lite/ruby/hash.rb +313 -301
  21. data/lib/lite/ruby/helpers/date_time_helper.rb +24 -0
  22. data/lib/lite/ruby/integer.rb +62 -63
  23. data/lib/lite/ruby/kernel.rb +19 -21
  24. data/lib/lite/ruby/numeric.rb +175 -177
  25. data/lib/lite/ruby/object.rb +128 -130
  26. data/lib/lite/ruby/open_struct.rb +17 -19
  27. data/lib/lite/ruby/range.rb +19 -21
  28. data/lib/lite/ruby/safe/object.rb +10 -8
  29. data/lib/lite/ruby/safe/string.rb +8 -7
  30. data/lib/lite/ruby/string.rb +348 -384
  31. data/lib/lite/ruby/struct.rb +7 -9
  32. data/lib/lite/ruby/time.rb +25 -30
  33. data/lib/lite/ruby/version.rb +1 -1
  34. metadata +10 -5
  35. data/lib/lite/ruby/configuration.rb +0 -38
  36. data/lib/lite/ruby/helpers/date_helper.rb +0 -107
  37. data/lib/lite/ruby/helpers/time_helper.rb +0 -86
@@ -1,171 +1,169 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- if Lite::Ruby.configuration.monkey_patches.include?('object')
4
- class Object
5
-
6
- FALSE_VALUES = %w[
7
- 0 f false n no off
8
- ].freeze
9
- TRUE_VALUES = %w[
10
- 1 t true y yes on
11
- ].freeze
12
-
13
- # NOTE: There is a class between the PG gem and the `array?` method.
14
- # We only need to skip this on migrations since that action
15
- # happens on a seperate runtime.
16
- unless defined?(PG) && ARGV.first.to_s.start_with?('db:')
17
- def array?
18
- is_a?(Array)
19
- end
20
- end
21
-
22
- def bool?
23
- true? || false?
24
- end
25
-
26
- def boolean?
27
- val = to_s.downcase
28
- TRUE_VALUES.include?(val) || FALSE_VALUES.include?(val)
3
+ class Object
4
+
5
+ FALSE_VALUES = %w[
6
+ 0 f false n no off
7
+ ].freeze
8
+ TRUE_VALUES = %w[
9
+ 1 t true y yes on
10
+ ].freeze
11
+
12
+ # NOTE: There is a clash between the PG gem and the `array?` method.
13
+ # We only need to skip this on migrations since that action
14
+ # happens on a seperate runtime.
15
+ unless defined?(PG) && ARGV.first.to_s.start_with?('db:')
16
+ def array?
17
+ is_a?(Array)
29
18
  end
19
+ end
30
20
 
31
- def date?
32
- is_a?(Date)
33
- end
21
+ def bool?
22
+ true? || false?
23
+ end
34
24
 
35
- # rubocop:disable Style/YodaCondition
36
- def false?
37
- false == self
38
- end
39
- # rubocop:enable Style/YodaCondition
25
+ def boolean?
26
+ val = to_s.downcase
27
+ TRUE_VALUES.include?(val) || FALSE_VALUES.include?(val)
28
+ end
40
29
 
41
- def falsey?
42
- nil? || FALSE_VALUES.include?(to_s.downcase)
43
- end
30
+ def date?
31
+ is_a?(Date)
32
+ end
44
33
 
45
- def float?
46
- is_a?(Float)
47
- end
34
+ # rubocop:disable Style/YodaCondition
35
+ def false?
36
+ false == self
37
+ end
38
+ # rubocop:enable Style/YodaCondition
48
39
 
49
- def hash?
50
- is_a?(Hash)
51
- end
40
+ def falsey?
41
+ nil? || FALSE_VALUES.include?(to_s.downcase)
42
+ end
52
43
 
53
- def integer?
54
- is_a?(Integer)
55
- end
44
+ def float?
45
+ is_a?(Float)
46
+ end
56
47
 
57
- # rubocop:disable Naming/PredicateName
58
- def is_any?(*objs)
59
- objs.any? { |obj| is_a?(obj) }
60
- end
61
- # rubocop:enable Naming/PredicateName
48
+ def hash?
49
+ is_a?(Hash)
50
+ end
62
51
 
63
- def numeral?
64
- !to_s.match(/\A[+-]?\d+?(\.\d+)?\Z/).nil?
65
- end
52
+ def integer?
53
+ is_a?(Integer)
54
+ end
66
55
 
67
- def numeric?
68
- is_a?(Numeric)
69
- end
56
+ # rubocop:disable Naming/PredicateName
57
+ def is_any?(*objects)
58
+ objects.any? { |obj| is_a?(obj) }
59
+ end
60
+ # rubocop:enable Naming/PredicateName
70
61
 
71
- def open_struct?
72
- is_a?(OpenStruct)
73
- end
62
+ def numeral?
63
+ !to_s.match(/\A[+-]?\d+?(\.\d+)?\Z/).nil?
64
+ end
74
65
 
75
- def palindrome?
76
- to_s == to_s.reverse
77
- end
66
+ def numeric?
67
+ is_a?(Numeric)
68
+ end
78
69
 
79
- def range?
80
- is_a?(Range)
81
- end
70
+ def open_struct?
71
+ is_a?(OpenStruct)
72
+ end
82
73
 
83
- def safe_call(*keys)
84
- try_call(*keys) || self
85
- end
74
+ def palindrome?
75
+ to_s == to_s.reverse
76
+ end
86
77
 
87
- def safe_send(*keys)
88
- try_send(*keys) || self
89
- end
78
+ def range?
79
+ is_a?(Range)
80
+ end
90
81
 
91
- def safe_try(*obj, &block)
92
- try(*obj, &block) || self
93
- end
82
+ def safe_call(...)
83
+ try_call(...) || self
84
+ end
94
85
 
95
- def salvage(placeholder = '---')
96
- blank? ? placeholder : self
97
- end
86
+ def safe_send(...)
87
+ try_send(...) || self
88
+ end
98
89
 
99
- def salvage_try(*obj, placeholder: '---', &block)
100
- try(*obj, &block).salvage(placeholder)
101
- end
90
+ def safe_try(...)
91
+ try(...) || self
92
+ end
102
93
 
103
- def send_chain(*keys)
104
- Array(keys).inject(self) { |obj, key| obj.send(*key) }
105
- end
94
+ def salvage(placeholder = '---')
95
+ blank? ? placeholder : self
96
+ end
106
97
 
107
- def send_chain_if(*keys)
108
- Array(keys).inject(self) { |obj, key| obj.send_if(*key) }
109
- end
98
+ def salvage_try(method_name = nil, *args, placeholder: '---', &block)
99
+ try(method_name, *args, &block).salvage(placeholder)
100
+ end
110
101
 
111
- def send_if(key, *args)
112
- return self unless respond_to?(key)
102
+ def send_chain(*args)
103
+ Array(args).inject(self) { |obj, argz| obj.send(*argz) }
104
+ end
113
105
 
114
- send(key, *args)
115
- end
106
+ def send_chain_if(*args)
107
+ Array(args).inject(self) { |obj, argz| obj.send_if(*argz) }
108
+ end
116
109
 
117
- def set?
118
- is_a?(Set)
119
- end
110
+ def send_if(key, *args, **kwargs, &block)
111
+ return self unless respond_to?(key)
120
112
 
121
- def string?
122
- is_a?(String)
123
- end
113
+ send(key, *args, **kwargs, &block)
114
+ end
124
115
 
125
- def struct?
126
- is_a?(Struct)
127
- end
116
+ def set?
117
+ is_a?(Set)
118
+ end
128
119
 
129
- def symbol?
130
- is_a?(Symbol)
131
- end
120
+ def string?
121
+ is_a?(String)
122
+ end
132
123
 
133
- def time?
134
- is_a?(Time)
135
- end
124
+ def struct?
125
+ is_a?(Struct)
126
+ end
136
127
 
137
- def to_bool
138
- return true if truthy?
139
- return false if falsey?
128
+ def symbol?
129
+ is_a?(Symbol)
130
+ end
140
131
 
141
- nil
142
- end
132
+ def time?
133
+ is_a?(Time)
134
+ end
143
135
 
144
- # rubocop:disable Style/YodaCondition
145
- def true?
146
- true == self
147
- end
148
- # rubocop:enable Style/YodaCondition
136
+ def to_bool
137
+ return true if truthy?
138
+ return false if falsey?
149
139
 
150
- def truthy?
151
- TRUE_VALUES.include?(to_s.downcase)
152
- end
140
+ nil
141
+ end
153
142
 
154
- def try_call(*keys)
155
- return unless respond_to?(:call)
143
+ # rubocop:disable Style/YodaCondition
144
+ def true?
145
+ true == self
146
+ end
147
+ # rubocop:enable Style/YodaCondition
156
148
 
157
- keys.blank? ? call : call(*keys)
158
- end
149
+ def truthy?
150
+ TRUE_VALUES.include?(to_s.downcase)
151
+ end
159
152
 
160
- def try_send(*keys)
161
- send(*keys)
162
- rescue StandardError
163
- nil
164
- end
153
+ def try_call(...)
154
+ return unless respond_to?(:call)
165
155
 
166
- alias to_b to_bool
156
+ call(...)
157
+ end
167
158
 
159
+ def try_send(...)
160
+ send(...)
161
+ rescue StandardError
162
+ nil
168
163
  end
169
164
 
170
- require 'lite/ruby/safe/object' unless defined?(ActiveSupport)
165
+ alias to_b to_bool
166
+
171
167
  end
168
+
169
+ require 'lite/ruby/safe/object' unless defined?(ActiveSupport)
@@ -1,30 +1,28 @@
1
1
  # frozen_string_literal: false
2
2
 
3
- if Lite::Ruby.configuration.monkey_patches.include?('open_struct')
4
- require 'ostruct'
3
+ require 'ostruct' unless defined?(OpenStruct)
5
4
 
6
- class OpenStruct
5
+ class OpenStruct
7
6
 
8
- def attributes
9
- @table
10
- end
7
+ def attributes
8
+ @table
9
+ end
11
10
 
12
- def replace(args)
13
- args.each { |key, val| send("#{key}=", val) }
14
- end
11
+ def replace(args)
12
+ args.each { |key, val| self[key] = val }
13
+ end
15
14
 
16
- def to_hash(table: true)
17
- return attributes unless table
15
+ def to_hash(table: true)
16
+ return attributes unless table
18
17
 
19
- { table: attributes }
20
- end
18
+ { table: attributes }
19
+ end
21
20
 
22
- def to_json(table: true)
23
- to_hash(table: table).to_json
24
- end
21
+ def to_json(table: true)
22
+ to_hash(table: table).to_json
23
+ end
25
24
 
26
- alias as_json to_json
27
- alias to_h to_hash
25
+ alias as_json to_json
26
+ alias to_h to_hash
28
27
 
29
- end
30
28
  end
@@ -1,32 +1,30 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- if Lite::Ruby.configuration.monkey_patches.include?('range')
4
- class Range
3
+ class Range
5
4
 
6
- def combine(other)
7
- to_a.concat(other.to_a)
8
- end
9
-
10
- def include_with_range?(other)
11
- return include?(other) unless other.is_a?(Range)
5
+ def combine(other)
6
+ to_a.concat(other.to_a)
7
+ end
12
8
 
13
- operator = exclude_end? && !other.exclude_end? ? :< : :<=
14
- include?(other.first) && other.last.send(operator, last)
15
- end
9
+ def include_with_range?(other)
10
+ return include?(other) unless other.is_a?(Range)
16
11
 
17
- def sample
18
- to_a.sample
19
- end
12
+ operator = exclude_end? && !other.exclude_end? ? :< : :<=
13
+ include?(other.first) && other.last.send(operator, last)
14
+ end
20
15
 
21
- def shuffle
22
- to_a.shuffle
23
- end
16
+ def sample
17
+ to_a.sample
18
+ end
24
19
 
25
- def within?(other)
26
- cover?(other.first) && cover?(other.last)
27
- end
20
+ def shuffle
21
+ to_a.shuffle
22
+ end
28
23
 
24
+ def within?(other)
25
+ cover?(other.first) && cover?(other.last)
29
26
  end
30
27
 
31
- require 'lite/ruby/safe/range' unless defined?(ActiveSupport)
32
28
  end
29
+
30
+ require 'lite/ruby/safe/range' unless defined?(ActiveSupport)
@@ -5,9 +5,7 @@ class Object
5
5
  def blank?
6
6
  object = self
7
7
  object = object.strip if respond_to?(:strip)
8
- return object.empty? if respond_to?(:empty?)
9
-
10
- !object
8
+ respond_to?(:empty?) ? object.empty? : !object
11
9
  end
12
10
 
13
11
  def deep_dup
@@ -26,15 +24,19 @@ class Object
26
24
  self if present?
27
25
  end
28
26
 
29
- def try(*obj, &block)
30
- try!(*obj, &block) if obj.empty? || respond_to?(obj.first)
27
+ def try(method_name = nil, *args, &block)
28
+ if method_name.nil? && block
29
+ block.arity.zero? ? instance_eval(&block) : yield(self)
30
+ elsif respond_to?(method_name)
31
+ public_send(method_name, *args, &block)
32
+ end
31
33
  end
32
34
 
33
- def try!(*obj, &block)
34
- if obj.empty? && defined?(yield)
35
+ def try!(method_name = nil, *args, &block)
36
+ if method_name.nil? && block
35
37
  block.arity.zero? ? instance_eval(&block) : yield(self)
36
38
  else
37
- public_send(*obj, &block)
39
+ public_send(method_name, *args, &block)
38
40
  end
39
41
  end
40
42
 
@@ -151,16 +151,17 @@ class String
151
151
 
152
152
  omission = options[:omission] || '...'
153
153
  seperator = options[:separator]
154
-
155
154
  size_with_room_for_omission = truncate_at - omission.length
155
+ stop = rindex(seperator || '', size_with_room_for_omission) if seperator
156
+ "#{self[0, stop || size_with_room_for_omission]}#{omission}"
157
+ end
156
158
 
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
159
+ def truncate_words(words_count, options = {})
160
+ sep = options[:separator] || /\s+/
161
+ sep = Regexp.escape(sep.to_s) unless sep.is_a?(Regexp)
162
+ return dup unless self =~ /\A((?>.+?#{sep}){#{words_count - 1}}.+?)#{sep}.*/m
162
163
 
163
- "#{self[0, stop]}#{omission}"
164
+ $1 + (options[:omission] || '...')
164
165
  end
165
166
 
166
167
  def underscore