ruby-rails-extensions 1.0.0.pre.rc.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (35) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +8 -0
  3. data/LICENSE.txt +21 -0
  4. data/lib/ruby-rails-extensions/extensions/compact_map.rb +29 -0
  5. data/lib/ruby-rails-extensions/extensions/current_day.rb +12 -0
  6. data/lib/ruby-rails-extensions/extensions/current_month.rb +21 -0
  7. data/lib/ruby-rails-extensions/extensions/current_week.rb +21 -0
  8. data/lib/ruby-rails-extensions/extensions/day_of_week.rb +21 -0
  9. data/lib/ruby-rails-extensions/extensions/display_military.rb +21 -0
  10. data/lib/ruby-rails-extensions/extensions/display_user.rb +30 -0
  11. data/lib/ruby-rails-extensions/extensions/find_bang.rb +29 -0
  12. data/lib/ruby-rails-extensions/extensions/find_dupes.rb +16 -0
  13. data/lib/ruby-rails-extensions/extensions/first_dupe.rb +11 -0
  14. data/lib/ruby-rails-extensions/extensions/google_format.rb +12 -0
  15. data/lib/ruby-rails-extensions/extensions/hash_only.rb +27 -0
  16. data/lib/ruby-rails-extensions/extensions/in_utc.rb +21 -0
  17. data/lib/ruby-rails-extensions/extensions/input.rb +21 -0
  18. data/lib/ruby-rails-extensions/extensions/month_and_year.rb +12 -0
  19. data/lib/ruby-rails-extensions/extensions/month_year.rb +12 -0
  20. data/lib/ruby-rails-extensions/extensions/overlaps.rb +21 -0
  21. data/lib/ruby-rails-extensions/extensions/pipe.rb +22 -0
  22. data/lib/ruby-rails-extensions/extensions/presence_bang.rb +22 -0
  23. data/lib/ruby-rails-extensions/extensions/remove_reserved_keywords.rb +30 -0
  24. data/lib/ruby-rails-extensions/extensions/to_bool.rb +47 -0
  25. data/lib/ruby-rails-extensions/extensions/to_dec.rb +28 -0
  26. data/lib/ruby-rails-extensions/extensions/to_i.rb +19 -0
  27. data/lib/ruby-rails-extensions/extensions/to_or_sentence.rb +21 -0
  28. data/lib/ruby-rails-extensions/extensions/to_sort_i.rb +22 -0
  29. data/lib/ruby-rails-extensions/extensions/to_x.rb +28 -0
  30. data/lib/ruby-rails-extensions/extensions/usd_to_f.rb +28 -0
  31. data/lib/ruby-rails-extensions/extensions/usd_to_i.rb +19 -0
  32. data/lib/ruby-rails-extensions/extensions/yesno.rb +28 -0
  33. data/lib/ruby-rails-extensions/version.rb +5 -0
  34. data/lib/ruby-rails-extensions.rb +91 -0
  35. metadata +98 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 63384b5d08dc21bee31ea0efa671be2f67158db5170ae6befc67e807bb327165
