mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-23 14:29:31 +00:00
34 lines
611 B
Ruby
34 lines
611 B
Ruby
# frozen_string_literal: true
|
|
|
|
module ActiveModel
|
|
class Serializer
|
|
class Fieldset
|
|
def initialize(fields)
|
|
@raw_fields = fields || {}
|
|
end
|
|
|
|
def fields
|
|
@fields ||= parsed_fields
|
|
end
|
|
|
|
def fields_for(type)
|
|
fields[type.singularize.to_sym] || fields[type.pluralize.to_sym]
|
|
end
|
|
|
|
protected
|
|
|
|
attr_reader :raw_fields
|
|
|
|
private
|
|
|
|
def parsed_fields
|
|
if raw_fields.is_a?(Hash)
|
|
raw_fields.each_with_object({}) { |(k, v), h| h[k.to_sym] = v.map(&:to_sym) }
|
|
else
|
|
{}
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|