lite-ruby 1.0.14 → 1.0.15
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -1
- data/Gemfile.lock +1 -1
- data/lib/lite/ruby/array.rb +221 -219
- data/lib/lite/ruby/boolean.rb +18 -16
- data/lib/lite/ruby/date.rb +18 -16
- data/lib/lite/ruby/enumerable.rb +169 -167
- data/lib/lite/ruby/hash.rb +309 -307
- data/lib/lite/ruby/helpers/date_helper.rb +97 -95
- data/lib/lite/ruby/helpers/time_helper.rb +79 -77
- data/lib/lite/ruby/integer.rb +57 -55
- data/lib/lite/ruby/kernel.rb +21 -19
- data/lib/lite/ruby/numeric.rb +189 -187
- data/lib/lite/ruby/object.rb +138 -136
- data/lib/lite/ruby/open_struct.rb +30 -28
- data/lib/lite/ruby/range.rb +23 -21
- data/lib/lite/ruby/string.rb +495 -493
- data/lib/lite/ruby/struct.rb +15 -13
- data/lib/lite/ruby/time.rb +27 -25
- data/lib/lite/ruby/version.rb +1 -1
- metadata +1 -1
data/lib/lite/ruby/object.rb
CHANGED
@@ -1,182 +1,184 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
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
|
+
def array?
|
14
|
+
is_a?(Array)
|
15
|
+
end
|
4
16
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
1 t true y yes on
|
10
|
-
].freeze
|
17
|
+
def blank?
|
18
|
+
object = self
|
19
|
+
object = object.strip if respond_to?(:strip)
|
20
|
+
return object.empty? if respond_to?(:empty?)
|
11
21
|
|
12
|
-
|
13
|
-
|
14
|
-
end
|
22
|
+
!object
|
23
|
+
end
|
15
24
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
return object.empty? if respond_to?(:empty?)
|
25
|
+
def bool?
|
26
|
+
true? || false?
|
27
|
+
end
|
20
28
|
|
21
|
-
|
22
|
-
|
29
|
+
def boolean?
|
30
|
+
val = to_s.downcase
|
31
|
+
TRUE_VALUES.include?(val) || FALSE_VALUES.include?(val)
|
32
|
+
end
|
23
33
|
|
24
|
-
|
25
|
-
|
26
|
-
|
34
|
+
def date?
|
35
|
+
is_a?(Date)
|
36
|
+
end
|
27
37
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
38
|
+
# rubocop:disable Style/YodaCondition
|
39
|
+
def false?
|
40
|
+
false == self
|
41
|
+
end
|
42
|
+
# rubocop:enable Style/YodaCondition
|
32
43
|
|
33
|
-
|
34
|
-
|
35
|
-
|
44
|
+
def falsey?
|
45
|
+
nil? || FALSE_VALUES.include?(to_s.downcase)
|
46
|
+
end
|
36
47
|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
end
|
41
|
-
# rubocop:enable Style/YodaCondition
|
48
|
+
def float?
|
49
|
+
is_a?(Float)
|
50
|
+
end
|
42
51
|
|
43
|
-
|
44
|
-
|
45
|
-
|
52
|
+
def hash?
|
53
|
+
is_a?(Hash)
|
54
|
+
end
|
46
55
|
|
47
|
-
|
48
|
-
|
49
|
-
|
56
|
+
def integer?
|
57
|
+
is_a?(Integer)
|
58
|
+
end
|
50
59
|
|
51
|
-
|
52
|
-
|
53
|
-
|
60
|
+
# rubocop:disable Naming/PredicateName
|
61
|
+
def is_any?(*objs)
|
62
|
+
objs.any? { |obj| is_a?(obj) }
|
63
|
+
end
|
64
|
+
# rubocop:enable Naming/PredicateName
|
54
65
|
|
55
|
-
|
56
|
-
|
57
|
-
|
66
|
+
def numeral?
|
67
|
+
!to_s.match(/\A[+-]?\d+?(\.\d+)?\Z/).nil?
|
68
|
+
end
|
58
69
|
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
end
|
63
|
-
# rubocop:enable Naming/PredicateName
|
70
|
+
def numeric?
|
71
|
+
is_a?(Numeric)
|
72
|
+
end
|
64
73
|
|
65
|
-
|
66
|
-
|
67
|
-
|
74
|
+
def open_struct?
|
75
|
+
is_a?(OpenStruct)
|
76
|
+
end
|
68
77
|
|
69
|
-
|
70
|
-
|
71
|
-
|
78
|
+
def palindrome?
|
79
|
+
to_s == to_s.reverse
|
80
|
+
end
|
72
81
|
|
73
|
-
|
74
|
-
|
75
|
-
|
82
|
+
def present?
|
83
|
+
!blank?
|
84
|
+
end
|
76
85
|
|
77
|
-
|
78
|
-
|
79
|
-
|
86
|
+
def range?
|
87
|
+
is_a?(Range)
|
88
|
+
end
|
80
89
|
|
81
|
-
|
82
|
-
|
83
|
-
|
90
|
+
def safe_call(*keys)
|
91
|
+
try_call(*keys) || self
|
92
|
+
end
|
84
93
|
|
85
|
-
|
86
|
-
|
87
|
-
|
94
|
+
def safe_send(*keys)
|
95
|
+
try_send(*keys) || self
|
96
|
+
end
|
88
97
|
|
89
|
-
|
90
|
-
|
91
|
-
|
98
|
+
def safe_try(*obj, &block)
|
99
|
+
try(*obj, &block) || self
|
100
|
+
end
|
92
101
|
|
93
|
-
|
94
|
-
|
95
|
-
|
102
|
+
def salvage(placeholder = '---')
|
103
|
+
blank? ? placeholder : self
|
104
|
+
end
|
96
105
|
|
97
|
-
|
98
|
-
|
99
|
-
|
106
|
+
def send_chain(*keys)
|
107
|
+
Array(keys).inject(self) { |obj, key| obj.send(*key) }
|
108
|
+
end
|
100
109
|
|
101
|
-
|
102
|
-
|
103
|
-
|
110
|
+
def send_chain_if(*keys)
|
111
|
+
Array(keys).inject(self) { |obj, key| obj.send_if(*key) }
|
112
|
+
end
|
104
113
|
|
105
|
-
|
106
|
-
|
107
|
-
end
|
114
|
+
def send_if(key, *args)
|
115
|
+
return self unless respond_to?(key)
|
108
116
|
|
109
|
-
|
110
|
-
|
111
|
-
end
|
117
|
+
send(key, *args)
|
118
|
+
end
|
112
119
|
|
113
|
-
|
114
|
-
|
120
|
+
def set?
|
121
|
+
is_a?(Set)
|
122
|
+
end
|
115
123
|
|
116
|
-
|
117
|
-
|
124
|
+
def string?
|
125
|
+
is_a?(String)
|
126
|
+
end
|
118
127
|
|
119
|
-
|
120
|
-
|
121
|
-
|
128
|
+
def struct?
|
129
|
+
is_a?(Struct)
|
130
|
+
end
|
122
131
|
|
123
|
-
|
124
|
-
|
125
|
-
|
132
|
+
def symbol?
|
133
|
+
is_a?(Symbol)
|
134
|
+
end
|
126
135
|
|
127
|
-
|
128
|
-
|
129
|
-
|
136
|
+
def time?
|
137
|
+
is_a?(Time)
|
138
|
+
end
|
130
139
|
|
131
|
-
|
132
|
-
|
133
|
-
|
140
|
+
def to_bool
|
141
|
+
return true if truthy?
|
142
|
+
return false if falsey?
|
134
143
|
|
135
|
-
|
136
|
-
|
137
|
-
end
|
144
|
+
nil
|
145
|
+
end
|
138
146
|
|
139
|
-
|
140
|
-
return true if truthy?
|
141
|
-
return false if falsey?
|
147
|
+
alias to_b to_bool
|
142
148
|
|
143
|
-
|
144
|
-
|
149
|
+
# rubocop:disable Style/YodaCondition
|
150
|
+
def true?
|
151
|
+
true == self
|
152
|
+
end
|
153
|
+
# rubocop:enable Style/YodaCondition
|
145
154
|
|
146
|
-
|
155
|
+
def truthy?
|
156
|
+
TRUE_VALUES.include?(to_s.downcase)
|
157
|
+
end
|
147
158
|
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
end
|
152
|
-
# rubocop:enable Style/YodaCondition
|
159
|
+
def try(*obj, &block)
|
160
|
+
try!(*obj, &block) if obj.empty? || respond_to?(obj.first)
|
161
|
+
end
|
153
162
|
|
154
|
-
|
155
|
-
|
156
|
-
|
163
|
+
def try!(*obj, &block)
|
164
|
+
if obj.empty? && block_given?
|
165
|
+
block.arity.zero? ? instance_eval(&block) : yield(self)
|
166
|
+
else
|
167
|
+
public_send(*obj, &block)
|
168
|
+
end
|
169
|
+
end
|
157
170
|
|
158
|
-
|
159
|
-
|
160
|
-
end
|
171
|
+
def try_call(*keys)
|
172
|
+
return unless respond_to?(:call)
|
161
173
|
|
162
|
-
|
163
|
-
if obj.empty? && block_given?
|
164
|
-
block.arity.zero? ? instance_eval(&block) : yield(self)
|
165
|
-
else
|
166
|
-
public_send(*obj, &block)
|
174
|
+
keys.blank? ? call : call(*keys)
|
167
175
|
end
|
168
|
-
end
|
169
176
|
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
177
|
+
def try_send(*keys)
|
178
|
+
send(*keys)
|
179
|
+
rescue StandardError
|
180
|
+
nil
|
181
|
+
end
|
175
182
|
|
176
|
-
def try_send(*keys)
|
177
|
-
send(*keys)
|
178
|
-
rescue StandardError
|
179
|
-
nil
|
180
183
|
end
|
181
|
-
|
182
184
|
end
|
@@ -1,42 +1,44 @@
|
|
1
1
|
# frozen_string_literal: false
|
2
2
|
|
3
|
-
|
3
|
+
if Lite::Ruby.configuration.monkey_patches.include?('open_struct')
|
4
|
+
require 'ostruct'
|
4
5
|
|
5
|
-
class OpenStruct
|
6
|
+
class OpenStruct
|
6
7
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
8
|
+
def initialize(hash = nil, &block)
|
9
|
+
@table = if block && block.arity == 2
|
10
|
+
Hash.new(&block)
|
11
|
+
else
|
12
|
+
{}
|
13
|
+
end
|
13
14
|
|
14
|
-
|
15
|
-
|
16
|
-
|
15
|
+
hash&.each do |key, val|
|
16
|
+
@table[key.to_sym] = val
|
17
|
+
new_ostruct_member(key)
|
18
|
+
end
|
19
|
+
|
20
|
+
yield self if block && block.arity == 1
|
17
21
|
end
|
18
22
|
|
19
|
-
|
20
|
-
|
23
|
+
def [](key)
|
24
|
+
key = key.to_sym unless key.is_a?(Symbol)
|
25
|
+
@table[key]
|
26
|
+
end
|
21
27
|
|
22
|
-
|
23
|
-
|
24
|
-
@table[key]
|
25
|
-
end
|
28
|
+
def []=(key, val)
|
29
|
+
raise TypeError, "can't modify frozen #{self.class}", caller(1) if frozen?
|
26
30
|
|
27
|
-
|
28
|
-
|
31
|
+
key = key.to_sym unless key.is_a?(Symbol)
|
32
|
+
@table[key] = val
|
33
|
+
end
|
29
34
|
|
30
|
-
|
31
|
-
|
32
|
-
|
35
|
+
def attributes
|
36
|
+
each_pair.with_object({}) { |(key, val), hash| hash[key] = val }
|
37
|
+
end
|
33
38
|
|
34
|
-
|
35
|
-
|
36
|
-
|
39
|
+
def replace(args)
|
40
|
+
args.each_pair { |key, val| send("#{key}=", val) }
|
41
|
+
end
|
37
42
|
|
38
|
-
def replace(args)
|
39
|
-
args.each_pair { |key, val| send("#{key}=", val) }
|
40
43
|
end
|
41
|
-
|
42
44
|
end
|
data/lib/lite/ruby/range.rb
CHANGED
@@ -1,32 +1,34 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
3
|
+
if Lite::Ruby.configuration.monkey_patches.include?('range')
|
4
|
+
class Range
|
4
5
|
|
5
|
-
|
6
|
-
|
7
|
-
|
6
|
+
def combine(other)
|
7
|
+
to_a.concat(other.to_a)
|
8
|
+
end
|
8
9
|
|
9
|
-
|
10
|
-
|
10
|
+
def include_with_range?(other)
|
11
|
+
return include?(other) unless other.is_a?(Range)
|
11
12
|
|
12
|
-
|
13
|
-
|
14
|
-
|
13
|
+
operator = exclude_end? && !other.exclude_end? ? :< : :<=
|
14
|
+
include?(other.first) && other.last.send(operator, last)
|
15
|
+
end
|
15
16
|
|
16
|
-
|
17
|
-
|
18
|
-
|
17
|
+
def overlaps?(other)
|
18
|
+
cover?(other.first) || other.cover?(first)
|
19
|
+
end
|
19
20
|
|
20
|
-
|
21
|
-
|
22
|
-
|
21
|
+
def sample
|
22
|
+
to_a.sample
|
23
|
+
end
|
23
24
|
|
24
|
-
|
25
|
-
|
26
|
-
|
25
|
+
def shuffle
|
26
|
+
to_a.shuffle
|
27
|
+
end
|
27
28
|
|
28
|
-
|
29
|
-
|
30
|
-
|
29
|
+
def within?(other)
|
30
|
+
cover?(other.first) && cover?(other.last)
|
31
|
+
end
|
31
32
|
|
33
|
+
end
|
32
34
|
end
|