-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove active support dependency for ood-portal-generator (#1572)
* Remove active support dependency for ood-portal-generator Fixes #1347 * Add comment about RAILS-LICENSE
- Loading branch information
Showing
5 changed files
with
104 additions
and
16 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,20 @@ | ||
Copyright (c) 2005-2021 David Heinemeier Hansson | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining | ||
a copy of this software and associated documentation files (the | ||
"Software"), to deal in the Software without restriction, including | ||
without limitation the rights to use, copy, modify, merge, publish, | ||
distribute, sublicense, and/or sell copies of the Software, and to | ||
permit persons to whom the Software is furnished to do so, subject to | ||
the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be | ||
included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | ||
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
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
81 changes: 81 additions & 0 deletions
81
ood-portal-generator/lib/ood_portal_generator/hash_extensions.rb
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,81 @@ | ||
module OodPortalGenerator | ||
# Some elements have been taken from Rails (https://github.com/rails/rails) | ||
# and it's LICENSE has been added as RAILS-LICENSE in the root directory of this project. | ||
module HashExtensions | ||
refine Hash do | ||
# Returns a hash that includes everything except given keys. | ||
# hash = { a: true, b: false, c: nil } | ||
# hash.except(:c) # => { a: true, b: false } | ||
# hash.except(:a, :b) # => { c: nil } | ||
# hash # => { a: true, b: false, c: nil } | ||
# | ||
# This is useful for limiting a set of parameters to everything but a few known toggles: | ||
# @person.update(params[:person].except(:admin)) | ||
def except(*keys) | ||
slice(*self.keys - keys) | ||
end unless method_defined?(:except) | ||
|
||
# Returns a new hash with all keys converted to symbols, as long as | ||
# they respond to +to_sym+. This includes the keys from the root hash | ||
# and from all nested hashes and arrays. | ||
# | ||
# hash = { 'person' => { 'name' => 'Rob', 'age' => '28' } } | ||
# | ||
# hash.deep_symbolize_keys | ||
# # => {:person=>{:name=>"Rob", :age=>"28"}} | ||
def deep_symbolize_keys | ||
deep_transform_keys { |key| key.to_sym rescue key } | ||
end | ||
|
||
# Returns a new hash with all keys converted by the block operation. | ||
# This includes the keys from the root hash and from all | ||
# nested hashes and arrays. | ||
# | ||
# hash = { person: { name: 'Rob', age: '28' } } | ||
# | ||
# hash.deep_transform_keys{ |key| key.to_s.upcase } | ||
# # => {"PERSON"=>{"NAME"=>"Rob", "AGE"=>"28"}} | ||
def deep_transform_keys(&block) | ||
_deep_transform_keys_in_object(self, &block) | ||
end | ||
|
||
# Destructively converts all keys by using the block operation. | ||
# This includes the keys from the root hash and from all | ||
# nested hashes and arrays. | ||
def deep_transform_keys!(&block) | ||
_deep_transform_keys_in_object!(self, &block) | ||
end | ||
|
||
private | ||
|
||
# Support methods for deep transforming nested hashes and arrays. | ||
def _deep_transform_keys_in_object(object, &block) | ||
case object | ||
when Hash | ||
object.each_with_object(self.class.new) 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 | ||
|
||
def _deep_transform_keys_in_object!(object, &block) | ||
case object | ||
when Hash | ||
object.keys.each do |key| | ||
value = object.delete(key) | ||
object[yield(key)] = _deep_transform_keys_in_object!(value, &block) | ||
end | ||
object | ||
when Array | ||
object.map! { |e| _deep_transform_keys_in_object!(e, &block) } | ||
else | ||
object | ||
end | ||
end | ||
end | ||
end | ||
end |