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