Class: MediaTypes::Serialization::SerializationBlockRegistration

Inherits:
SerializationBaseRegistration show all
Defined in:
lib/media_types/serialization/serialization_registration.rb

Overview

A registration with a block to be executed when called.

Instance Attribute Summary collapse

Attributes inherited from SerializationBaseRegistration

#display_identifier, #inout, #serializer, #validator

Instance Method Summary collapse

Constructor Details

#initialize(serializer, inout, validator, display_identifier, version, block, raw) ⇒ SerializationBlockRegistration

Returns a new instance of SerializationBlockRegistration.



149
150
151
152
153
154
# File 'lib/media_types/serialization/serialization_registration.rb', line 149

def initialize(serializer, inout, validator, display_identifier, version, block, raw)
  self.version = version
  self.block = block
  self.raw = raw
  super(serializer, inout, validator, display_identifier)
end

Instance Attribute Details

#blockObject

Returns the value of attribute block.



211
212
213
# File 'lib/media_types/serialization/serialization_registration.rb', line 211

def block
  @block
end

#rawObject

Returns the value of attribute raw.



211
212
213
# File 'lib/media_types/serialization/serialization_registration.rb', line 211

def raw
  @raw
end

#versionObject

Returns the value of attribute version.



211
212
213
# File 'lib/media_types/serialization/serialization_registration.rb', line 211

def version
  @version
end

Instance Method Details

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



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/media_types/serialization/serialization_registration.rb', line 188

def call(victim, context, dsl: nil, raw: nil)
  raw = self.raw if raw.nil?

  result = nil
  if dsl.nil?
    result = victim
    result = block.call(victim, version, context) if block
  else
    result = dsl.instance_exec victim, version, context, &block
  end

  if !raw && inout == :output
    begin
      validator.validate!(result)
    rescue MediaTypes::Scheme::ValidationError => inner
      raise OutputValidationFailedError, inner
    end
    result = MediaTypes::Serialization.json_encoder.call(result)
  end

  result
end

#decode(victim, _context) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/media_types/serialization/serialization_registration.rb', line 164

def decode(victim, _context)
  raise CannotDecodeOutputError if inout != :input

  unless raw
    if defined? Oj::ParseError
      begin
        victim = MediaTypes::Serialization.json_decoder.call(victim)
        validator.validate!(victim, loose: true)
      rescue MediaTypes::Scheme::ValidationError, Oj::ParseError, JSON::ParserError, EncodingError => inner
        raise InputValidationFailedError, inner
      end
    else
      begin
        victim = MediaTypes::Serialization.json_decoder.call(victim)
        validator.validate!(victim, loose: true)
      rescue MediaTypes::Scheme::ValidationError, JSON::ParserError, EncodingError => inner
        raise InputValidationFailedError, inner
      end
    end
  end

  victim
end

#merge(other) ⇒ Object



156
157
158
159
160
161
162
# File 'lib/media_types/serialization/serialization_registration.rb', line 156

def merge(other)
  return nil unless other.is_a?(SerializationAliasRegistration)

  return self if other.optional

  nil
end