Method: CORS::Rules#required

Defined in:
lib/cors/rules.rb

#required(name, constraints = nil) {|value| ... } ⇒ Hash

Declare a required rule; the value must be present, and it must match the given constraints or block matcher.

Examples:

with a regexp

@required "content-type", %r|image/jpe?g|

with a string

required "content-type", "image/jpeg"

with an array

required "content-type", ["image/jpeg", "image/jpg"]

with a block

required "content-type" do |value|
  value =~ %r|image/jpe?g|
end

Parameters:

  • name

    can be any valid hash key of the parameters to be validated

  • constraints (Regexp, String, Array) (defaults to: nil)

Yields:

  • (value)

Yield Parameters:

  • value

    of the key ‘name` in the parameters to be validated

Returns:

  • (Hash)

    the newly created rule



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/cors/rules.rb', line 74

def required(name, constraints = nil, &block)
  matcher = if block_given? then block
  elsif constraints.is_a?(Regexp)
    constraints.method(:===)
  elsif constraints.is_a?(String)
    constraints.method(:===)
  elsif constraints.is_a?(Array)
    constraints.method(:include?)
  else
    raise ArgumentError, "unknown matcher #{(constraints || block).inspect}"
  end

  { name: name, matcher: matcher, required: true }.tap do |rule|
    @rules << rule
    @rules_map[name] << rule
  end
end