-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rework core extensions to reconcile class definitions with directory …
…structure
- Loading branch information
Showing
6 changed files
with
134 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file was deleted.
Oops, something went wrong.