Class: RESTFramework::NativeModelSerializer
- Inherits:
-
BaseSerializer
- Object
- BaseSerializer
- RESTFramework::NativeModelSerializer
- Defined in:
- lib/rest_framework/serializers.rb
Overview
This serializer uses ‘.as_json` to serialize objects. Despite the name, `.as_json` is a Rails method which converts objects to Ruby primitives (with the top-level being either an array or a hash).
Instance Attribute Summary
Attributes inherited from BaseSerializer
Instance Method Summary collapse
-
#_resolve_serializer_config(node: nil) ⇒ Object
Recursive method for traversing a config and evaluating nested serializers.
-
#get_action ⇒ Object
Get controller action, if possible.
-
#get_local_serializer_config ⇒ Object
Get a locally defined configuration, if one is defined.
-
#get_resolved_serializer_config ⇒ Object
Get a serializer config and resolve any nested serializers into configs.
-
#get_serializer_config ⇒ Object
Get a configuration passable to ‘as_json` for the model.
-
#initialize(model: nil, many: nil, **kwargs) ⇒ NativeModelSerializer
constructor
A new instance of NativeModelSerializer.
-
#serialize ⇒ Object
Convert the object(s) to Ruby primitives.
Constructor Details
#initialize(model: nil, many: nil, **kwargs) ⇒ NativeModelSerializer
Returns a new instance of NativeModelSerializer.
21 22 23 24 25 |
# File 'lib/rest_framework/serializers.rb', line 21 def initialize(model: nil, many: nil, **kwargs) super(**kwargs) @many = many @model = model || (@controller ? @controller.send(:get_model) : nil) end |
Instance Method Details
#_resolve_serializer_config(node: nil) ⇒ Object
Recursive method for traversing a config and evaluating nested serializers.
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/rest_framework/serializers.rb', line 72 def _resolve_serializer_config(node: nil) # First base case: found a serializer, so evaluate it and return it. if node.is_a?(Class) && (node < BaseSerializer) return node.new(controller: @controller).get_resolved_serializer_config end # Second base case: found a serializer instance, so evaluate it and return it. if node.is_a?(BaseSerializer) return node.get_resolved_serializer_config end # Third base case: node is not iterable, so return it. unless node.respond_to?(:each) return node end # Recursive case: node is iterable, so iterate and recursively resolve serializers. if node.is_a? Hash node.each do |k,v| node[k] = self._resolve_serializer_config(node: v) end else node.map! do |v| self._resolve_serializer_config(node: v) end end return node end |
#get_action ⇒ Object
Get controller action, if possible.
28 29 30 |
# File 'lib/rest_framework/serializers.rb', line 28 def get_action return @controller&.action_name&.to_sym end |
#get_local_serializer_config ⇒ Object
Get a locally defined configuration, if one is defined.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/rest_framework/serializers.rb', line 33 def get_local_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 return self.plural_config if @many && self.plural_config return self.singular_config if !@many && self.singular_config # lastly, try the default config return self.config end |
#get_resolved_serializer_config ⇒ Object
Get a serializer config and resolve any nested serializers into configs.
103 104 105 106 107 108 |
# File 'lib/rest_framework/serializers.rb', line 103 def get_resolved_serializer_config config = self.get_serializer_config # traverse the config, resolving nested serializers return _resolve_serializer_config(node: config) end |
#get_serializer_config ⇒ Object
Get a configuration passable to ‘as_json` for the model.
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/rest_framework/serializers.rb', line 52 def get_serializer_config # return a locally defined serializer config if one is defined local_config = self.get_local_serializer_config return local_config if local_config # return a serializer config if one is defined serializer_config = @controller.send(:get_native_serializer_config) return serializer_config if serializer_config # otherwise, build a serializer config from fields fields = @controller.send(:get_fields) unless fields.blank? columns, methods = fields.partition { |f| f.to_s.in?(@model.column_names) } return {only: columns, methods: methods} end return {} end |
#serialize ⇒ Object
Convert the object(s) to Ruby primitives.
111 112 113 114 115 116 117 118 119 |
# File 'lib/rest_framework/serializers.rb', line 111 def serialize if @object @many = @object.respond_to?(:each) if @many.nil? return @object.as_json( self.get_resolved_serializer_config ) end return nil end |