forked from rails/webpacker
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
95 additions
and
11 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
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
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,25 @@ | ||
require "active_support/core_ext/enumerable" | ||
require "active_support/core_ext/hash/keys" | ||
|
||
class Hash | ||
def deep_symbolize_keys | ||
deep_transform_keys { |key| key.to_sym rescue key } | ||
end | ||
|
||
def deep_transform_keys(&block) | ||
_deep_transform_keys_in_object(self, &block) | ||
end | ||
|
||
def _deep_transform_keys_in_object(object, &block) | ||
case object | ||
when Hash | ||
object.each_with_object({}) do |(key, value), result| | ||
result[yield(key)] = _deep_transform_keys_in_object(value, &block) | ||
end | ||
when Array | ||
object.map { |e| _deep_transform_keys_in_object(e, &block) } | ||
else | ||
object | ||
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 |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# frozen_string_literal: true | ||
|
||
class String | ||
# Same as +indent+, except it indents the receiver in-place. | ||
# | ||
# Returns the indented string, or +nil+ if there was nothing to indent. | ||
def indent!(amount, indent_string = nil, indent_empty_lines = false) | ||
indent_string = indent_string || self[/^[ \t]/] || " " | ||
re = indent_empty_lines ? /^/ : /^(?!$)/ | ||
gsub!(re, indent_string * amount) | ||
end | ||
|
||
# Indents the lines in the receiver: | ||
# | ||
# <<EOS.indent(2) | ||
# def some_method | ||
# some_code | ||
# end | ||
# EOS | ||
# # => | ||
# def some_method | ||
# some_code | ||
# end | ||
# | ||
# The second argument, +indent_string+, specifies which indent string to | ||
# use. The default is +nil+, which tells the method to make a guess by | ||
# peeking at the first indented line, and fallback to a space if there is | ||
# none. | ||
# | ||
# " foo".indent(2) # => " foo" | ||
# "foo\n\t\tbar".indent(2) # => "\t\tfoo\n\t\t\t\tbar" | ||
# "foo".indent(2, "\t") # => "\t\tfoo" | ||
# | ||
# While +indent_string+ is typically one space or tab, it may be any string. | ||
# | ||
# The third argument, +indent_empty_lines+, is a flag that says whether | ||
# empty lines should be indented. Default is false. | ||
# | ||
# "foo\n\nbar".indent(2) # => " foo\n\n bar" | ||
# "foo\n\nbar".indent(2, nil, true) # => " foo\n \n bar" | ||
# | ||
def indent(amount, indent_string = nil, indent_empty_lines = false) | ||
dup.tap { |_| _.indent!(amount, indent_string, indent_empty_lines) } | ||
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
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
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
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
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
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
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