Class: RESTFramework::NativeSerializer
- Inherits:
-
BaseSerializer
- Object
- BaseSerializer
- RESTFramework::NativeSerializer
- Defined in:
- lib/rest_framework/serializers.rb
Overview
This serializer uses ‘.serializable_hash` to convert objects to Ruby primitives (with the top-level being either an array or a hash).
Direct Known Subclasses
Instance Attribute Summary
Attributes inherited from BaseSerializer
Class Method Summary collapse
-
.[](key) ⇒ Object
Allow a serializer class to be used as a hash directly in a nested serializer config.
- .[]=(key, value) ⇒ Object
-
.filter_subconfig(subconfig, except, additive: false) ⇒ Object
Helper to filter (mutate) a single subconfig for specific keys.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Allow a serializer instance to be used as a hash directly in a nested serializer config.
- #[]=(key, value) ⇒ Object
-
#_get_raw_serializer_config ⇒ Object
Get the raw serializer config.
-
#filter_except(config) ⇒ Object
Helper to filter out configuration properties based on the :except query parameter.
-
#get_action ⇒ Object
Get controller action, if possible.
-
#get_controller_native_serializer_config ⇒ Object
Helper to get a native serializer configuration from the controller.
-
#get_local_native_serializer_config ⇒ Object
Get a locally defined native serializer configuration, if one is defined.
-
#get_serializer_config ⇒ Object
Get a configuration passable to ‘serializable_hash` for the object, filtered if required.
-
#initialize(object = nil, many: nil, model: nil, **kwargs) ⇒ NativeSerializer
constructor
A new instance of NativeSerializer.
- #serialize(**kwargs) ⇒ Object
Methods inherited from BaseSerializer
Constructor Details
#initialize(object = nil, many: nil, model: nil, **kwargs) ⇒ NativeSerializer
Returns a new instance of NativeSerializer.
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/rest_framework/serializers.rb', line 30 def initialize(object=nil, many: nil, model: nil, **kwargs) super(object, **kwargs) if many.nil? # Determine if we are dealing with many objects or just one. @many = @object.is_a?(Enumerable) else @many = many end # Determine model either explicitly, or by inspecting @object or @controller. @model = model @model ||= @object.class if @object.is_a?(ActiveRecord::Base) @model ||= @object[0].class if @many && @object.is_a?(Enumerable) && @object.is_a?(ActiveRecord::Base) @model ||= @controller.send(:get_model) if @controller end |
Class Method Details
.[](key) ⇒ Object
Allow a serializer class to be used as a hash directly in a nested serializer config.
194 195 196 197 |
# File 'lib/rest_framework/serializers.rb', line 194 def self.[](key) @_nested_config ||= self.new.get_serializer_config return @_nested_config[key] end |
.[]=(key, value) ⇒ Object
199 200 201 202 |
# File 'lib/rest_framework/serializers.rb', line 199 def self.[]=(key, value) @_nested_config ||= self.new.get_serializer_config return @_nested_config[key] = value end |
.filter_subconfig(subconfig, except, additive: false) ⇒ Object
Helper to filter (mutate) a single subconfig for specific keys.
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/rest_framework/serializers.rb', line 87 def self.filter_subconfig(subconfig, except, additive: false) return subconfig if !subconfig && !additive if subconfig.is_a?(Array) subconfig = subconfig.map(&:to_sym) if additive # Only add fields which are not already included. subconfig += except - subconfig else subconfig -= except end elsif subconfig.is_a?(Hash) # Additive doesn't make sense in a hash context since we wouldn't know the values. unless additive subconfig.symbolize_keys! subconfig.reject! { |k, _v| k.in?(except) } end elsif !subconfig else # Subconfig is a single element (assume string/symbol). subconfig = subconfig.to_sym if subconfig.in?(except) subconfig = [] unless additive elsif additive subconfig = [subconfig, *except] end end return subconfig end |
Instance Method Details
#[](key) ⇒ Object
Allow a serializer instance to be used as a hash directly in a nested serializer config.
183 184 185 186 |
# File 'lib/rest_framework/serializers.rb', line 183 def [](key) @_nested_config ||= self.get_serializer_config return @_nested_config[key] end |
#[]=(key, value) ⇒ Object
188 189 190 191 |
# File 'lib/rest_framework/serializers.rb', line 188 def []=(key, value) @_nested_config ||= self.get_serializer_config return @_nested_config[key] = value end |
#_get_raw_serializer_config ⇒ Object
Get the raw serializer config.
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/rest_framework/serializers.rb', line 141 def _get_raw_serializer_config # Return a locally defined serializer config if one is defined. if local_config = self.get_local_native_serializer_config return local_config end # Return a serializer config if one is defined on the controller. if serializer_config = self.get_controller_native_serializer_config return serializer_config end # If the config wasn't determined, build a serializer config from model fields. fields = @controller.send(:get_fields) if @controller if fields if @model columns, methods = fields.partition { |f| f.in?(@model.column_names) } else columns = fields methods = [] end return {only: columns, methods: methods} end # By default, pass an empty configuration, allowing the serialization of all columns. return {} end |
#filter_except(config) ⇒ Object
Helper to filter out configuration properties based on the :except query parameter.
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/rest_framework/serializers.rb', line 118 def filter_except(config) return config unless @controller except_query_param = @controller.class.try(:native_serializer_except_query_param) if except = @controller.request.query_parameters[except_query_param].presence except = except.split(",").map(&:strip).map(&:to_sym) unless except.empty? # Duplicate the config to avoid mutating class state. config = config.deep_dup # Filter `only`, `except` (additive), `include`, and `methods`. config[:only] = self.class.filter_subconfig(config[:only], except) config[:except] = self.class.filter_subconfig(config[:except], except, additive: true) config[:include] = self.class.filter_subconfig(config[:include], except) config[:methods] = self.class.filter_subconfig(config[:methods], except) end end return config end |
#get_action ⇒ Object
Get controller action, if possible.
50 51 52 |
# File 'lib/rest_framework/serializers.rb', line 50 def get_action return @controller&.action_name&.to_sym end |
#get_controller_native_serializer_config ⇒ Object
Helper to get a native serializer configuration from the controller.
74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/rest_framework/serializers.rb', line 74 def get_controller_native_serializer_config return nil unless @controller if @many == true controller_serializer = @controller.class.try(:native_serializer_plural_config) elsif @many == false controller_serializer = @controller.class.try(:native_serializer_singular_config) end return controller_serializer || @controller.class.try(:native_serializer_config) end |
#get_local_native_serializer_config ⇒ Object
Get a locally defined native serializer configuration, if one is defined.
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/rest_framework/serializers.rb', line 55 def get_local_native_serializer_config action = self.get_action if action && self.action_config # Index action should use :list serializer config if :index is not provided. action = :list if action == :index && !self.action_config.key?(:index) return self.action_config[action] if self.action_config[action] end # No action_config, so try singular/plural config if explicitly instructed to via @many. return self.plural_config if @many == true && self.plural_config return self.singular_config if @many == false && self.singular_config # Lastly, try returning the default config, or singular/plural config in that order. return self.config || self.singular_config || self.plural_config end |
#get_serializer_config ⇒ Object
Get a configuration passable to ‘serializable_hash` for the object, filtered if required.
170 171 172 |
# File 'lib/rest_framework/serializers.rb', line 170 def get_serializer_config return filter_except(self._get_raw_serializer_config) end |
#serialize(**kwargs) ⇒ Object
174 175 176 177 178 179 180 |
# File 'lib/rest_framework/serializers.rb', line 174 def serialize(**kwargs) if @object.respond_to?(:to_ary) return @object.map { |r| r.serializable_hash(self.get_serializer_config) } end return @object.serializable_hash(self.get_serializer_config) end |