Class: MediaTypes::Validations
- Inherits:
-
Object
- Object
- MediaTypes::Validations
- Defined in:
- lib/media_types/validations.rb
Overview
Takes care of registering validations for a media type. It allows for nested schemes and registers each one so it can be looked up at a later time.
Instance Attribute Summary collapse
-
#scheme ⇒ Object
readonly
Returns the value of attribute scheme.
Instance Method Summary collapse
-
#find(media_type, default = -> { Scheme.new(allow_empty: true) { not_strict } }) ⇒ Scheme
Looks up the validations for Constructable.
-
#initialize(media_type, registry = {}, scheme = Scheme.new(registry: registry, current_type: media_type), &block) ⇒ Validations
constructor
Creates a new stack of validations.
- #method_missing(method_name, *arguments, &block) ⇒ Object
- #respond_to_missing?(method_name, include_private = false) ⇒ Boolean
Constructor Details
#initialize(media_type, registry = {}, scheme = Scheme.new(registry: registry, current_type: media_type), &block) ⇒ Validations
Creates a new stack of validations
28 29 30 31 32 33 34 |
# File 'lib/media_types/validations.rb', line 28 def initialize(media_type, registry = {}, scheme = Scheme.new(registry: registry, current_type: media_type), &block) self.media_type = media_type self.registry = registry.merge!(media_type.as_key => scheme) self.scheme = scheme instance_exec(&block) if block_given? end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *arguments, &block) ⇒ Object
51 52 53 54 55 56 57 58 59 60 |
# File 'lib/media_types/validations.rb', line 51 def method_missing(method_name, *arguments, **kwargs, &block) if scheme.respond_to?(method_name) media_type.__getobj__.media_type_combinations ||= Set.new media_type.__getobj__.media_type_combinations.add(media_type.as_key) return scheme.send(method_name, *arguments, **kwargs, &block) end super end |
Instance Attribute Details
#scheme ⇒ Object
Returns the value of attribute scheme.
15 16 17 |
# File 'lib/media_types/validations.rb', line 15 def scheme @scheme end |
Instance Method Details
#find(media_type, default = -> { Scheme.new(allow_empty: true) { not_strict } }) ⇒ Scheme
Looks up the validations for Constructable
43 44 45 46 47 |
# File 'lib/media_types/validations.rb', line 43 def find(media_type, default = -> { Scheme.new(allow_empty: true) { not_strict } }) registry.fetch(media_type.as_key) do default.call end end |
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
74 75 76 |
# File 'lib/media_types/validations.rb', line 74 def respond_to_missing?(method_name, include_private = false) scheme.respond_to?(method_name) || super end |