Class: SoberSwag::Reporting::Output::Pattern

Inherits:
Base
  • Object
show all
Defined in:
lib/sober_swag/reporting/output/pattern.rb

Overview

Output with a particular pattern.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#view, #views

Methods included from Interface

#call!, #described, #enum, #in_range, #list, #nilable, #partitioned, #referenced, #reporting?, #serialize, #via_map

Constructor Details

#initialize(output, pattern) ⇒ Pattern

Returns a new instance of Pattern.



7
8
9
10
# File 'lib/sober_swag/reporting/output/pattern.rb', line 7

def initialize(output, pattern)
  @output = output
  @pattern = pattern
end

Instance Attribute Details

#outputInterface (readonly)

Returns:



14
15
16
# File 'lib/sober_swag/reporting/output/pattern.rb', line 14

def output
  @output
end

#patternRegexp (readonly)

Returns:

  • (Regexp)


18
19
20
# File 'lib/sober_swag/reporting/output/pattern.rb', line 18

def pattern
  @pattern
end

Instance Method Details

#call(input) ⇒ Object



20
21
22
# File 'lib/sober_swag/reporting/output/pattern.rb', line 20

def call(input)
  output.call(input)
end

#serialize_report(value) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/sober_swag/reporting/output/pattern.rb', line 24

def serialize_report(value)
  base = output.serialize_report(value)

  return base if base.is_a?(Report::Error)

  if pattern.match?(base)
    base
  else
    Report::Value.new(['did not match pattern'])
  end
end

#swagger_schemaObject



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/sober_swag/reporting/output/pattern.rb', line 36

def swagger_schema
  schema, defs = output.swagger_schema

  merged =
    if schema.key?(:$ref)
      { oneOf: [schema] }
    else
      schema
    end.merge(pattern: pattern.to_s.gsub('?-mix:', ''))
  [merged, defs]
end