Class: MediaTypes::Validations

Inherits:
Object
  • Object
show all
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.

See Also:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(media_type, registry = {}, scheme = Scheme.new(registry: registry, current_type: media_type), &block) ⇒ Validations

Creates a new stack of validations

Parameters:

  • media_type (Constructable)

    a Constructable media type

  • registry (Hash) (defaults to: {})

    the registry reference, or nil if top level

  • scheme (Scheme) (defaults to: Scheme.new(registry: registry, current_type: media_type))

    the current scheme or nil if top level

See Also:



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

#schemeObject

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

Parameters:

  • media_type (String, Constructable)
  • default (lambda) (defaults to: -> { Scheme.new(allow_empty: true) { not_strict } })

    the lambda if nothing can be found

Returns:

  • (Scheme)

    the scheme for the given media_type



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

Returns:

  • (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