Class: RESTFramework::NativeSerializer

Inherits:
BaseSerializer show all
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

NativeModelSerializer

Class Method Summary collapse

Instance Method Summary collapse

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
# 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[0].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.



135
136
137
138
139
140
# File 'lib/rest_framework/serializers.rb', line 135

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



141
142
143
144
145
146
# File 'lib/rest_framework/serializers.rb', line 141

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.



121
122
123
124
125
126
# File 'lib/rest_framework/serializers.rb', line 121

def [](key)
  unless instance_variable_defined?(:@_nested_config)
    @_nested_config = self.get_serializer_config
  end
  return @_nested_config[key]
end

#[]=(key, value) ⇒ Object



127
128
129
130
131
132
# File 'lib/rest_framework/serializers.rb', line 127

def []=(key, value)
  unless instance_variable_defined?(:@_nested_config)
    @_nested_config = self.get_serializer_config
  end
  return @_nested_config[key] = value
end

#get_actionObject

Get controller action, if possible.



39
40
41
# File 'lib/rest_framework/serializers.rb', line 39

def get_action
  return @controller&.action_name&.to_sym
end

#get_controller_native_serializer_configObject

Helper to get a native serializer configuration from the controller.



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/rest_framework/serializers.rb', line 63

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_configObject

Get a locally defined native serializer configuration, if one is defined.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/rest_framework/serializers.rb', line 44

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_configObject

Get a configuration passable to ‘serializable_hash` for the object.



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
101
102
# File 'lib/rest_framework/serializers.rb', line 76

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

#serializeObject

Convert the object (record or recordset) to Ruby primitives.



105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/rest_framework/serializers.rb', line 105

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