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/object.rb
CHANGED
@@ -1,171 +1,169 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
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
|
-
|
32
|
-
|
33
|
-
|
21
|
+
def bool?
|
22
|
+
true? || false?
|
23
|
+
end
|
34
24
|
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
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
|
-
|
42
|
-
|
43
|
-
|
30
|
+
def date?
|
31
|
+
is_a?(Date)
|
32
|
+
end
|
44
33
|
|
45
|
-
|
46
|
-
|
47
|
-
|
34
|
+
# rubocop:disable Style/YodaCondition
|
35
|
+
def false?
|
36
|
+
false == self
|
37
|
+
end
|
38
|
+
# rubocop:enable Style/YodaCondition
|
48
39
|
|
49
|
-
|
50
|
-
|
51
|
-
|
40
|
+
def falsey?
|
41
|
+
nil? || FALSE_VALUES.include?(to_s.downcase)
|
42
|
+
end
|
52
43
|
|
53
|
-
|
54
|
-
|
55
|
-
|
44
|
+
def float?
|
45
|
+
is_a?(Float)
|
46
|
+
end
|
56
47
|
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
end
|
61
|
-
# rubocop:enable Naming/PredicateName
|
48
|
+
def hash?
|
49
|
+
is_a?(Hash)
|
50
|
+
end
|
62
51
|
|
63
|
-
|
64
|
-
|
65
|
-
|
52
|
+
def integer?
|
53
|
+
is_a?(Integer)
|
54
|
+
end
|
66
55
|
|
67
|
-
|
68
|
-
|
69
|
-
|
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
|
-
|
72
|
-
|
73
|
-
|
62
|
+
def numeral?
|
63
|
+
!to_s.match(/\A[+-]?\d+?(\.\d+)?\Z/).nil?
|
64
|
+
end
|
74
65
|
|
75
|
-
|
76
|
-
|
77
|
-
|
66
|
+
def numeric?
|
67
|
+
is_a?(Numeric)
|
68
|
+
end
|
78
69
|
|
79
|
-
|
80
|
-
|
81
|
-
|
70
|
+
def open_struct?
|
71
|
+
is_a?(OpenStruct)
|
72
|
+
end
|
82
73
|
|
83
|
-
|
84
|
-
|
85
|
-
|
74
|
+
def palindrome?
|
75
|
+
to_s == to_s.reverse
|
76
|
+
end
|
86
77
|
|
87
|
-
|
88
|
-
|
89
|
-
|
78
|
+
def range?
|
79
|
+
is_a?(Range)
|
80
|
+
end
|
90
81
|
|
91
|
-
|
92
|
-
|
93
|
-
|
82
|
+
def safe_call(...)
|
83
|
+
try_call(...) || self
|
84
|
+
end
|
94
85
|
|
95
|
-
|
96
|
-
|
97
|
-
|
86
|
+
def safe_send(...)
|
87
|
+
try_send(...) || self
|
88
|
+
end
|
98
89
|
|
99
|
-
|
100
|
-
|
101
|
-
|
90
|
+
def safe_try(...)
|
91
|
+
try(...) || self
|
92
|
+
end
|
102
93
|
|
103
|
-
|
104
|
-
|
105
|
-
|
94
|
+
def salvage(placeholder = '---')
|
95
|
+
blank? ? placeholder : self
|
96
|
+
end
|
106
97
|
|
107
|
-
|
108
|
-
|
109
|
-
|
98
|
+
def salvage_try(method_name = nil, *args, placeholder: '---', &block)
|
99
|
+
try(method_name, *args, &block).salvage(placeholder)
|
100
|
+
end
|
110
101
|
|
111
|
-
|
112
|
-
|
102
|
+
def send_chain(*args)
|
103
|
+
Array(args).inject(self) { |obj, argz| obj.send(*argz) }
|
104
|
+
end
|
113
105
|
|
114
|
-
|
115
|
-
|
106
|
+
def send_chain_if(*args)
|
107
|
+
Array(args).inject(self) { |obj, argz| obj.send_if(*argz) }
|
108
|
+
end
|
116
109
|
|
117
|
-
|
118
|
-
|
119
|
-
end
|
110
|
+
def send_if(key, *args, **kwargs, &block)
|
111
|
+
return self unless respond_to?(key)
|
120
112
|
|
121
|
-
|
122
|
-
|
123
|
-
end
|
113
|
+
send(key, *args, **kwargs, &block)
|
114
|
+
end
|
124
115
|
|
125
|
-
|
126
|
-
|
127
|
-
|
116
|
+
def set?
|
117
|
+
is_a?(Set)
|
118
|
+
end
|
128
119
|
|
129
|
-
|
130
|
-
|
131
|
-
|
120
|
+
def string?
|
121
|
+
is_a?(String)
|
122
|
+
end
|
132
123
|
|
133
|
-
|
134
|
-
|
135
|
-
|
124
|
+
def struct?
|
125
|
+
is_a?(Struct)
|
126
|
+
end
|
136
127
|
|
137
|
-
|
138
|
-
|
139
|
-
|
128
|
+
def symbol?
|
129
|
+
is_a?(Symbol)
|
130
|
+
end
|
140
131
|
|
141
|
-
|
142
|
-
|
132
|
+
def time?
|
133
|
+
is_a?(Time)
|
134
|
+
end
|
143
135
|
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
end
|
148
|
-
# rubocop:enable Style/YodaCondition
|
136
|
+
def to_bool
|
137
|
+
return true if truthy?
|
138
|
+
return false if falsey?
|
149
139
|
|
150
|
-
|
151
|
-
|
152
|
-
end
|
140
|
+
nil
|
141
|
+
end
|
153
142
|
|
154
|
-
|
155
|
-
|
143
|
+
# rubocop:disable Style/YodaCondition
|
144
|
+
def true?
|
145
|
+
true == self
|
146
|
+
end
|
147
|
+
# rubocop:enable Style/YodaCondition
|
156
148
|
|
157
|
-
|
158
|
-
|
149
|
+
def truthy?
|
150
|
+
TRUE_VALUES.include?(to_s.downcase)
|
151
|
+
end
|
159
152
|
|
160
|
-
|
161
|
-
|
162
|
-
rescue StandardError
|
163
|
-
nil
|
164
|
-
end
|
153
|
+
def try_call(...)
|
154
|
+
return unless respond_to?(:call)
|
165
155
|
|
166
|
-
|
156
|
+
call(...)
|
157
|
+
end
|
167
158
|
|
159
|
+
def try_send(...)
|
160
|
+
send(...)
|
161
|
+
rescue StandardError
|
162
|
+
nil
|
168
163
|
end
|
169
164
|
|
170
|
-
|
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
|
-
|
4
|
-
require 'ostruct'
|
3
|
+
require 'ostruct' unless defined?(OpenStruct)
|
5
4
|
|
6
|
-
|
5
|
+
class OpenStruct
|
7
6
|
|
8
|
-
|
9
|
-
|
10
|
-
|
7
|
+
def attributes
|
8
|
+
@table
|
9
|
+
end
|
11
10
|
|
12
|
-
|
13
|
-
|
14
|
-
|
11
|
+
def replace(args)
|
12
|
+
args.each { |key, val| self[key] = val }
|
13
|
+
end
|
15
14
|
|
16
|
-
|
17
|
-
|
15
|
+
def to_hash(table: true)
|
16
|
+
return attributes unless table
|
18
17
|
|
19
|
-
|
20
|
-
|
18
|
+
{ table: attributes }
|
19
|
+
end
|
21
20
|
|
22
|
-
|
23
|
-
|
24
|
-
|
21
|
+
def to_json(table: true)
|
22
|
+
to_hash(table: table).to_json
|
23
|
+
end
|
25
24
|
|
26
|
-
|
27
|
-
|
25
|
+
alias as_json to_json
|
26
|
+
alias to_h to_hash
|
28
27
|
|
29
|
-
end
|
30
28
|
end
|
data/lib/lite/ruby/range.rb
CHANGED
@@ -1,32 +1,30 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
class Range
|
3
|
+
class Range
|
5
4
|
|
6
|
-
|
7
|
-
|
8
|
-
|
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
|
-
|
14
|
-
|
15
|
-
end
|
9
|
+
def include_with_range?(other)
|
10
|
+
return include?(other) unless other.is_a?(Range)
|
16
11
|
|
17
|
-
|
18
|
-
|
19
|
-
|
12
|
+
operator = exclude_end? && !other.exclude_end? ? :< : :<=
|
13
|
+
include?(other.first) && other.last.send(operator, last)
|
14
|
+
end
|
20
15
|
|
21
|
-
|
22
|
-
|
23
|
-
|
16
|
+
def sample
|
17
|
+
to_a.sample
|
18
|
+
end
|
24
19
|
|
25
|
-
|
26
|
-
|
27
|
-
|
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
|
-
|
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(*
|
30
|
-
|
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!(*
|
34
|
-
if
|
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(*
|
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
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
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
|
-
|
164
|
+
$1 + (options[:omission] || '...')
|
164
165
|
end
|
165
166
|
|
166
167
|
def underscore
|