-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Add optional recursive camelization of keys. #428
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 @@ | ||
module ActiveModel | ||
module Convertable | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
class_attribute :_root_proc, :_key_proc | ||
|
||
self._key_proc = ->(original) { original } | ||
self._root_proc = ->(original) { original } | ||
end | ||
|
||
module ClassMethods | ||
def camelize_keys!(first_letter = :lower) | ||
self._key_proc = ->(original){ camelize_keys original, first_letter } | ||
self._root_proc = ->(original){ original.to_s.camelize(first_letter) } | ||
end | ||
|
||
def convert_keys(&proc) | ||
self._key_proc = proc | ||
end | ||
|
||
def camelize_keys(original, first_letter = :lower) | ||
original.each_with_object({}) do |(key,value), hash| | ||
hash.merge!(key.to_s.camelize(first_letter) => (value.is_a?(Hash) ? camelize_keys(value, first_letter) : value)) | ||
end | ||
end | ||
|
||
def _convert_root(root) | ||
_root_proc.call(root) | ||
end | ||
|
||
def _convert_keys(original = {}) | ||
_key_proc.call(original) | ||
end | ||
end | ||
|
||
def _convert_root(root) | ||
_root_proc.call(root) | ||
end | ||
|
||
def _convert_keys(original = {}) | ||
_key_proc.call(original) | ||
end | ||
end | ||
end |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,62 @@ | ||
require 'test_helper' | ||
def MiniTest.filter_backtrace(bt) | ||
bt | ||
end | ||
|
||
module ActiveModel | ||
class Serializer | ||
class CamelCaseLowerTest < ActiveModel::TestCase | ||
def setup | ||
@camel_case = CamelCase.new({ key_one: 'Name 1', key_two: 'Name 2' }) | ||
@camel_case_serializer = CamelCaseSerializer.new(@camel_case) | ||
@camel_case_serializer.class_eval do | ||
camelize_keys! | ||
end | ||
end | ||
|
||
def test_attributes_definition | ||
assert_equal([:key_one, :key_two], | ||
@camel_case_serializer.class._attributes) | ||
end | ||
|
||
def test_convert_keys_using_serializable_hash | ||
assert_equal({ | ||
'keyOne' => 'Name 1', 'keyTwo' => 'Name 2' | ||
}, @camel_case_serializer.serializable_hash) | ||
end | ||
|
||
def test_convert_keys_using_as_json | ||
assert_equal({ | ||
'camelCase' => { 'keyOne' => 'Name 1', 'keyTwo' => 'Name 2' } | ||
}, @camel_case_serializer.as_json) | ||
end | ||
end | ||
|
||
class CamelCaseUpperTest < ActiveModel::TestCase | ||
def setup | ||
@camel_case = CamelCase.new({ key_one: 'Name 1', key_two: 'Name 2' }) | ||
@camel_case_serializer = CamelCaseSerializer.new(@camel_case) | ||
@camel_case_serializer.class_eval do | ||
camelize_keys! :upper | ||
end | ||
end | ||
|
||
def test_attributes_definition | ||
assert_equal([:key_one, :key_two], | ||
@camel_case_serializer.class._attributes) | ||
end | ||
|
||
def test_convert_keys_using_serializable_hash | ||
assert_equal({ | ||
'KeyOne' => 'Name 1', 'KeyTwo' => 'Name 2' | ||
}, @camel_case_serializer.serializable_hash) | ||
end | ||
|
||
def test_convert_keys_using_as_json | ||
assert_equal({ | ||
'CamelCase' => { 'KeyOne' => 'Name 1', 'KeyTwo' => 'Name 2' } | ||
}, @camel_case_serializer.as_json) | ||
end | ||
end | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this needed? ^^^
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not really, I can do
def self.included
if you want?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should just include Convertable, there's no need to use that hook