Class: FastSerializer::Schema

Inherits:
Object
  • Object
show all
Defined in:
lib/fast_serializer/schema.rb,
lib/fast_serializer/schema/mixin.rb

Defined Under Namespace

Modules: Mixin

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}, root = nil, strict = nil) ⇒ Schema

Returns a new instance of Schema.



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/fast_serializer/schema.rb', line 9

def initialize(params = {}, root = nil, strict = nil)
  @root                   = root
  @strict                 = strict || FastSerializer.config.strict
  @serialization_schema   = FastSerializer::JsonModel::Object.new
  @params                 = FastSerializer::Utils.symbolize_keys(params || {})
  @params[:self]          = self
  @params[:include_index] = {}
  @params[:exclude_index] = {}

  self.include = @params.delete(:include)
  self.exclude = @params.delete(:exclude)
end

Instance Attribute Details

#_rootObject (readonly)

Returns the value of attribute _root.



7
8
9
# File 'lib/fast_serializer/schema.rb', line 7

def _root
  @_root
end

#paramsObject (readonly)

Returns the value of attribute params.



7
8
9
# File 'lib/fast_serializer/schema.rb', line 7

def params
  @params
end

#serialization_schemaObject (readonly)

Returns the value of attribute serialization_schema.



7
8
9
# File 'lib/fast_serializer/schema.rb', line 7

def serialization_schema
  @serialization_schema
end

#strictObject (readonly)

Returns the value of attribute strict.



7
8
9
# File 'lib/fast_serializer/schema.rb', line 7

def strict
  @strict
end

Instance Method Details

#attribute(attribute_name, opts = {}, &block) ⇒ Object

Defines an attribute for serialization

Parameters:

  • attribute_name (String, Symbol)

    an attribute name

  • opts (Hash) (defaults to: {})

    attribute options

  • block (Proc)

    result is used as the attribute value

Options Hash (opts):

  • :if (Proc)

    conditional clause. accepts a proc/lambda which has to return a boolean

  • :unless (Proc) — default: see opts:if


59
60
61
62
63
64
65
66
# File 'lib/fast_serializer/schema.rb', line 59

def attribute(attribute_name, opts = {}, &block)
  method = (opts.is_a?(String) || opts.is_a?(Symbol)) ? opts : block
  opts = opts.is_a?(Hash) ? opts : {}

  serialization_schema.add_attribute(
    JsonModel::Attribute.new(attribute_name, method, opts)
  )
end

#attributes(*attribute_names) ⇒ Object

Defines a list of attributes for serialization

each of these attributes value is fetched calling a corresponding method from a resource instance passed to the serializer

Parameters:

  • attribute_names (Array<String, Symbol>)

    a list of attributes to serialize



43
44
45
46
47
48
49
# File 'lib/fast_serializer/schema.rb', line 43

def attributes(*attribute_names)
  attribute_names.each do |attribute_name|
    serialization_schema.add_attribute(
      JsonModel::Attribute.new(attribute_name, attribute_name)
    )
  end
end

#deep_copyObject



124
125
126
127
128
129
130
131
132
# File 'lib/fast_serializer/schema.rb', line 124

def deep_copy
  schema = FastSerializer::Schema.new(params, _root, strict)

  serialization_schema.attributes.each do |key, attribute|
    schema.serialization_schema.attributes[key] = attribute
  end

  schema
end

#exclude=(list) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/fast_serializer/schema.rb', line 30

def exclude=(list)
  return unless list
  return if list.empty?

  @params[:exclude]       = list.map(&:to_sym)
  @params[:exclude_index] = @params[:exclude].map { |key| [key, nil] }.to_h
end

#has_many(attribute_name, opts = {}) ⇒ Object

Parameters:

  • attribute_name (String)
  • opts (Hash) (defaults to: {})

    attribute options



93
94
95
96
97
98
99
100
101
102
103
# File 'lib/fast_serializer/schema.rb', line 93

