Class: Committee::Middleware::Options::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/committee/middleware/options/base.rb

Direct Known Subclasses

RequestValidation, ResponseValidation

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Base

Returns a new instance of Base.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/committee/middleware/options/base.rb', line 20

def initialize(options = {})
  @original_options = options.dup.freeze

  # Schema related
  @schema = options[:schema]
  @schema_path = options[:schema_path]
  @strict_reference_validation = options.fetch(:strict_reference_validation, false)

  # Error handling
  @error_class = options.fetch(:error_class, Committee::ValidationError)
  @error_handler = options[:error_handler]
  @raise_error = options[:raise] || false
  @ignore_error = options.fetch(:ignore_error, false)

  # Routing
  @prefix = options[:prefix]
  @accept_request_filter = options[:accept_request_filter] || ->(_) { true }

  validate!
end

Instance Attribute Details

#accept_request_filterObject (readonly)

Returns the value of attribute accept_request_filter.



15
16
17
# File 'lib/committee/middleware/options/base.rb', line 15

def accept_request_filter
  @accept_request_filter
end

#error_classObject (readonly)

Returns the value of attribute error_class.



10
11
12
# File 'lib/committee/middleware/options/base.rb', line 10

def error_class
  @error_class
end

#error_handlerObject (readonly)

Returns the value of attribute error_handler.



11
12
13
# File 'lib/committee/middleware/options/base.rb', line 11

def error_handler
  @error_handler
end

#ignore_errorObject (readonly)

Returns the value of attribute ignore_error.



13
14
15
# File 'lib/committee/middleware/options/base.rb', line 13

def ignore_error
  @ignore_error
end

#prefixObject (readonly)

Returns the value of attribute prefix.



14
15
16
# File 'lib/committee/middleware/options/base.rb', line 14

def prefix
  @prefix
end

#raise_errorObject (readonly)

Returns the value of attribute raise_error.



12
13
14
# File 'lib/committee/middleware/options/base.rb', line 12

def raise_error
  @raise_error
end

#schemaObject (readonly)

Common options



8
9
10
# File 'lib/committee/middleware/options/base.rb', line 8

def schema
  @schema
end

#schema_pathObject (readonly)

Returns the value of attribute schema_path.



9
10
11
# File 'lib/committee/middleware/options/base.rb', line 9

def schema_path
  @schema_path
end

#strict_reference_validationObject (readonly)

Schema loading options



18
19
20
# File 'lib/committee/middleware/options/base.rb', line 18

def strict_reference_validation
  @strict_reference_validation
end

Class Method Details

.from(options) ⇒ Object

Create an Option object from Hash options Returns the object as-is if already an Option object



56
57
58
59
60
61
62
63
64
65
# File 'lib/committee/middleware/options/base.rb', line 56

def self.from(options)
  case options
  when self
    options
  when Hash
    new(options)
  else
    raise ArgumentError, "options must be a Hash or #{name}"
  end
end

Instance Method Details

#[](key) ⇒ Object

Allow Hash-like access for backward compatibility



42
43
44
# File 'lib/committee/middleware/options/base.rb', line 42

def [](key)
  to_h[key]
end

#fetch(key, default = nil) ⇒ Object



46
47
48
# File 'lib/committee/middleware/options/base.rb', line 46

def fetch(key, default = nil)
  to_h.fetch(key, default)
end

#to_hObject



50
51
52
# File 'lib/committee/middleware/options/base.rb', line 50

def to_h
  @to_h ||= build_hash
end