4
+ data.tar.gz: 59aae08f32d2792062dd41fcc92fbe625f48084453e12e5b890af6797ce09129
5
+ SHA512:
6
+ metadata.gz: 34f78b9aeb8c96af2d67801c1ab7e1b833993703f6f90293558df03dc646e129f8eb7b4b9937116f77dbcda3e154fc1c513bf2ea42350053674fa89e271bcc0d
7
+ data.tar.gz: c19b9648f700e4b5b380c970d018a389cdd74f2de5c84f0eb94da8677048e8214f028763f9ce88672700a52b9c7ed63738925089c0f06c0cf1cac02d64b8bf7c
data/CHANGELOG.md ADDED
@@ -0,0 +1,8 @@
1
+ Unreleased Changes
2
+ ------------------
3
+
4
+ 1.0.0 (2023-09-25)
5
+ ------------------
6
+
7
+ * Initial release
8
+ * Migration from `rails_extensions_core` and ~30 sub-gems to `ruby-rails-extensions` with configurations
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Brands Insurance
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'forwardable'
4
+ require 'set'
5
+
6
+ Array.class_eval do
7
+ unless method_defined?(:compact_map)
8
+ # @return [Array]
9
+ # Similar to `filter_map` but excludes `nil` instead of falsy
10
+ def compact_map
11
+ return to_enum(:compact_map) unless block_given?
12
+
13
+ r = []
14
+ each do |*args|
15
+ result = yield(*args)
16
+ r.push(result) unless result.nil?
17
+ end
18
+ r
19
+ end
20
+ end
21
+ end
22
+
23
+ Set.class_eval do
24
+ unless method_defined?(:compact_map)
25
+ extend Forwardable
26
+
27
+ def_delegators :to_a, :compact_map
28
+ end
29
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'date'
4
+
5
+ DateTime.class_eval do
6
+ unless method_defined?(:current_day)
7
+ # :nodoc:
8
+ def current_day
9
+ beginning_of_day..end_of_day
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'date'
4
+
5
+ Date.class_eval do
6
+ unless method_defined?(:current_month)
7
+ # :nodoc:
8
+ def current_month
9
+ beginning_of_month..end_of_month
10
+ end
11
+ end
12
+ end
13
+
14
+ DateTime.class_eval do
15
+ unless method_defined?(:current_month)
16
+ # :nodoc:
17
+ def current_month
18
+ beginning_of_month..end_of_month
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'date'
4
+
5
+ Date.class_eval do
6
+ unless method_defined?(:current_week)
7
+ # @return Range<Date>
8
+ def current_week
9
+ beginning_of_week..end_of_week
10
+ end
11
+ end
12
+ end
13
+
14
+ DateTime.class_eval do
15
+ unless method_defined?(:current_week)
16
+ # @return Range<Date>
17
+ def current_week
18
+ beginning_of_week..end_of_week
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'date'
4
+
5
+ Date.class_eval do
6
+ unless method_defined?(:day_of_week)
7
+ # @return [String] - Full day word
8
+ def day_of_week
9
+ strftime('%A')
10
+ end
11
+ end
12
+ end
13
+
14
+ DateTime.class_eval do
15
+ unless method_defined?(:day_of_week)
16
+ # @return [String] - Full day word
17
+ def day_of_week
18
+ strftime('%A')
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'date'
4
+
5
+ DateTime.class_eval do
6
+ unless method_defined?(:display_military)
7
+ # @return [String]
8
+ def display_military(all = false)
9
+ strftime(all ? '%m/%d/%Y %T' : '%-m/%-d/%y %T')
10
+ end
11
+ end
12
+ end
13
+
14
+ Time.class_eval do
15
+ unless method_defined?(:display_military)
16
+ # @return [String]
17
+ def display_military(all = false)
18
+ to_datetime.display_military(all)
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'date'
4
+
5
+ Date.class_eval do
6
+ unless method_defined?(:display_user)
7
+ # @return [String] - mm/dd/yyyy or m/d/yy
8
+ def display_user(all = false)
9
+ strftime(all ? '%m/%d/%Y' : '%-m/%-d/%y')
10
+ end
11
+ end
12
+ end
13
+
14
+ DateTime.class_eval do
15
+ unless method_defined?(:display_user)
16
+ # @return [String] - mm/dd/yyyy or m/d/yy
17
+ def display_user(all = false)
18
+ strftime(all ? '%m/%d/%Y %r' : '%-m/%-d/%y %r')
19
+ end
20
+ end
21
+ end
22
+
23
+ Time.class_eval do
24
+ unless method_defined?(:display_user)
25
+ # @return [String] - mm/dd/yyyy or m/d/yy
26
+ def display_user(all = false)
27
+ to_datetime.display_user(all)
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'forwardable'
4
+ require 'set'
5
+
6
+ module FindBangExtension
7
+ class NotFoundError < StandardError
8
+ end
9
+ end
10
+
11
+ Array.class_eval do
12
+ unless method_defined?(:find!)
13
+ # :nodoc:
14
+ def find!(&block)
15
+ x = find(&block)
16
+ raise(FindBangExtension::NotFoundError, 'Not found') if x.nil?
17
+
18
+ x
19
+ end
20
+ end
21
+ end
22
+
23
+ Set.class_eval do
24
+ unless method_defined?(:find!)
25
+ extend Forwardable
26
+
27
+ def_delegators :to_a, :find!
28
+ end
29
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ Array.class_eval do
4
+ unless method_defined?(:find_dupes)
5
+ # @param threshold [Integer] default 1
6
+ #
7
+ # @return [Array] excluding items that appear more than `threshold` times
8
+ #
9
+ def find_dupes(threshold = 1)
10
+ results = select { |item| count(item) > threshold }
11
+ results.uniq!
12
+
13
+ results
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ Array.class_eval do
4
+ unless method_defined?(:first_dupe)
5
+ # @param threshold [Integer] default 1
6
+ # @return [*] first items that appears more than `threshold` times
7
+ def first_dupe(threshold = 1)
8
+ find { |item| count(item) > threshold }
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'date'
4
+
5
+ DateTime.class_eval do
6
+ unless method_defined?(:google_format)
7
+ # @return [String] yyyy-mm-ddThh:mm:ss+hhmm
8
+ def google_format
9
+ strftime('%FT%T%z')
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ Hash.class_eval do
4
+ unless method_defined?(:only)
5
+ # The opposite of +Hash.except+.
6
+ # ~/.rvm/gems/ruby-2.1.2/gems/activesupport-4.2.5/lib/active_support/core_ext/hash/except.rb
7
+ # https://apidock.com/rails/Hash/except
8
+ #
9
+ # Selects only the supplied keys.
10
+ #
11
+ # http://api.rubyonrails.org/classes/Hash.html#method-i-slice
12
+ #
13
+ def only(*keys)
14
+ slice(*keys)
15
+ end
16
+ end
17
+
18
+ unless method_defined?(:only!)
19
+ # See +only+
20
+ #
21
+ # http://apidock.com/rails/v4.1.8/Hash/extract%21
22
+ #
23
+ def only!(*keys)
24
+ slice!(*keys)
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'date'
4
+
5
+ DateTime.class_eval do
6
+ unless method_defined?(:in_utc)
7
+ # @return [DateTime]
8
+ def in_utc
9
+ utc? ? self : in_time_zone('UTC').to_datetime
10
+ end
11
+ end
12
+ end
13
+
14
+ Time.class_eval do
15
+ unless method_defined?(:in_utc)
16
+ # @return [Time]
17
+ def in_utc
18
+ utc? ? self : in_time_zone('UTC')
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'date'
4
+
5
+ Date.class_eval do
6
+ unless method_defined?(:input)
7
+ # @return [String] - yyyy-mm-dd
8
+ def input(_all = false)
9
+ strftime('%F')
10
+ end
11
+ end
12
+ end
13
+
14
+ DateTime.class_eval do
15
+ unless method_defined?(:input)
16
+ # @return [String] - yyyy-mm-dd[Thh:mm::ss]
17
+ def input(all = false)
18
+ strftime(all ? '%F %T' : '%F')
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'date'
4
+
5
+ Date.class_eval do
6
+ unless method_defined?(:month_and_year)
7
+ # @return [String] - mm/yy
8
+ def month_and_year
9
+ strftime('%m/%y')
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'date'
4
+
5
+ Date.class_eval do
6
+ unless method_defined?(:month_year)
7
+ # @return [String] - yyyy-mm
8
+ def month_year
9
+ input[0, 7]
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'forwardable'
4
+ require 'set'
5
+
6
+ Array.class_eval do
7
+ unless method_defined?(:overlaps?)
8
+ # @return [Boolean]
9
+ def overlaps?(other)
10
+ !(self & Array(other)).empty?
11
+ end
12
+ end
13
+ end
14
+
15
+ Set.class_eval do
16
+ unless method_defined?(:overlaps?)
17
+ extend Forwardable
18
+
19
+ def_delegators :to_a, :overlaps?
20
+ end
21
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'forwardable'
4
+ require 'set'
5
+
6
+ Array.class_eval do
7
+ unless method_defined?(:pipe)
8
+ # Replace my_array.map(&:method1).map(&:method2) with my_array.pipe(:method1, :method2)
9
+ # @return [Array]
10
+ def pipe(*fns)
11
+ map { |a| fns.reduce(a, &:send) }
12
+ end
13
+ end
14
+ end
15
+
16
+ Set.class_eval do
17
+ unless method_defined?(:pipe)
18
+ extend Forwardable
19
+
20
+ def_delegators :to_a, :pipe
21
+ end
22
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PresenceBangExtension
4
+ class BlankObjectError < StandardError
5
+ end
6
+ end
7
+
8
+ Object.class_eval do
9
+ unless method_defined?(:presence!)
10
+ # Returns self if `present?`
11
+ #
12
+ # @raise [BlankObject] if `blank?`
13
+ #
14
+ # @return [*]
15
+ #
16
+ def presence!
17
+ raise(PresenceBangExtension::BlankObjectError, 'Blank object') if blank?
18
+
19
+ self
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ if defined?(ActiveJob::Base)
4
+ ActiveJob::Base.class_eval do
5
+ unless method_defined?(:remove_reserved_keywords)
6
+ # Removes any reserved keywords that can't be passed along.
7
+ #
8
+ # @param arguments [...]
9
+ #
10
+ # @return [String, Numeric, Hash, Array, nil]
11
+ #
12
+ def remove_reserved_keywords(arguments)
13
+ case arguments
14
+ when String, Numeric
15
+ arguments
16
+ when Hash
17
+ arguments.each_with_object({}) do |(key, value), obj|
18
+ next if key.start_with?('_aj')
19
+
20
+ obj[key] = remove_reserved_keywords(value)
21
+ end
22
+ when Array
23
+ arguments.map do |arg|
24
+ remove_reserved_keywords(arg)
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'forwardable'
4
+
5
+ TrueClass.class_eval do
6
+ unless method_defined?(:to_bool)
7
+ # :nodoc:
8
+ def to_bool
9
+ true
10
+ end
11
+ end
12
+ end
13
+
14
+ FalseClass.class_eval do
15
+ unless method_defined?(:to_bool)
16
+ # :nodoc:
17
+ def to_bool
18
+ false
19
+ end
20
+ end
21
+ end
22
+
23
+ NilClass.class_eval do
24
+ unless method_defined?(:to_bool)
25
+ # :nodoc:
26
+ def to_bool
27
+ false
28
+ end
29
+ end
30
+ end
31
+
32
+ String.class_eval do
33
+ unless method_defined?(:to_bool)
34
+ # :nodoc:
35
+ def to_bool
36
+ match?(/^(true|1|t|y|yes)$/i)
37
+ end
38
+ end
39
+ end
40
+
41
+ Numeric.class_eval do
42
+ unless method_defined?(:to_bool)
43
+ extend Forwardable
44
+
45
+ def_delegators :to_s, :to_bool
46
+ end
47
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ NilClass.class_eval do
4
+ unless method_defined?(:to_dec)
5
+ # :nodoc:
6
+ def to_dec(*_args)
7
+ nil
8
+ end
9
+ end
10
+ end
11
+
12
+ Numeric.class_eval do
13
+ unless method_defined?(:to_dec)
14
+ # :nodoc:
15
+ def to_dec(p = 4)
16
+ (to_f / 100.0).round(p)
17
+ end
18
+ end
19
+ end
20
+
21
+ String.class_eval do
22
+ unless method_defined?(:to_dec)
23
+ # :nodoc:
24
+ def to_dec(p = 4)
25
+ (to_f / 100.0).round(p)
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ TrueClass.class_eval do
4
+ unless method_defined?(:to_i)
5
+ # :nodoc:
6
+ def to_i
7
+ 1
8
+ end
9
+ end
10
+ end
11
+
12
+ FalseClass.class_eval do
13
+ unless method_defined?(:to_i)
14
+ # :nodoc:
15
+ def to_i
16
+ 0
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'forwardable'
4
+ require 'set'
5
+
6
+ Array.class_eval do
7
+ unless method_defined?(:to_or_sentence)
8
+ # @return [String]
9
+ def to_or_sentence
10
+ to_sentence(two_words_connector: ' or ', last_word_connector: ', or ')
11
+ end
12
+ end
13
+ end
14
+
15
+ Set.class_eval do
16
+ unless method_defined?(:to_or_sentence)
17
+ extend Forwardable
18
+
19
+ def_delegators :to_a, :to_or_sentence
20
+ end
21
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ TrueClass.class_eval do
4
+ unless method_defined?(:to_sort_i)
5
+ alias_method :to_sort_i, :to_i
6
+ end
7
+ end
8
+
9
+ FalseClass.class_eval do
10
+ unless method_defined?(:to_sort_i)
11
+ alias_method :to_sort_i, :to_i
12
+ end
13
+ end
14
+
15
+ NilClass.class_eval do
16
+ unless method_defined?(:to_sort_i)
17
+ # :nodoc:
18
+ def to_sort_i
19
+ -1
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ TrueClass.class_eval do
4
+ unless method_defined?(:to_x)
5
+ # :nodoc:
6
+ def to_x(*_args)
7
+ 'X'
8
+ end
9
+ end
10
+ end
11
+
12
+ FalseClass.class_eval do
13
+ unless method_defined?(:to_x)
14
+ # :nodoc:
15
+ def to_x(unless_true = '')
16
+ unless_true
17
+ end
18
+ end
19
+ end
20
+
21
+ NilClass.class_eval do
22
+ unless method_defined?(:to_x)
23
+ # :nodoc:
24
+ def to_x(unless_true = nil)
25
+ unless_true
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ NilClass.class_eval do
4
+ unless method_defined?(:usd_to_f)
5
+ # :nodoc:
6
+ def usd_to_f
7
+ nil
8
+ end
9
+ end
10
+ end
11
+
12
+ Numeric.class_eval do
13
+ unless method_defined?(:usd_to_f)
14
+ # :nodoc:
15
+ def usd_to_f
16
+ to_f
17
+ end
18
+ end
19
+ end
20
+
21
+ String.class_eval do
22
+ unless method_defined?(:usd_to_f)
23
+ # :nodoc:
24
+ def usd_to_f
25
+ gsub(/[$,]/, '').to_f
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ NilClass.class_eval do
4
+ unless method_defined?(:usd_to_i)
5
+ # :nodoc:
6
+ def usd_to_i
7
+ nil
8
+ end
9
+ end
10
+ end
11
+
12
+ String.class_eval do
13
+ unless method_defined?(:usd_to_i)
14
+ # :nodoc:
15
+ def usd_to_i
16
+ gsub(/[$,]/, '').to_i
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ TrueClass.class_eval do
4
+ unless method_defined?(:yesno)
5
+ # :nodoc:
6
+ def yesno
7
+ 'Yes'
8
+ end
9
+ end
10
+ end
11
+
12
+ FalseClass.class_eval do
13
+ unless method_defined?(:yesno)
14
+ # :nodoc:
15
+ def yesno
16
+ 'No'
17
+ end
18
+ end
19
+ end
20
+
21
+ NilClass.class_eval do
22
+ unless method_defined?(:yesno)
23
+ # :nodoc:
24
+ def yesno
25
+ 'N/A'
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyRailsExtensions
4
+ VERSION = '1.0.0-rc.1'
5
+ end
@@ -0,0 +1,91 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyRailsExtensions
4
+ # @return [CONFIGURATION]
5
+ def self.configuration
6
+ @configuration ||= CONFIGURATION.new
7
+ end
8
+
9
+ # @yield [Configuration]
10
+ def self.configure
11
+ yield(configuration)
12
+
13
+ gem_list.each do |gem_base|
14
+ if configuration.public_send("#{flag_name(gem_base)}?")
15
+ require_relative("ruby-rails-extensions/extensions/#{gem_base}")
16
+ end
17
+ end
18
+ end
19
+
20
+ # @return [Array<Symbol>]
21
+ def configuration_flags
22
+ CONFIGURATION.members
23
+ end
24
+
25
+ module_function :configuration_flags
26
+
27
+ private
28
+
29
+ # @return [Symbol] the flag name
30
+ def flag_name(gem_base)
31
+ :"include_#{gem_base}"
32
+ end
33
+
34
+ module_function :flag_name
35
+
36
+ BOOLEAN_GEMS = %i[
37
+ compact_map
38
+ current_day
39
+ current_month
40
+ current_week
41
+ day_of_week
42
+ display_military
43
+ display_user
44
+ find_bang
45
+ find_dupes
46
+ first_dupe
47
+ google_format
48
+ hash_only
49
+ in_utc
50
+ input
51
+ month_and_year
52
+ month_year
53
+ overlaps
54
+ pipe
55
+ presence_bang
56
+ remove_reserved_keywords
57
+ to_bool
58
+ to_dec
59
+ to_i
60
+ to_or_sentence
61
+ to_sort_i
62
+ to_x
63
+ usd_to_f
64
+ usd_to_i
65
+ yesno
66
+ ].freeze
67
+
68
+ # @return [Array<Symbol>] the list of gems
69
+ def gem_list
70
+ BOOLEAN_GEMS
71
+ end
72
+
73
+ module_function :gem_list
74
+
75
+ CONFIGURATION = Struct.new(*gem_list.map { |gem| flag_name(gem) }) do
76
+ members.each do |gem_member|
77
+ define_method(:"#{gem_member}?") do
78
+ public_send(gem_member) != false
79
+ end
80
+ end
81
+
82
+ # Sets all flags to true by default
83
+ def initialize
84
+ super
85
+
86
+ members.each do |gem_member|
87
+ public_send("#{gem_member}=", true)
88
+ end
89
+ end
90
+ end
91
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby-rails-extensions
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0.pre.rc.1
5
+ platform: ruby
6
+ authors:
7
+ - Brands Insurance
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-09-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rubocop-rubomatic-rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 1.3.0
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '2.0'
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: 1.3.0
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '2.0'
33
+ description:
34
+ email:
35
+ - documents@brandsinsurance.com
36
+ executables: []
37
+ extensions: []
38
+ extra_rdoc_files: []
39
+ files:
40
+ - CHANGELOG.md
41
+ - LICENSE.txt
42
+ - lib/ruby-rails-extensions.rb
43
+ - lib/ruby-rails-extensions/extensions/compact_map.rb
44
+ - lib/ruby-rails-extensions/extensions/current_day.rb
45
+ - lib/ruby-rails-extensions/extensions/current_month.rb
46
+ - lib/ruby-rails-extensions/extensions/current_week.rb
47
+ - lib/ruby-rails-extensions/extensions/day_of_week.rb
48
+ - lib/ruby-rails-extensions/extensions/display_military.rb
49
+ - lib/ruby-rails-extensions/extensions/display_user.rb
50
+ - lib/ruby-rails-extensions/extensions/find_bang.rb
51
+ - lib/ruby-rails-extensions/extensions/find_dupes.rb
52
+ - lib/ruby-rails-extensions/extensions/first_dupe.rb
53
+ - lib/ruby-rails-extensions/extensions/google_format.rb
54
+ - lib/ruby-rails-extensions/extensions/hash_only.rb
55
+ - lib/ruby-rails-extensions/extensions/in_utc.rb
56
+ - lib/ruby-rails-extensions/extensions/input.rb
57
+ - lib/ruby-rails-extensions/extensions/month_and_year.rb
58
+ - lib/ruby-rails-extensions/extensions/month_year.rb
59
+ - lib/ruby-rails-extensions/extensions/overlaps.rb
60
+ - lib/ruby-rails-extensions/extensions/pipe.rb
61
+ - lib/ruby-rails-extensions/extensions/presence_bang.rb
62
+ - lib/ruby-rails-extensions/extensions/remove_reserved_keywords.rb
63
+ - lib/ruby-rails-extensions/extensions/to_bool.rb
64
+ - lib/ruby-rails-extensions/extensions/to_dec.rb
65
+ - lib/ruby-rails-extensions/extensions/to_i.rb
66
+ - lib/ruby-rails-extensions/extensions/to_or_sentence.rb
67
+ - lib/ruby-rails-extensions/extensions/to_sort_i.rb
68
+ - lib/ruby-rails-extensions/extensions/to_x.rb
69
+ - lib/ruby-rails-extensions/extensions/usd_to_f.rb
70
+ - lib/ruby-rails-extensions/extensions/usd_to_i.rb
71
+ - lib/ruby-rails-extensions/extensions/yesno.rb
72
+ - lib/ruby-rails-extensions/version.rb
73
+ homepage: https://github.com/BrandsInsurance/rails-extensions
74
+ licenses:
75
+ - MIT
76
+ metadata:
77
+ rubygems_mfa_required: 'true'
78
+ source_code_uri: https://github.com/BrandsInsurance/rails-extensions
79
+ post_install_message:
80
+ rdoc_options: []
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: 3.0.1
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">"
91
+ - !ruby/object:Gem::Version
92
+ version: 1.3.1
93
+ requirements: []
94
+ rubygems_version: 3.2.15
95
+ signing_key:
96
+ specification_version: 4
97
+ summary: Loads custom methods on various ruby classes
98
+ test_files: []