mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 07:16:49 +00:00
Support lowerCamel key format
This commit is contained in:
@@ -38,6 +38,11 @@ end
|
||||
WARN
|
||||
end
|
||||
|
||||
def format_keys(format)
|
||||
@key_format = format
|
||||
end
|
||||
attr_reader :key_format
|
||||
|
||||
if RUBY_VERSION >= '2.0'
|
||||
def serializer_for(resource)
|
||||
if resource.respond_to?(:to_ary)
|
||||
@@ -110,6 +115,7 @@ end
|
||||
@wrap_in_array = options[:_wrap_in_array]
|
||||
@only = Array(options[:only]) if options[:only]
|
||||
@except = Array(options[:except]) if options[:except]
|
||||
@key_format = options[:key_format]
|
||||
end
|
||||
attr_accessor :object, :scope, :root, :meta_key, :meta
|
||||
|
||||
@@ -190,10 +196,35 @@ end
|
||||
end
|
||||
end
|
||||
|
||||
def key_format
|
||||
@key_format || self.class.key_format || CONFIG.key_format
|
||||
end
|
||||
|
||||
def format_key(key)
|
||||
if key_format == :lower_camel
|
||||
key.to_s.camelize(:lower)
|
||||
else
|
||||
key
|
||||
end
|
||||
end
|
||||
|
||||
def convert_keys(hash)
|
||||
Hash[hash.map do |k,v|
|
||||
key = if k.is_a?(Symbol)
|
||||
format_key(k).to_sym
|
||||
else
|
||||
format_key(k)
|
||||
end
|
||||
|
||||
[key ,v]
|
||||
end]
|
||||
end
|
||||
|
||||
def serializable_object(options={})
|
||||
return @wrap_in_array ? [] : nil if @object.nil?
|
||||
hash = attributes
|
||||
hash.merge! associations
|
||||
hash = convert_keys(hash) if key_format.present?
|
||||
@wrap_in_array ? [hash] : hash
|
||||
end
|
||||
alias_method :serializable_hash, :serializable_object
|
||||
|
||||
Reference in New Issue
Block a user