def has_many(attribute_name, opts = {})
  serialization_schema.add_attribute(
    JsonModel::HasManyRelationship.new(
      opts.delete(:key) || attribute_name,
      opts.delete(:method) || attribute_name,
      opts.delete(:serializer),
      opts.delete(:schema),
      opts,
    )
  )
end

#has_one(attribute_name, opts = {}) ⇒ Object Also known as: belongs_to

Defines an attribute for serialization

Parameters:

  • attribute_name (String, Symbol)

    an attribute name

  • opts (Hash) (defaults to: {})

    attribute options

Options Hash (opts):

  • :if (Proc)

    conditional clause. accepts a proc/lambda which has to return a boolean

  • :unless (Proc) — default: see opts:if
  • :serializer (FastSerializer::Schema::Mixin, nil)

    a serializer class with injected module or a inherited class

  • :schema (FastSerializer::Schema)


77
78
79
80
81
82
83
84
85
86
87
# File 'lib/fast_serializer/schema.rb', line 77

def has_one(attribute_name, opts = {})
  serialization_schema.add_attribute(
    JsonModel::HasOneRelationship.new(
      opts.delete(:key) || attribute_name,
      opts.delete(:method) || attribute_name,
      opts.delete(:serializer),
      opts.delete(:schema),
      opts,
    )
  )
end

#include=(list) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/fast_serializer/schema.rb', line 22

def include=(list)
  return unless list
  return if list.empty?

  @params[:include]       = list.map(&:to_sym)
  @params[:include_index] = @params[:include].map { |key| [key, nil] }.to_h
end

#list(attribute_name, opts = {}) ⇒ Object

Parameters:

  • attribute_name (String)
  • opts (Hash) (defaults to: {})
    • attribute options



107
108
109
110
111
112
113
114
115
116
117
# File 'lib/fast_serializer/schema.rb', line 107

def list(attribute_name, opts = {})
  serialization_schema.add_attribute(
    JsonModel::Array.new(
      attribute_name,
      attribute_name,
      opts.delete(:serializer),
      opts.delete(:schema),
      opts,
    )
  )
end

#root(root_key) ⇒ Object

Parameters:

  • root_key (String)
    • a key under which serialization result is nested



120
121
122
# File 'lib/fast_serializer/schema.rb', line 120

def root(root_key)
  @_root = root_key
end

#serialize_resource(resource, params = {}, context = self) ⇒ Object



134
135
136
137
138
139
140
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
168
# File 'lib/fast_serializer/schema.rb', line 134

def serialize_resource(resource, params = {}, context = self)
  _params_dup = FastSerializer::Utils.symbolize_keys(self.params)
  Utils.ref_merge(_params_dup, params)
  self.params.delete(:meta)

  meta        = _params_dup.delete(:meta)

  is_collection = if _params_dup.key?(:is_collection)
    _params_dup.delete(:is_collection)
    params.delete(:is_collection)
  else
    resource.respond_to?(:each) && !resource.respond_to?(:each_pair)
  end

  root = (_root || _params_dup.delete(:root))

  res = if is_collection

          if !context.is_a?(self.class)
            # need to bind context
            resource.map { |entry| context.class.new(entry, _params_dup).serializable_hash }
          else
            JsonModel::Array.new(:base, :base, nil, serialization_schema).serialize(resource, _params_dup, context)
          end

        else
          serialization_schema.serialize(resource, _params_dup, context)
        end

  res = { root => res } if root && !root.empty?

  res[:meta] = meta if res.is_a?(Hash) && meta

  res
end

#serialize_resource_to_json(resource, params = {}, context = self) ⇒ Object



170
171
172
# File 'lib/fast_serializer/schema.rb', line 170

def serialize_resource_to_json(resource, params = {}, context = self)
  FastSerializer.config.coder.dump(serialize_resource(resource, params, context))
end