Module: MediaTypes::Serialization::Base::ClassMethods

Defined in:
lib/media_types/serialization/base.rb

Instance Method Summary collapse

Instance Method Details

#deserialize(victim, media_type_identifier, context:) ⇒ Object



225
226
227
228
229
230
231
232
# File 'lib/media_types/serialization/base.rb', line 225

def deserialize(victim, media_type_identifier, context:)
  merged_input_registrations = SerializationRegistration.new(:input)
  serializer_input_registrations.keys.each do |k|
    merged_input_registrations = merged_input_registrations.merge(serializer_input_registrations[k])
  end

  merged_input_registrations.call(victim, media_type_identifier, context)
end

#disable_wildcardsObject



29
30
31
# File 'lib/media_types/serialization/base.rb', line 29

def disable_wildcards
  self.serializer_disable_wildcards = true
end

#enable_wildcardsObject



33
34
35
# File 'lib/media_types/serialization/base.rb', line 33

def enable_wildcards
  self.serializer_disable_wildcards = false
end

#input(view: nil, version: nil, versions: nil, &block) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/media_types/serialization/base.rb', line 135

def input(view: nil, version: nil, versions: nil, &block)
  versions = [version] if versions.nil?
  raise VersionsNotAnArrayError unless versions.is_a? Array

  raise ValidatorNotSpecifiedError, :input if serializer_validator.nil?

  unless serializer_input_registrations.has_key? view
    serializer_input_registrations[view] = SerializationRegistration.new(:input)
  end

  versions.each do |v|
    validator = serializer_validator.view(view).version(v)
    validator.override_suffix(:json) unless serializer_validated

    serializer_input_registrations[view].register_block(self, validator, v, block, false)
  end
end

#input_alias(media_type_identifier, view: nil, suffix: media_type_identifier == 'application/json' || media_type_identifier.end_with?('+json') ? :json : nil) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/media_types/serialization/base.rb', line 170

def input_alias(
  media_type_identifier,
  view: nil,
  suffix: media_type_identifier == 'application/json' || media_type_identifier.end_with?('+json') ? :json : nil
)
  validator = serializer_validator.view(view).override_suffix(suffix)
  victim_identifier = validator.identifier

  unless serializer_input_registrations.has_key? view
    serializer_input_registrations[view] = SerializationRegistration.new(:input)
  end

  serializer_input_registrations[view].register_alias(
    self,
    media_type_identifier,
    victim_identifier,
    false,
    true,
    wildcards: false
  )
end

#input_alias_optional(media_type_identifier, view: nil, suffix: media_type_identifier == 'application/json' || media_type_identifier.end_with?('+json') ? :json : nil) ⇒ Object



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/media_types/serialization/base.rb', line 192

def input_alias_optional(
  media_type_identifier,
  view: nil,
  suffix: media_type_identifier == 'application/json' || media_type_identifier.end_with?('+json') ? :json : nil
)
  validator = serializer_validator.view(view).override_suffix(suffix)
  victim_identifier = validator.identifier

  unless serializer_input_registrations.has_key? view
    serializer_input_registrations[view] = SerializationRegistration.new(:input)
  end

  serializer_input_registrations[view].register_alias(
    self,
    media_type_identifier,
    victim_identifier,
    true,
    true,
    wildcards: false
  )
end

#input_raw(view: nil, version: nil, versions: nil, suffix: nil, &block) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/media_types/serialization/base.rb', line 153

def input_raw(view: nil, version: nil, versions: nil, suffix: nil, &block)
  versions = [version] if versions.nil?
  raise VersionsNotAnArrayError unless versions.is_a? Array

  raise ValidatorNotSpecifiedError, :input if serializer_validator.nil?

  unless serializer_input_registrations.has_key? view
    serializer_input_registrations[view] = SerializationRegistration.new(:input)
  end

  versions.each do |v|
    validator = serializer_validator.view(view).version(v).override_suffix(suffix)

    serializer_input_registrations[view].register_block(self, validator, v, block, true)
  end
end

#inputs_for(views:) ⇒ Object



243
244
245
246
247
248
249
250
# File 'lib/media_types/serialization/base.rb', line 243

def inputs_for(views:)
  merged_input_registrations = SerializationRegistration.new(:input)
  views.each do |k|
    merged_input_registrations = merged_input_registrations.merge(serializer_input_registrations[k]) if serializer_input_registrations.has_key?(k)
  end

  merged_input_registrations
end

#output(view: nil, version: nil, versions: nil, &block) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/media_types/serialization/base.rb', line 37

