Class: MediaTypes::Serialization::SerializationRegistration
- Inherits:
-
Object
- Object
- MediaTypes::Serialization::SerializationRegistration
- Defined in:
- lib/media_types/serialization/serialization_registration.rb
Overview
A collection that manages media type identifier registrations
Instance Attribute Summary collapse
-
#inout ⇒ Object
Returns the value of attribute inout.
-
#registrations ⇒ Object
Returns the value of attribute registrations.
Instance Method Summary collapse
- #call(victim, media_type, context, dsl: nil, raw: nil) ⇒ Object
- #decode(victim, media_type, context) ⇒ Object
- #filter(views:) ⇒ Object
- #has?(identifier) ⇒ Boolean
- #identifier_for(input_identifier) ⇒ Object
-
#initialize(direction) ⇒ SerializationRegistration
constructor
A new instance of SerializationRegistration.
- #merge(other) ⇒ Object
- #register_alias(serializer, alias_identifier, target_identifier, optional, hide_variant, wildcards: true) ⇒ Object
- #register_block(serializer, validator, version, block, raw, wildcards: true) ⇒ Object
Constructor Details
#initialize(direction) ⇒ SerializationRegistration
Returns a new instance of SerializationRegistration.
10 11 12 13 |
# File 'lib/media_types/serialization/serialization_registration.rb', line 10 def initialize(direction) self.registrations = {} self.inout = direction end |
Instance Attribute Details
#inout ⇒ Object
Returns the value of attribute inout.
15 16 17 |
# File 'lib/media_types/serialization/serialization_registration.rb', line 15 def inout @inout end |
#registrations ⇒ Object
Returns the value of attribute registrations.
15 16 17 |
# File 'lib/media_types/serialization/serialization_registration.rb', line 15 def registrations @registrations end |
Instance Method Details
#call(victim, media_type, context, dsl: nil, raw: nil) ⇒ Object
86 87 88 89 90 91 |
# File 'lib/media_types/serialization/serialization_registration.rb', line 86 def call(victim, media_type, context, dsl: nil, raw: nil) registration = registrations[media_type] raise UnregisteredMediaTypeUsageError.new(media_type, registrations.keys) if registration.nil? registration.call(victim, context, dsl: dsl, raw: raw) end |
#decode(victim, media_type, context) ⇒ Object
79 80 81 82 83 84 |
# File 'lib/media_types/serialization/serialization_registration.rb', line 79 def decode(victim, media_type, context) registration = registrations[media_type] raise UnregisteredMediaTypeUsageError.new(media_type, registrations.keys) if registration.nil? registration.decode(victim, context) end |
#filter(views:) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/media_types/serialization/serialization_registration.rb', line 100 def filter(views:) result = SerializationRegistration.new inout registrations.each do |identifier, registration| if views.include? registration.validator.view result.registrations[identifier] = registration end end result end |
#has?(identifier) ⇒ Boolean
17 18 19 |
# File 'lib/media_types/serialization/serialization_registration.rb', line 17 def has?(identifier) registrations.key? identifier end |
#identifier_for(input_identifier) ⇒ Object
93 94 95 96 97 98 |
# File 'lib/media_types/serialization/serialization_registration.rb', line 93 def identifier_for(input_identifier) registration = registrations[input_identifier] raise UnregisteredMediaTypeUsageError.new(media_type, registrations.keys) if registration.nil? registration.display_identifier end |
#merge(other) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/media_types/serialization/serialization_registration.rb', line 56 def merge(other) raise Error, 'Trying to merge two SerializationRegistration objects with a different direction.' unless inout == other.inout result = SerializationRegistration.new(inout) prev_keys = Set.new(registrations.keys) new_keys = Set.new(other.registrations.keys) overlap = prev_keys & new_keys result.registrations = registrations.merge(other.registrations) overlap.each do |identifier| prev_item = registrations[identifier] new_item = other.registrations[identifier] merge_result = prev_item.merge(new_item) raise DuplicateUsageError.new(identifier, inout, prev_item.serializer, new_item.serializer) if merge_result.nil? result.registrations[identifier] = merge_result end result end |
#register_alias(serializer, alias_identifier, target_identifier, optional, hide_variant, wildcards: true) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/media_types/serialization/serialization_registration.rb', line 34 def register_alias(serializer, alias_identifier, target_identifier, optional, hide_variant, wildcards: true) raise DuplicateDefinitionError.new(alias_identifier, inout) if registrations.key? alias_identifier unless registrations.key? target_identifier potential_match = registrations.keys.find do |k| k.start_with? target_identifier end raise VersionedAliasDefinitionError.new(target_identifier, inout, potential_match) unless potential_match.nil? raise UnbackedAliasDefinitionError.new(target_identifier, inout) end target = registrations[target_identifier] result_content_type = alias_identifier result_content_type += "; variant=#{target_identifier}" unless hide_variant registration = SerializationAliasRegistration.new serializer, inout, target.validator, result_content_type, target, optional, hide_variant registrations[alias_identifier] = registration register_wildcards(alias_identifier, registration) if wildcards && inout == :output end |
#register_block(serializer, validator, version, block, raw, wildcards: true) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/media_types/serialization/serialization_registration.rb', line 21 def register_block(serializer, validator, version, block, raw, wildcards: true) identifier = validator.identifier raise DuplicateDefinitionError.new(identifier, inout) if registrations.key? identifier raise ValidatorNotDefinedError.new(identifier, inout) unless raw || validator.validatable? registration = SerializationBlockRegistration.new serializer, inout, validator, identifier, version, block, raw registrations[identifier] = registration register_wildcards(identifier, registration) if wildcards && inout == :output end |