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
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
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_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_hashfor the object. -
#initialize(many: nil, model: nil, **kwargs) ⇒ NativeSerializer
constructor
A new instance of NativeSerializer.
-
#serialize ⇒ Object
Convert the object (record or recordset) to Ruby primitives.
Constructor Details
#initialize(many: nil, model: nil, **kwargs) ⇒ NativeSerializer
Returns a new instance of NativeSerializer.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/rest_framework/serializers.rb', line 21 def initialize(many: nil, model: nil, **kwargs) super(**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.
137 138 139 140 141 142 |
# File 'lib/rest_framework/serializers.rb', line 137 def self.[](key) unless instance_variable_defined?(:@_nested_config) @_nested_config = self.new.get_serializer_config end return @_nested_config[key] end |
.[]=(key, value) ⇒ Object
143 144 145 146 147 148 |
# File 'lib/rest_framework/serializers.rb', line 143 def self.[]=(key, value) unless instance_variable_defined?(:@_nested_config) @_nested_config = self.new.get_serializer_config end return @_nested_config[key] = value end |
Instance Method Details
#[](key) ⇒ Object
Allow a serializer instance to be used as a hash directly in a nested serializer config.
123 124 125 126 127 128 |
# File 'lib/rest_framework/serializers.rb', line 123 def [](key) unless instance_variable_defined?(:@_nested_config) @_nested_config = self.get_serializer_config end return @_nested_config[key] end |
#[]=(key, value) ⇒ Object
129 130 131 132 133 134 |
# File 'lib/rest_framework/serializers.rb', line 129 def []=(key, value) unless instance_variable_defined?(:@_nested_config) @_nested_config = self.get_serializer_config end return @_nested_config[key] = value end |
#get_action ⇒ Object
Get controller action, if possible.
41 42 43 |
# File 'lib/rest_framework/serializers.rb', line 41 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.
65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/rest_framework/serializers.rb', line 65 def get_controller_native_serializer_config return nil unless @controller if @many == true controller_serializer = @controller.try(:native_serializer_plural_config) elsif @many == false controller_serializer = @controller.try(:native_serializer_singular_config) end return controller_serializer || @controller.try(:native_serializer_config) end |
#get_local_native_serializer_config ⇒ Object
Get a locally defined native serializer configuration, if one is defined.
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/rest_framework/serializers.rb', line 46 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.
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/rest_framework/serializers.rb', line 78 def get_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 = 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 |
#serialize ⇒ Object
Convert the object (record or recordset) to Ruby primitives.
107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/rest_framework/serializers.rb', line 107 def serialize if @object begin if @object.is_a?(Enumerable) return @object.map { |r| r.serializable_hash(self.get_serializer_config) } end return @object.serializable_hash(self.get_serializer_config) rescue NoMethodError end end # Raise an error if we cannot serialize the object. raise RESTFramework::UnserializableError.new(@object) end |