Skip to content

Commit

Permalink
Rework core extensions to reconcile class definitions with directory …
Browse files Browse the repository at this point in the history
…structure
  • Loading branch information
moveson committed Dec 22, 2024
1 parent d6bba2c commit 3b734d3
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 93 deletions.
28 changes: 28 additions & 0 deletions lib/core_ext/action_view/helpers/url_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# frozen_string_literal: true

module CoreExt
module UrlHelperExtensions
alias original_link_to link_to
# Monkey Patch link_to to translate the boolean attribute "disabled" into a class
# Using the signature (*args, &blocks) makes this patch resistant to API changes
# since all args are captured as is
def link_to(*args, &block)
# Locate first hash in args with :disabled, and modify in place
options = args.find do |hash|
hash.key?(:disabled)
rescue StandardError
false
end
options[:class] = [options[:class], "disabled"].join(" ") if options && options[:disabled]
original_link_to(*args, &block) # Call original link_to
end
end
end

module ActionView
module Helpers
module UrlHelper
include CoreExt::UrlHelperExtensions
end
end
end
62 changes: 35 additions & 27 deletions lib/core_ext/array.rb
Original file line number Diff line number Diff line change
@@ -1,37 +1,45 @@
class Array
def average
sum / size.to_f
end
# frozen_string_literal: true

def elements_before(index_element, inclusive: false)
i = index(index_element)
return [] unless i
module CoreExt
module ArrayExtensions
def average
sum / size.to_f
end

i += 1 if inclusive
self[0, i]
end
def elements_before(index_element, inclusive: false)
i = index(index_element)
return [] unless i

def elements_after(index_element, inclusive: false)
i = index(index_element)
return [] unless i
i += 1 if inclusive
self[0, i]
end

i -= 1 if inclusive
self[(i + 1)..-1]
end
def elements_after(index_element, inclusive: false)
i = index(index_element)
return [] unless i

def element_before(index_element)
elements_before(index_element).last
end
i -= 1 if inclusive
self[(i + 1)..-1]
end

def element_after(index_element)
elements_after(index_element).first
end
def element_before(index_element)
elements_before(index_element).last
end

def included_before?(index_element, subject_element)
elements_before(index_element).include?(subject_element)
end
def element_after(index_element)
elements_after(index_element).first
end

def included_before?(index_element, subject_element)
elements_before(index_element).include?(subject_element)
end

def included_after?(index_element, subject_element)
elements_after(index_element).include?(subject_element)
def included_after?(index_element, subject_element)
elements_after(index_element).include?(subject_element)
end
end
end

class Array
include CoreExt::ArrayExtensions
end
58 changes: 33 additions & 25 deletions lib/core_ext/enumerable.rb
Original file line number Diff line number Diff line change
@@ -1,34 +1,42 @@
module Enumerable
def count_by(&block)
Hash[group_by(&block).map { |k, v| [k, v.size] }]
end
# frozen_string_literal: true

def count_each
count_by { |e| e }
end
module CoreExt
module EnumerableExtensions
def count_by(&block)
Hash[group_by(&block).map { |k, v| [k, v.size] }]
end

def count_each
count_by { |e| e }
end

def each_with_iteration
Enumerator.new do |y|
iteration = 1
enum = cycle
loop do
enum.peek # raises StopIteration if self.empty?
size.times do
e = [enum.next, iteration]
y << (block_given? ? yield(e) : e)
def each_with_iteration
Enumerator.new do |y|
iteration = 1
enum = cycle
loop do
enum.peek # raises StopIteration if self.empty?
size.times do
e = [enum.next, iteration]
y << (block_given? ? yield(e) : e)
end
iteration += 1
end
iteration += 1
end
end
end

def group_by_equality(&block)
result = {}
each do |element|
proposed_key = block.call(element)
existing_key = result.keys.find { |key| key == proposed_key }
existing_key ? result[existing_key] << element : result[proposed_key] = [element]
def group_by_equality(&block)
result = {}
each do |element|
proposed_key = block.call(element)
existing_key = result.keys.find { |key| key == proposed_key }
existing_key ? result[existing_key] << element : result[proposed_key] = [element]
end
result
end
result
end
end

module Enumerable
include CoreExt::EnumerableExtensions
end
26 changes: 17 additions & 9 deletions lib/core_ext/numeric.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
class Numeric
def round_to_nearest(rounding_quotient = 0)
if rounding_quotient.zero?
round
else
(self / rounding_quotient.to_f).round * rounding_quotient
# frozen_string_literal: true

module CoreExt
module NumericExtensions
def round_to_nearest(rounding_quotient = 0)
if rounding_quotient.zero?
round
else
(self / rounding_quotient.to_f).round * rounding_quotient
end
end
end

def numericize # Parallel to String#numericize
self
def numericize # Parallel to String#numericize
self
end
end
end

class Numeric
include CoreExt::NumericExtensions
end
33 changes: 21 additions & 12 deletions lib/core_ext/string.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
class String
def numeric?
true if Float(self)
rescue StandardError
false
end
# frozen_string_literal: true

def numericize
gsub(/[^\d.]/, "").to_f
end
module CoreExt
module StringExtensions
def numeric?
true if Float(self)
rescue StandardError
false
end

def numericize
gsub(/[^\d.]/, "").to_f
end

def to_boolean
ActiveRecord::Type::Boolean.new.cast(self)
def to_boolean
ActiveRecord::Type::Boolean.new.cast(self)
end

alias to_bool to_boolean
end
alias to_bool to_boolean
end

class String
include CoreExt::StringExtensions
end
20 changes: 0 additions & 20 deletions lib/core_ext/url_helper.rb

This file was deleted.

0 comments on commit 3b734d3

Please sign in to comment.