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 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, &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, &block) ⇒ Validations
Creates a new stack of validations
26 27 28 29 30 31 32 |
# File 'lib/media_types/validations.rb', line 26 def initialize(media_type, registry = {}, scheme = Scheme.new, &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
47 48 49 50 51 52 53 54 55 56 |
# File 'lib/media_types/validations.rb', line 47 def method_missing(method_name, *arguments, &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, &block) end super end |
Instance Method Details
#find(media_type, default = -> { Scheme.new(allow_empty: true) { not_strict } }) ⇒ Scheme
Looks up the validations for Constructable
41 42 43 44 45 |
# File 'lib/media_types/validations.rb', line 41 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
58 59 60 |
# File 'lib/media_types/validations.rb', line 58 def respond_to_missing?(method_name, include_private = false) scheme.respond_to?(method_name) || super end |