Skip to content

Commit

Permalink
Add CacheCrispies::Base#transform_keys method
Browse files Browse the repository at this point in the history
  • Loading branch information
wujibear committed Dec 17, 2022
1 parent dea85f8 commit db678c7
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions lib/cache_crispies/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -226,13 +226,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,
with: with,
through: through,
to: to,
Expand All @@ -250,5 +251,25 @@ def self.merge(attribute = nil, with: nil)
serialize(nil, from: attribute, with: with)
end
private_class_method :merge

# 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 if @transform_keys.present?

@transform_keys = proc
end

# 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

0 comments on commit db678c7

Please sign in to comment.