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
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 55be0ba360ddc75f362e31c5e4dd863f6816b897e3fb722db748698a379e586f
|
4
|
+
data.tar.gz: 879cdcb54384e5927bacbad99714f54c2d737a86f55aac79d4f97d83de658b59
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 868b8f77e7e7ec4d974ac7af0e0f07352362d009ff1ab748868965a072f4c7cafd1ea9ec44ec30a6828f19a7d59a9ec1b4bafd7272a25fde053f7d442809f6ca
|
7
|
+
data.tar.gz: 43d272c9b3bc33ccceef003fc3f4e8be1bb3dd6ec83dd6b42879186e7c52dab68c57b0cfa8b6fe5a5660e756a06c0b2110fab411bed6d1e239b5866c8a310a58
|
data/CHANGELOG.md
CHANGED
@@ -1,6 +1,14 @@
|
|
1
1
|
Unreleased Changes
|
2
2
|
------------------
|
3
3
|
|
4
|
+
2.0.0 (2024-07-26)
|
5
|
+
------------------
|
6
|
+
|
7
|
+
* Possible breaking change:
|
8
|
+
* The check for `method_defined?` was removed because the whole point of this gem is to define the methonds in that
|
9
|
+
block. This means there are some things assumed the developer using this gem already has, from this gem or rails,
|
10
|
+
e.g. `Enumerable#compact_blank_join` relies on `Enumerable#compact_blank`
|
11
|
+
|
4
12
|
1.1.1 (2024-03-01)
|
5
13
|
------------------
|
6
14
|
|
@@ -1,67 +1,62 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
end
|
3
|
+
# requires `ActiveModel::AttributeAssignment` and `Hash#stringify_keys`
|
4
|
+
module ActiveModel
|
5
|
+
module AttributeAssignment
|
6
|
+
# Allows you to set nil attributes by passing in a hash of attributes with
|
7
|
+
# keys matching the attribute names.
|
8
|
+
#
|
9
|
+
# @raise [ActiveModel::ForbiddenAttributesError] If the passed hash responds to `permitted?` method
|
10
|
+
# and the return value of this method is +false+.
|
11
|
+
#
|
12
|
+
# @example
|
13
|
+
#
|
14
|
+
# class Cat
|
15
|
+
# include ActiveModel::AttributeAssignment
|
16
|
+
# attr_accessor :name, :status
|
17
|
+
# end
|
18
|
+
#
|
19
|
+
# cat = Cat.new
|
20
|
+
# cat.assign_nil_attributes(name: "Gorby")
|
21
|
+
# cat.name # => 'Gorby'
|
22
|
+
# cat.status # => nil
|
23
|
+
# cat.assign_nil_attributes(status: "yawning")
|
24
|
+
# cat.name # => 'Gorby'
|
25
|
+
# cat.status # => 'yawning'
|
26
|
+
# cat.assign_nil_attributes(status: "sleeping")
|
27
|
+
# cat.name # => 'Gorby'
|
28
|
+
# cat.status # => 'yawning'
|
29
|
+
#
|
30
|
+
# @param new_attributes
|
31
|
+
#
|
32
|
+
# @return [void]
|
33
|
+
#
|
34
|
+
def assign_nil_attributes(new_attributes)
|
35
|
+
unless new_attributes.respond_to?(:stringify_keys)
|
36
|
+
raise(ArgumentError, 'When assigning attributes, you must pass a hash as an argument.')
|
37
|
+
end
|
39
38
|
|
40
|
-
|
39
|
+
return if new_attributes.empty?
|
41
40
|
|
42
|
-
|
41
|
+
attributes = new_attributes.stringify_keys
|
43
42
|
|
44
|
-
|
45
|
-
|
46
|
-
end
|
43
|
+
_assign_nil_attributes(sanitize_for_mass_assignment(attributes))
|
44
|
+
end
|
47
45
|
|
48
|
-
|
46
|
+
private
|
49
47
|
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
next unless v.nil?
|
48
|
+
# Assign any `nil` attribute
|
49
|
+
#
|
50
|
+
# @param attributes [Hash]
|
51
|
+
#
|
52
|
+
# @return [void]
|
53
|
+
#
|
54
|
+
def _assign_nil_attributes(attributes)
|
55
|
+
attributes.each do |k, v|
|
56
|
+
next unless v.nil?
|
60
57
|
|
61
|
-
|
62
|
-
end
|
63
|
-
end
|
58
|
+
_assign_attribute(k, v)
|
64
59
|
end
|
65
|
-
|
60
|
+
end
|
66
61
|
end
|
67
62
|
end
|
@@ -1,41 +1,33 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
String.class_eval do
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
replace(encode('UTF-8', invalid: :replace, undef: :replace, replace: replace_with))
|
13
|
-
end
|
4
|
+
# Remove all non-UTF-8 chars from a string.
|
5
|
+
#
|
6
|
+
# @param replace_with [String, nil] What to replace invalid chars with.
|
7
|
+
#
|
8
|
+
# @return [String]
|
9
|
+
#
|
10
|
+
def clean!(replace_with = '')
|
11
|
+
replace(encode('UTF-8', invalid: :replace, undef: :replace, replace: replace_with))
|
14
12
|
end
|
15
13
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
dup.clean!(replace_with)
|
20
|
-
end
|
14
|
+
# @see {#clean!}
|
15
|
+
def clean(replace_with = '')
|
16
|
+
dup.clean!(replace_with)
|
21
17
|
end
|
22
18
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
replace(encode('UTF-8', 'binary', invalid: :replace, undef: :replace, replace: replace_with))
|
32
|
-
end
|
19
|
+
# Remove all non-UTF-8 chars from a string and convert to binary.
|
20
|
+
#
|
21
|
+
# @param replace_with [String, nil] What to replace invalid chars with.
|
22
|
+
#
|
23
|
+
# @return [String]
|
24
|
+
#
|
25
|
+
def clean_binary!(replace_with = '')
|
26
|
+
replace(encode('UTF-8', 'binary', invalid: :replace, undef: :replace, replace: replace_with))
|
33
27
|
end
|
34
28
|
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
dup.clean_binary!(replace_with)
|
39
|
-
end
|
29
|
+
# @see {#clean_binary!}
|
30
|
+
def clean_binary(replace_with = '')
|
31
|
+
dup.clean_binary!(replace_with)
|
40
32
|
end
|
41
33
|
end
|
@@ -1,13 +1,11 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
Enumerable.class_eval do
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
compact_blank.join(using)
|
11
|
-
end
|
4
|
+
# Joins the elements together after calling `compact_blank`
|
5
|
+
#
|
6
|
+
# @return [String]
|
7
|
+
#
|
8
|
+
def compact_blank_join(using = '')
|
9
|
+
compact_blank.join(using)
|
12
10
|
end
|
13
11
|
end
|
@@ -4,26 +4,22 @@ require 'forwardable'
|
|
4
4
|
require 'set'
|
5
5
|
|
6
6
|
Array.class_eval do
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
return to_enum(:compact_map) unless block_given?
|
7
|
+
# @return [Array]
|
8
|
+
# Similar to `filter_map` but excludes `nil` instead of falsy
|
9
|
+
def compact_map
|
10
|
+
return to_enum(:compact_map) unless block_given?
|
12
11
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
end
|
18
|
-
r
|
12
|
+
r = []
|
13
|
+
each do |*args|
|
14
|
+
result = yield(*args)
|
15
|
+
r.push(result) unless result.nil?
|
19
16
|
end
|
17
|
+
r
|
20
18
|
end
|
21
19
|
end
|
22
20
|
|
23
21
|
Set.class_eval do
|
24
|
-
|
25
|
-
extend Forwardable
|
22
|
+
extend Forwardable
|
26
23
|
|
27
|
-
|
28
|
-
end
|
24
|
+
def_delegators :to_a, :compact_map
|
29
25
|
end
|
@@ -3,19 +3,15 @@
|
|
3
3
|
require 'date'
|
4
4
|
|
5
5
|
DateTime.class_eval do
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
beginning_of_month..end_of_month
|
10
|
-
end
|
6
|
+
# :nodoc:
|
7
|
+
def current_month
|
8
|
+
beginning_of_month..end_of_month
|
11
9
|
end
|
12
10
|
end
|
13
11
|
|
14
12
|
Date.class_eval do
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
beginning_of_month..end_of_month
|
19
|
-
end
|
13
|
+
# :nodoc:
|
14
|
+
def current_month
|
15
|
+
beginning_of_month..end_of_month
|
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
|
-
beginning_of_week..end_of_week
|
10
|
-
end
|
6
|
+
# @return Range<DateTime>
|
7
|
+
def current_week
|
8
|
+
beginning_of_week..end_of_week
|
11
9
|
end
|
12
10
|
end
|
13
11
|
|
14
12
|
Date.class_eval do
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
beginning_of_week..end_of_week
|
19
|
-
end
|
13
|
+
# @return Range<Date>
|
14
|
+
def current_week
|
15
|
+
beginning_of_week..end_of_week
|
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('%A')
|
10
|
-
end
|
6
|
+
# @return [String] - Full day word
|
7
|
+
def day_of_week
|
8
|
+
strftime('%A')
|
11
9
|
end
|
12
10
|
end
|
13
11
|
|
14
12
|
Date.class_eval do
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
strftime('%A')
|
19
|
-
end
|
13
|
+
# @return [String] - Full day word
|
14
|
+
def day_of_week
|
15
|
+
strftime('%A')
|
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 ? '%m/%d/%Y %T' : '%-m/%-d/%y %T')
|
10
|
-
end
|
6
|
+
# @return [String]
|
7
|
+
def display_military(all = false)
|
8
|
+
strftime(all ? '%m/%d/%Y %T' : '%-m/%-d/%y %T')
|
11
9
|
end
|
12
10
|
end
|
13
11
|
|
14
12
|
Time.class_eval do
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
to_datetime.display_military(all)
|
19
|
-
end
|
13
|
+
# @return [String]
|
14
|
+
def display_military(all = false)
|
15
|
+
to_datetime.display_military(all)
|
20
16
|
end
|
21
17
|
end
|
@@ -3,28 +3,22 @@
|
|
3
3
|
require 'date'
|
4
4
|
|
5
5
|
DateTime.class_eval do
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
strftime(all ? '%m/%d/%Y %r' : '%-m/%-d/%y %r')
|
10
|
-
end
|
6
|
+
# @return [String] - mm/dd/yyyy HH:MM:SS AM/PM or m/d/yy HH:MM:SS AM/PM
|
7
|
+
def display_user(all = false)
|
8
|
+
strftime(all ? '%m/%d/%Y %r' : '%-m/%-d/%y %r')
|
11
9
|
end
|
12
10
|
end
|
13
11
|
|
14
12
|
Time.class_eval do
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
to_datetime.display_user(all)
|
19
|
-
end
|
13
|
+
# @return [String] - mm/dd/yyyy or m/d/yy
|
14
|
+
def display_user(all = false)
|
15
|
+
to_datetime.display_user(all)
|
20
16
|
end
|
21
17
|
end
|
22
18
|
|
23
19
|
Date.class_eval do
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
strftime(all ? '%m/%d/%Y' : '%-m/%-d/%y')
|
28
|
-
end
|
20
|
+
# @return [String] - mm/dd/yyyy or m/d/yy
|
21
|
+
def display_user(all = false)
|
22
|
+
strftime(all ? '%m/%d/%Y' : '%-m/%-d/%y')
|
29
23
|
end
|
30
24
|
end
|
@@ -4,21 +4,17 @@ require 'forwardable'
|
|
4
4
|
require 'set'
|
5
5
|
|
6
6
|
Array.class_eval do
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
raise(RubyRailsExtensions::Errors::NotFoundError, 'Not found') if x.nil?
|
7
|
+
# :nodoc:
|
8
|
+
def find!(&block)
|
9
|
+
x = find(&block)
|
10
|
+
raise(RubyRailsExtensions::Errors::NotFoundError, 'Not found') if x.nil?
|
12
11
|
|
13
|
-
|
14
|
-
end
|
12
|
+
x
|
15
13
|
end
|
16
14
|
end
|
17
15
|
|
18
16
|
Set.class_eval do
|
19
|
-
|
20
|
-
extend Forwardable
|
17
|
+
extend Forwardable
|
21
18
|
|
22
|
-
|
23
|
-
end
|
19
|
+
def_delegators :to_a, :find!
|
24
20
|
end
|
@@ -1,16 +1,14 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
Array.class_eval do
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
results.uniq!
|
4
|
+
# @param threshold [Integer] default 1
|
5
|
+
#
|
6
|
+
# @return [Array] excluding items that appear more than `threshold` times
|
7
|
+
#
|
8
|
+
def find_dupes(threshold = 1)
|
9
|
+
results = select { |item| count(item) > threshold }
|
10
|
+
results.uniq!
|
12
11
|
|
13
|
-
|
14
|
-
end
|
12
|
+
results
|
15
13
|
end
|
16
14
|
end
|
@@ -1,11 +1,9 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
Array.class_eval do
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
find { |item| count(item) > threshold }
|
9
|
-
end
|
4
|
+
# @param threshold [Integer] default 1
|
5
|
+
# @return [*] first items that appears more than `threshold` times
|
6
|
+
def first_dupe(threshold = 1)
|
7
|
+
find { |item| count(item) > threshold }
|
10
8
|
end
|
11
9
|
end
|
@@ -3,10 +3,8 @@
|
|
3
3
|
require 'date'
|
4
4
|
|
5
5
|
DateTime.class_eval do
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
strftime('%FT%T%z')
|
10
|
-
end
|
6
|
+
# @return [String] yyyy-mm-ddThh:mm:ss+hhmm
|
7
|
+
def google_format
|
8
|
+
strftime('%FT%T%z')
|
11
9
|
end
|
12
10
|
end
|
@@ -1,27 +1,23 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
Hash.class_eval do
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
slice(*keys)
|
15
|
-
end
|
4
|
+
# The opposite of +Hash.except+.
|
5
|
+
# ~/.rvm/gems/ruby-2.1.2/gems/activesupport-4.2.5/lib/active_support/core_ext/hash/except.rb
|
6
|
+
# https://apidock.com/rails/Hash/except
|
7
|
+
#
|
8
|
+
# Selects only the supplied keys.
|
9
|
+
#
|
10
|
+
# http://api.rubyonrails.org/classes/Hash.html#method-i-slice
|
11
|
+
#
|
12
|
+
def only(*keys)
|
13
|
+
slice(*keys)
|
16
14
|
end
|
17
15
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
slice!(*keys)
|
25
|
-
end
|
16
|
+
# See +only+
|
17
|
+
#
|
18
|
+
# http://apidock.com/rails/v4.1.8/Hash/extract%21
|
19
|
+
#
|
20
|
+
def only!(*keys)
|
21
|
+
slice!(*keys)
|
26
22
|
end
|
27
23
|
end
|