def output(view: nil, version: nil, versions: nil, &block)
  versions = [version] if versions.nil?
  raise VersionsNotAnArrayError unless versions.is_a? Array

  raise ValidatorNotSpecifiedError, :output if serializer_validator.nil?

  unless serializer_output_registrations.has_key? view
    serializer_output_registrations[view] = SerializationRegistration.new(:output)
  end

  versions.each do |v|
    validator = serializer_validator.view(view).version(v)
    validator.override_suffix(:json) unless serializer_validated

    serializer_output_registrations[view].register_block(
      self,
      validator,
      v,
      block,
      false,
      wildcards:
      !serializer_disable_wildcards
    )
  end
end

#output_alias(media_type_identifier, view: nil, suffix: media_type_identifier == 'application/json' || media_type_identifier.end_with?('+json') ? :json : nil, hide_variant: false) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/media_types/serialization/base.rb', line 89

def output_alias(
  media_type_identifier,
  view: nil,
  suffix: media_type_identifier == 'application/json' || media_type_identifier.end_with?('+json') ? :json : nil,
  hide_variant: false
)
  validator = serializer_validator.view(view).override_suffix(suffix)
  victim_identifier = validator.identifier

  unless serializer_output_registrations.has_key? view
    serializer_output_registrations[view] = SerializationRegistration.new(:output)
  end

  serializer_output_registrations[view].register_alias(
    self,
    media_type_identifier,
    victim_identifier,
    false,
    hide_variant,
    wildcards: !serializer_disable_wildcards
  )
end

#output_alias_optional(media_type_identifier, view: nil, suffix: media_type_identifier == 'application/json' || media_type_identifier.end_with?('+json') ? :json : nil, hide_variant: false) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/media_types/serialization/base.rb', line 112

def output_alias_optional(
  media_type_identifier,
  view: nil,
  suffix: media_type_identifier == 'application/json' || media_type_identifier.end_with?('+json') ? :json : nil,
  hide_variant: false
)
  validator = serializer_validator.view(view).override_suffix(suffix)
  victim_identifier = validator.identifier

  unless serializer_output_registrations.has_key? view
    serializer_output_registrations[view] = SerializationRegistration.new(:output)
  end

  serializer_output_registrations[view].register_alias(
    self,
    media_type_identifier,
    victim_identifier,
    true,
    hide_variant,
    wildcards: !serializer_disable_wildcards
  )
end

#output_raw(view: nil, version: nil, versions: nil, suffix: nil, &block) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/media_types/serialization/base.rb', line 63

def output_raw(view: nil, version: nil, versions: nil, suffix: nil, &block)
  versions = [version] if versions.nil?
  raise VersionsNotAnArrayError unless versions.is_a? Array

  raise ValidatorNotSpecifiedError, :output if serializer_validator.nil?

  unless serializer_output_registrations.has_key? view
    serializer_output_registrations[view] = SerializationRegistration.new(:output)
  end

  versions.each do |v|
    validator = serializer_validator.view(view)
                                    .version(v)
                                    .override_suffix(suffix)

    serializer_output_registrations[view].register_block(
      self,
      validator,
      v,
      block,
      true,
      wildcards: !serializer_disable_wildcards
    )
  end
end

#outputs_for(views:) ⇒ Object



234
235
236
237
238
239
240
241
# File 'lib/media_types/serialization/base.rb', line 234

def outputs_for(views:)
  merged_output_registrations = SerializationRegistration.new(:output)
  views.each do |k|
    merged_output_registrations = merged_output_registrations.merge(serializer_output_registrations[k]) if serializer_output_registrations.has_key?(k)
  end
  
  merged_output_registrations
end

#serialize(victim, media_type_identifier, context:, dsl: nil, raw: nil) ⇒ Object



214
215
216
217
218
219
220
221
222
223
# File 'lib/media_types/serialization/base.rb', line 214

def serialize(victim, media_type_identifier, context:, dsl: nil, raw: nil)
  dsl ||= SerializationDSL.new(self, context: context)
  
  merged_output_registrations = SerializationRegistration.new(:output)
  serializer_output_registrations.keys.each do |k|
    merged_output_registrations = merged_output_registrations.merge(serializer_output_registrations[k])
  end

  merged_output_registrations.call(victim, media_type_identifier.to_s, context, dsl: dsl, raw: raw)
end

#unvalidated(prefix) ⇒ Object



12
13
14
15
16
17
# File 'lib/media_types/serialization/base.rb', line 12

def unvalidated(prefix)
  self.serializer_validated = false
  self.serializer_validator = FakeValidator.new(prefix)
  self.serializer_input_registrations = {}
  self.serializer_output_registrations = {}
end

#validator(validator = nil) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/media_types/serialization/base.rb', line 19

def validator(validator = nil)
  raise NoValidatorSetError if !defined? serializer_validator && validator.nil?
  return serializer_validator if validator.nil?

  self.serializer_validated = true
  self.serializer_validator = validator
  self.serializer_input_registrations = {}
  self.serializer_output_registrations = {}
end