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.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/rest_framework/serializers.rb', line 17

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.



131
132
133
134
135
136
# File 'lib/rest_framework/serializers.rb', line 131

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



137
138
139
140
141
142
# File 'lib/rest_framework/serializers.rb', line 137

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.



117
118
119
120
121
122
# File 'lib/rest_framework/serializers.rb', line 117

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

#[]=(key, value) ⇒ Object



123
124
125
126
127
128
# File 'lib/rest_framework/serializers.rb', line 123

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.



35
36
37
# File 'lib/rest_framework/serializers.rb', line 35

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.



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/rest_framework/serializers.rb', line 59

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.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/rest_framework/serializers.rb', line 40

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.



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

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.



101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/rest_framework/serializers.rb', line 101

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