ruby-rails-extensions 1.1.1 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +8 -0
- data/lib/ruby-rails-extensions/extensions/all_keys.rb +3 -5
- data/lib/ruby-rails-extensions/extensions/all_values.rb +3 -5
- data/lib/ruby-rails-extensions/extensions/any_key.rb +3 -5
- data/lib/ruby-rails-extensions/extensions/any_value.rb +3 -5
- data/lib/ruby-rails-extensions/extensions/assign_nil.rb +51 -56
- data/lib/ruby-rails-extensions/extensions/clean_string.rb +22 -30
- data/lib/ruby-rails-extensions/extensions/compact_blank_join.rb +6 -8
- data/lib/ruby-rails-extensions/extensions/compact_map.rb +11 -15
- data/lib/ruby-rails-extensions/extensions/current_day.rb +3 -5
- data/lib/ruby-rails-extensions/extensions/current_month.rb +6 -10
- data/lib/ruby-rails-extensions/extensions/current_week.rb +6 -10
- data/lib/ruby-rails-extensions/extensions/day_of_week.rb +6 -10
- data/lib/ruby-rails-extensions/extensions/display_military.rb +6 -10
- data/lib/ruby-rails-extensions/extensions/display_user.rb +9 -15
- data/lib/ruby-rails-extensions/extensions/find_bang.rb +7 -11
- data/lib/ruby-rails-extensions/extensions/find_dupes.rb +8 -10
- data/lib/ruby-rails-extensions/extensions/first_dupe.rb +4 -6
- data/lib/ruby-rails-extensions/extensions/google_format.rb +3 -5
- data/lib/ruby-rails-extensions/extensions/hash_only.rb +16 -20
- data/lib/ruby-rails-extensions/extensions/humanize_symbol.rb +2 -4
- data/lib/ruby-rails-extensions/extensions/in_utc.rb +6 -10
- data/lib/ruby-rails-extensions/extensions/input.rb +6 -10
- data/lib/ruby-rails-extensions/extensions/month_and_year.rb +3 -5
- data/lib/ruby-rails-extensions/extensions/month_year.rb +3 -5
- data/lib/ruby-rails-extensions/extensions/no_keys.rb +3 -5
- data/lib/ruby-rails-extensions/extensions/no_values.rb +3 -5
- data/lib/ruby-rails-extensions/extensions/only_some.rb +16 -18
- data/lib/ruby-rails-extensions/extensions/overlaps.rb +5 -9
- data/lib/ruby-rails-extensions/extensions/pipe.rb +6 -10
- data/lib/ruby-rails-extensions/extensions/presence_bang.rb +9 -11
- data/lib/ruby-rails-extensions/extensions/quarter_dates.rb +28 -34
- data/lib/ruby-rails-extensions/extensions/range_add.rb +36 -38
- data/lib/ruby-rails-extensions/extensions/range_multiply.rb +36 -38
- data/lib/ruby-rails-extensions/extensions/remove_reserved_keywords.rb +18 -20
- data/lib/ruby-rails-extensions/extensions/safe_parse.rb +15 -21
- data/lib/ruby-rails-extensions/extensions/select_present.rb +2 -6
- data/lib/ruby-rails-extensions/extensions/select_present_join.rb +1 -3
- data/lib/ruby-rails-extensions/extensions/set_to_sentence.rb +2 -4
- data/lib/ruby-rails-extensions/extensions/to_bool.rb +14 -24
- data/lib/ruby-rails-extensions/extensions/to_dec.rb +9 -15
- data/lib/ruby-rails-extensions/extensions/to_i.rb +6 -10
- data/lib/ruby-rails-extensions/extensions/to_local.rb +8 -12
- data/lib/ruby-rails-extensions/extensions/to_negative_i.rb +22 -28
- data/lib/ruby-rails-extensions/extensions/to_nonzero_i.rb +22 -28
- data/lib/ruby-rails-extensions/extensions/to_or_sentence.rb +5 -9
- data/lib/ruby-rails-extensions/extensions/to_positive_i.rb +22 -28
- data/lib/ruby-rails-extensions/extensions/to_sort_i.rb +5 -11
- data/lib/ruby-rails-extensions/extensions/to_x.rb +9 -15
- data/lib/ruby-rails-extensions/extensions/todays_date.rb +9 -12
- data/lib/ruby-rails-extensions/extensions/uniq_map.rb +13 -15
- data/lib/ruby-rails-extensions/extensions/usd_to_f.rb +9 -15
- data/lib/ruby-rails-extensions/extensions/usd_to_i.rb +6 -10
- data/lib/ruby-rails-extensions/extensions/weighted_sum.rb +16 -18
- data/lib/ruby-rails-extensions/extensions/yesno.rb +9 -15
- data/lib/ruby-rails-extensions/extensions/zero_range.rb +14 -16
- data/lib/ruby-rails-extensions/version.rb +1 -1
- metadata +2 -2
@@ -3,19 +3,15 @@
|
|
3
3
|
require 'date'
|
4
4
|
|
5
5
|
DateTime.class_eval do
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
utc? ? self : in_time_zone('UTC').to_datetime
|
10
|
-
end
|
6
|
+
# @return [DateTime]
|
7
|
+
def in_utc
|
8
|
+
utc? ? self : in_time_zone('UTC').to_datetime
|
11
9
|
end
|
12
10
|
end
|
13
11
|
|
14
12
|
Time.class_eval do
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
utc? ? self : in_time_zone('UTC')
|
19
|
-
end
|
13
|
+
# @return [Time]
|
14
|
+
def in_utc
|
15
|
+
utc? ? self : in_time_zone('UTC')
|
20
16
|
end
|
21
17
|
end
|
@@ -3,19 +3,15 @@
|
|
3
3
|
require 'date'
|
4
4
|
|
5
5
|
DateTime.class_eval do
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
strftime(all ? '%F %T' : '%F')
|
10
|
-
end
|
6
|
+
# @return [String] - yyyy-mm-dd[Thh:mm::ss]
|
7
|
+
def input(all = false)
|
8
|
+
strftime(all ? '%F %T' : '%F')
|
11
9
|
end
|
12
10
|
end
|
13
11
|
|
14
12
|
Date.class_eval do
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
strftime('%F')
|
19
|
-
end
|
13
|
+
# @return [String] - yyyy-mm-dd
|
14
|
+
def input(_all = false)
|
15
|
+
strftime('%F')
|
20
16
|
end
|
21
17
|
end
|
@@ -1,28 +1,26 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
Array.class_eval do
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
return any?(*meth_args) && !all?(*meth_args) if meth_args.size.positive?
|
4
|
+
# Same as `my_array.any?(&block) && !my_array.all?(&block)`
|
5
|
+
#
|
6
|
+
# @return [Boolean]
|
7
|
+
#
|
8
|
+
def only_some?(*meth_args)
|
9
|
+
# Check arg size since `any?` arg take presence over block
|
10
|
+
return any?(*meth_args) && !all?(*meth_args) if meth_args.size.positive?
|
12
11
|
|
13
|
-
|
14
|
-
|
12
|
+
any = false
|
13
|
+
all = true
|
15
14
|
|
16
|
-
|
17
|
-
|
15
|
+
each do |*args|
|
16
|
+
result = yield(*args)
|
18
17
|
|
19
|
-
|
20
|
-
|
18
|
+
any ||= result
|
19
|
+
all &&= result
|
21
20
|
|
22
|
-
|
23
|
-
end
|
24
|
-
|
25
|
-
return any && !all
|
21
|
+
break if any && !all
|
26
22
|
end
|
23
|
+
|
24
|
+
return any && !all
|
27
25
|
end
|
28
26
|
end
|
@@ -4,18 +4,14 @@ require 'forwardable'
|
|
4
4
|
require 'set'
|
5
5
|
|
6
6
|
Array.class_eval do
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
!(self & Array(other)).empty?
|
11
|
-
end
|
7
|
+
# @return [Boolean]
|
8
|
+
def overlaps?(other)
|
9
|
+
!(self & Array(other)).empty?
|
12
10
|
end
|
13
11
|
end
|
14
12
|
|
15
13
|
Set.class_eval do
|
16
|
-
|
17
|
-
extend Forwardable
|
14
|
+
extend Forwardable
|
18
15
|
|
19
|
-
|
20
|
-
end
|
16
|
+
def_delegators :to_a, :overlaps?
|
21
17
|
end
|
@@ -4,19 +4,15 @@ require 'forwardable'
|
|
4
4
|
require 'set'
|
5
5
|
|
6
6
|
Array.class_eval do
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
map { |a| fns.reduce(a, &:send) }
|
12
|
-
end
|
7
|
+
# Replace my_array.map(&:method1).map(&:method2) with my_array.pipe(:method1, :method2)
|
8
|
+
# @return [Array]
|
9
|
+
def pipe(*fns)
|
10
|
+
map { |a| fns.reduce(a, &:send) }
|
13
11
|
end
|
14
12
|
end
|
15
13
|
|
16
14
|
Set.class_eval do
|
17
|
-
|
18
|
-
extend Forwardable
|
15
|
+
extend Forwardable
|
19
16
|
|
20
|
-
|
21
|
-
end
|
17
|
+
def_delegators :to_a, :pipe
|
22
18
|
end
|
@@ -1,17 +1,15 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
Object.class_eval do
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
raise(RubyRailsExtensions::Errors::BlankObjectError, 'Blank object') if blank?
|
4
|
+
# Returns self if `present?`
|
5
|
+
#
|
6
|
+
# @raise [BlankObject] if `blank?`
|
7
|
+
#
|
8
|
+
# @return [*]
|
9
|
+
#
|
10
|
+
def presence!
|
11
|
+
raise(RubyRailsExtensions::Errors::BlankObjectError, 'Blank object') if blank?
|
13
12
|
|
14
|
-
|
15
|
-
end
|
13
|
+
self
|
16
14
|
end
|
17
15
|
end
|
@@ -1,47 +1,41 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
String.class_eval do
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
quarter_date.beginning_of_quarter
|
11
|
-
end
|
4
|
+
# Return the quarter start date.
|
5
|
+
#
|
6
|
+
# @return [Date]
|
7
|
+
#
|
8
|
+
def quarter_start
|
9
|
+
quarter_date.beginning_of_quarter
|
12
10
|
end
|
13
11
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
quarter_date.end_of_quarter
|
21
|
-
end
|
12
|
+
# Return the quarter end date.
|
13
|
+
#
|
14
|
+
# @return [Date]
|
15
|
+
#
|
16
|
+
def quarter_end
|
17
|
+
quarter_date.end_of_quarter
|
22
18
|
end
|
23
19
|
|
24
20
|
private
|
25
21
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
end
|
22
|
+
# Parses a string for a quarter date. Expects the string to follow: 'qQyy' where
|
23
|
+
# q = 1/2/3/4 and yy to be the last two digits of the year.
|
24
|
+
#
|
25
|
+
# @raise [RubyRailsExtensions::Errors::InvalidFormatError] if `self` is not qQyy
|
26
|
+
#
|
27
|
+
# @return [Date]
|
28
|
+
#
|
29
|
+
def quarter_date
|
30
|
+
unless match?(/\A[1-4]q\d\d\z/i)
|
31
|
+
raise(RubyRailsExtensions::Errors::InvalidFormatError, "Unexpected format (given #{self}, expected qQyy)")
|
32
|
+
end
|
38
33
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
34
|
+
values = scan(/\d/)
|
35
|
+
quarter = values.shift.to_i
|
36
|
+
year = "20#{values.join('')}".to_i
|
37
|
+
month = quarter * 3
|
43
38
|
|
44
|
-
|
45
|
-
end
|
39
|
+
Date.new(year, month, 1)
|
46
40
|
end
|
47
41
|
end
|
@@ -1,47 +1,45 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
Range.class_eval do
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
end
|
22
|
-
|
23
|
-
new_begin, new_end =
|
24
|
-
if other.is_a?(Range)
|
25
|
-
unless other.begin.is_a?(Numeric)
|
26
|
-
raise(
|
27
|
-
RubyRailsExtensions::Errors::NonNumericError,
|
28
|
-
"Wrong range type (given #{other.begin.class}, expected Numeric)"
|
29
|
-
)
|
30
|
-
end
|
4
|
+
# Allows adding a to a range. This will create a new range since ranges are immutable.
|
5
|
+
#
|
6
|
+
# @note Make sure to assign this to a new var or inline replacement `+=`.
|
7
|
+
#
|
8
|
+
# @raise [RubyRailsExtensions::Errors::NonNumericError]
|
9
|
+
#
|
10
|
+
# @param other [Numeric, Range<Numeric>]
|
11
|
+
#
|
12
|
+
# @return [Range]
|
13
|
+
#
|
14
|
+
def +(other)
|
15
|
+
unless self.begin.is_a?(Numeric)
|
16
|
+
raise(
|
17
|
+
RubyRailsExtensions::Errors::NonNumericError,
|
18
|
+
"Wrong range type (given #{self.begin.class}, expected Numeric)"
|
19
|
+
)
|
20
|
+
end
|
31
21
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
22
|
+
new_begin, new_end =
|
23
|
+
if other.is_a?(Range)
|
24
|
+
unless other.begin.is_a?(Numeric)
|
25
|
+
raise(
|
26
|
+
RubyRailsExtensions::Errors::NonNumericError,
|
27
|
+
"Wrong range type (given #{other.begin.class}, expected Numeric)"
|
28
|
+
)
|
29
|
+
end
|
40
30
|
|
41
|
-
|
31
|
+
[self.begin + other.begin, self.end + other.end]
|
32
|
+
else
|
33
|
+
unless other.is_a?(Numeric)
|
34
|
+
raise(
|
35
|
+
RubyRailsExtensions::Errors::NonNumericError,
|
36
|
+
"Wrong type (given #{other.class}, expected Numeric)"
|
37
|
+
)
|
42
38
|
end
|
43
39
|
|
44
|
-
|
45
|
-
|
40
|
+
[self.begin + other, self.end + other]
|
41
|
+
end
|
42
|
+
|
43
|
+
Range.new(new_begin, new_end, exclude_end?)
|
46
44
|
end
|
47
45
|
end
|
@@ -1,47 +1,45 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
Range.class_eval do
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
end
|
22
|
-
|
23
|
-
new_begin, new_end =
|
24
|
-
if other.is_a?(Range)
|
25
|
-
unless other.begin.is_a?(Numeric)
|
26
|
-
raise(
|
27
|
-
RubyRailsExtensions::Errors::NonNumericError,
|
28
|
-
"Wrong range type (given #{other.begin.class}, expected Numeric)"
|
29
|
-
)
|
30
|
-
end
|
4
|
+
# Allows multiplying a range. This will create a new range due to range objects being immutable.
|
5
|
+
#
|
6
|
+
# @note Make sure to assign this to a new var or do inline replacement `*=`.
|
7
|
+
#
|
8
|
+
# @raise [RubyRailsExtensions::Errors::NonNumericError]
|
9
|
+
#
|
10
|
+
# @param other [Numeric, Range<Numeric>]
|
11
|
+
#
|
12
|
+
# @return [Range]
|
13
|
+
#
|
14
|
+
def *(other)
|
15
|
+
unless self.begin.is_a?(Numeric)
|
16
|
+
raise(
|
17
|
+
RubyRailsExtensions::Errors::NonNumericError,
|
18
|
+
"Wrong range type (given #{self.begin.class}, expected Numeric)"
|
19
|
+
)
|
20
|
+
end
|
31
21
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
22
|
+
new_begin, new_end =
|
23
|
+
if other.is_a?(Range)
|
24
|
+
unless other.begin.is_a?(Numeric)
|
25
|
+
raise(
|
26
|
+
RubyRailsExtensions::Errors::NonNumericError,
|
27
|
+
"Wrong range type (given #{other.begin.class}, expected Numeric)"
|
28
|
+
)
|
29
|
+
end
|
40
30
|
|
41
|
-
|
31
|
+
[self.begin * other.begin, self.end * other.end]
|
32
|
+
else
|
33
|
+
unless other.is_a?(Numeric)
|
34
|
+
raise(
|
35
|
+
RubyRailsExtensions::Errors::NonNumericError,
|
36
|
+
"Wrong type (given #{other.class}, expected Numeric)"
|
37
|
+
)
|
42
38
|
end
|
43
39
|
|
44
|
-
|
45
|
-
|
40
|
+
[self.begin * other, self.end * other]
|
41
|
+
end
|
42
|
+
|
43
|
+
Range.new(new_begin, new_end, exclude_end?)
|
46
44
|
end
|
47
45
|
end
|
@@ -2,27 +2,25 @@
|
|
2
2
|
|
3
3
|
if defined?(ActiveJob::Base)
|
4
4
|
ActiveJob::Base.class_eval do
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
next if key.start_with?('_aj')
|
5
|
+
# Removes any reserved keywords that can't be passed along.
|
6
|
+
#
|
7
|
+
# @param arguments [...]
|
8
|
+
#
|
9
|
+
# @return [String, Numeric, Hash, Array, nil]
|
10
|
+
#
|
11
|
+
def remove_reserved_keywords(arguments)
|
12
|
+
case arguments
|
13
|
+
when String, Numeric
|
14
|
+
arguments
|
15
|
+
when Hash
|
16
|
+
arguments.each_with_object({}) do |(key, value), obj|
|
17
|
+
next if key.start_with?('_aj')
|
19
18
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
end
|
19
|
+
obj[key] = remove_reserved_keywords(value)
|
20
|
+
end
|
21
|
+
when Array
|
22
|
+
arguments.map do |arg|
|
23
|
+
remove_reserved_keywords(arg)
|
26
24
|
end
|
27
25
|
end
|
28
26
|
end
|
@@ -2,13 +2,11 @@
|
|
2
2
|
|
3
3
|
Time.class_eval do
|
4
4
|
class << self
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
nil
|
11
|
-
end
|
5
|
+
# Parses the `str` and returns `nil` if invalid
|
6
|
+
def safe_parse(str)
|
7
|
+
parse(str)
|
8
|
+
rescue
|
9
|
+
nil
|
12
10
|
end
|
13
11
|
end
|
14
12
|
end
|
@@ -16,13 +14,11 @@ end
|
|
16
14
|
if defined?(ActiveSupport::TimeZone)
|
17
15
|
ActiveSupport::TimeZone.class_eval do
|
18
16
|
class << self
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
nil
|
25
|
-
end
|
17
|
+
# Parses the `str` and returns `nil` if invalid
|
18
|
+
def safe_parse(str)
|
19
|
+
parse(str)
|
20
|
+
rescue
|
21
|
+
nil
|
26
22
|
end
|
27
23
|
end
|
28
24
|
end
|
@@ -30,13 +26,11 @@ end
|
|
30
26
|
|
31
27
|
Date.class_eval do
|
32
28
|
class << self
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
nil
|
39
|
-
end
|
29
|
+
# Parses the `str` and returns `nil` if invalid
|
30
|
+
def safe_parse(str)
|
31
|
+
parse(str)
|
32
|
+
rescue
|
33
|
+
nil
|
40
34
|
end
|
41
35
|
end
|
42
36
|
end
|
@@ -1,11 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
Enumerable.class_eval do
|
4
|
-
|
5
|
-
alias_method :select_present, :compact_blank
|
6
|
-
end
|
4
|
+
alias_method :select_present, :compact_blank
|
7
5
|
|
8
|
-
|
9
|
-
alias_method :select_present!, :compact_blank!
|
10
|
-
end
|
6
|
+
alias_method :select_present!, :compact_blank!
|
11
7
|
end
|
@@ -1,7 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
Enumerable.class_eval do
|
4
|
-
|
5
|
-
alias_method :select_present_join, :compact_blank_join
|
6
|
-
end
|
4
|
+
alias_method :select_present_join, :compact_blank_join
|
7
5
|
end
|