ruby-rails-extensions 0.1.0

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 +33 -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 +26 -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 +106 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: d56af69b3c8476fe02a20b1b9afd927f80e8ac40ff659e5e6654ee590d7ce15e
4
+ data.tar.gz: 1ce72548d0d1d98c1c77b45c7cf417858bd1b0388a3fd71e6468b00a9c9e8c88
5
+ SHA512:
6
+ metadata.gz: '08c4e8c6951e77ebdf318385fcf6d9e490260786e297455e7b5dfc804672659234aba622eeab15d864cb261f1744e9d15bb3ec184e11f4b4ac3694f2cc14fda1'
7
+ data.tar.gz: bba958f49013cca6f9fcee9bbada3f31c9f8b9a36577aeec19751d393a71e0e857332fe851403f5b0dc189170896d125de4eb6ccd0855d111909adf14c8d262e
data/CHANGELOG.md ADDED
@@ -0,0 +1,8 @@
1
+ Unreleased Changes
2
+ ------------------
3
+
4
+ 0.1.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,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'forwardable'
4
+ require 'set'
5
+
6
+ module RubyRailsExtensions
7
+ module Extensions
8
+ module FindBang
9
+ class NotFoundError < StandardError
10
+ end
11
+ end
12
+ end
13
+ end
14
+
15
+ Array.class_eval do
16
+ unless method_defined?(:find!)
17
+ # :nodoc:
18
+ def find!(&block)
19
+ x = find(&block)
20
+ raise(RubyRailsExtensions::Extensions::FindBang::NotFoundError, 'Not found') if x.nil?
21
+
22
+ x
23
+ end
24
+ end
25
+ end
26
+
27
+ Set.class_eval do
28
+ unless method_defined?(:find!)
29
+ extend Forwardable
30
+
31
+ def_delegators :to_a, :find!
32
+ end
33
+ 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,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyRailsExtensions
4
+ module Extensions
5
+ module PresenceBang
6
+ class BlankObjectError < StandardError
7
+ end
8
+ end
9
+ end
10
+ end
11
+
12
+ Object.class_eval do
13
+ unless method_defined?(:presence!)
14
+ # Returns self if `present?`
15
+ #
16
+ # @raise [BlankObject] if `blank?`
17
+ #
18
+ # @return [*]
19
+ #
20
+ def presence!
21
+ raise(RubyRailsExtensions::Extensions::PresenceBang::BlankObjectError, 'Blank object') if blank?
22
+
23
+ self
24
+ end
25
+ end
26
+ 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 = '0.1.0'
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
+ next unless configuration.public_send("#{flag_name(gem_base)}?")
15
+
16
+ require_relative("ruby-rails-extensions/extensions/#{gem_base}")
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,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby-rails-extensions
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Brands Insurance
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-09-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rubocop
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '='
18
+ - !ruby/object:Gem::Version
19
+ version: 1.28.2
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '='
25
+ - !ruby/object:Gem::Version
26
+ version: 1.28.2
27
+ - !ruby/object:Gem::Dependency
28
+ name: rubocop-performance
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '='
32
+ - !ruby/object:Gem::Version
33
+ version: 1.13.3
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '='
39
+ - !ruby/object:Gem::Version
40
+ version: 1.13.3
41
+ description:
42
+ email:
43
+ - documents@brandsinsurance.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - CHANGELOG.md
49
+ - LICENSE.txt
50
+ - lib/ruby-rails-extensions.rb
51
+ - lib/ruby-rails-extensions/extensions/compact_map.rb
52
+ - lib/ruby-rails-extensions/extensions/current_day.rb
53
+ - lib/ruby-rails-extensions/extensions/current_month.rb
54
+ - lib/ruby-rails-extensions/extensions/current_week.rb
55
+ - lib/ruby-rails-extensions/extensions/day_of_week.rb
56
+ - lib/ruby-rails-extensions/extensions/display_military.rb
57
+ - lib/ruby-rails-extensions/extensions/display_user.rb
58
+ - lib/ruby-rails-extensions/extensions/find_bang.rb
59
+ - lib/ruby-rails-extensions/extensions/find_dupes.rb
60
+ - lib/ruby-rails-extensions/extensions/first_dupe.rb
61
+ - lib/ruby-rails-extensions/extensions/google_format.rb
62
+ - lib/ruby-rails-extensions/extensions/hash_only.rb
63
+ - lib/ruby-rails-extensions/extensions/in_utc.rb
64
+ - lib/ruby-rails-extensions/extensions/input.rb
65
+ - lib/ruby-rails-extensions/extensions/month_and_year.rb
66
+ - lib/ruby-rails-extensions/extensions/month_year.rb
67
+ - lib/ruby-rails-extensions/extensions/overlaps.rb
68
+ - lib/ruby-rails-extensions/extensions/pipe.rb
69
+ - lib/ruby-rails-extensions/extensions/presence_bang.rb
70
+ - lib/ruby-rails-extensions/extensions/remove_reserved_keywords.rb
71
+ - lib/ruby-rails-extensions/extensions/to_bool.rb
72
+ - lib/ruby-rails-extensions/extensions/to_dec.rb
73
+ - lib/ruby-rails-extensions/extensions/to_i.rb
74
+ - lib/ruby-rails-extensions/extensions/to_or_sentence.rb
75
+ - lib/ruby-rails-extensions/extensions/to_sort_i.rb
76
+ - lib/ruby-rails-extensions/extensions/to_x.rb
77
+ - lib/ruby-rails-extensions/extensions/usd_to_f.rb
78
+ - lib/ruby-rails-extensions/extensions/usd_to_i.rb
79
+ - lib/ruby-rails-extensions/extensions/yesno.rb
80
+ - lib/ruby-rails-extensions/version.rb
81
+ homepage: https://github.com/BrandsInsurance/rails-extensions
82
+ licenses:
83
+ - MIT
84
+ metadata:
85
+ rubygems_mfa_required: 'true'
86
+ source_code_uri: https://github.com/BrandsInsurance/rails-extensions
87
+ post_install_message:
88
+ rdoc_options: []
89
+ require_paths:
90
+ - lib
91
+ required_ruby_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: 2.5.3
96
+ required_rubygems_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ requirements: []
102
+ rubygems_version: 3.0.3
103
+ signing_key:
104
+ specification_version: 4
105
+ summary: Loads custom methods on various ruby classes
106
+ test_files: []