-
Notifications
You must be signed in to change notification settings - Fork 15
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
Add CacheCrispies::Base#transform_keys method #55
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -193,6 +193,16 @@ def self.file_hashes | |
).uniq.sort | ||
end | ||
|
||
# Sets a method or Proc to transform keys by | ||
# | ||
# @param proc [Proc] a Proc to execute on serializable keys | ||
# @return [Proc] a Proc to execute on the keys | ||
def self.transform_keys(proc = nil) | ||
return @transform_keys ||= proc if proc.present? || @transform_keys.present? | ||
|
||
self.superclass.transform_keys if self.superclass.respond_to?(:transform_keys) | ||
end | ||
|
||
private | ||
|
||
def self.file_hash | ||
|
@@ -241,13 +251,14 @@ def self.serialize( | |
) | ||
attribute_names.flat_map do |attrib| | ||
attrib = attrib&.to_sym | ||
key = attrib | ||
current_nesting = Array(@nesting).dup | ||
current_conditions = Array(@conditions).dup | ||
|
||
@attributes << | ||
Attribute.new( | ||
attrib, | ||
from: from, | ||
transform_key(attrib), | ||
from: from || attrib, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not great at seeing GH notifications, sorry. I'll need to get back in the code here to recall correctly, but I believe this was meant as an improvement. |
||
with: with, | ||
through: through, | ||
to: to, | ||
|
@@ -265,5 +276,15 @@ def self.merge(attribute = nil, with: nil) | |
serialize(nil, from: attribute, with: with) | ||
end | ||
private_class_method :merge | ||
|
||
# Transforms an attribute key using a specified Proc | ||
# | ||
# @param key [String] the key for an attribute | ||
# @return [String] a transformed key | ||
def self.transform_key(key) | ||
return key if transform_keys.blank? | ||
|
||
transform_keys.call(key) | ||
end | ||
end | ||
end |
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.
Could we use a block here rather than a lambda argument?
It just reads a bit better and is a bit more consistent to me to have
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.
It looks like you just repeated the line I have there... accident yes?
You're meaning to have something like this instead?