Class: Rage::OpenAPI::Parsers::Ext::Blueprinter::Visitor

Inherits:
Prism::Visitor
  • Object
show all
Defined in:
lib/rage/openapi/parsers/ext/blueprinter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parser, is_collection) ⇒ Visitor

Returns a new instance of Visitor.



47
48
49
50
51
52
53
54
55
# File 'lib/rage/openapi/parsers/ext/blueprinter.rb', line 47

def initialize(parser, is_collection)
  @parser = parser
  @is_collection = is_collection

  @context = nil
  @schema = {}
  @segment = @schema
  @identifier = {}
end

Instance Attribute Details

#schemaObject

Returns the value of attribute schema.



45
46
47
# File 'lib/rage/openapi/parsers/ext/blueprinter.rb', line 45

def schema
  @schema
end

Instance Method Details

#build_schemaObject



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/rage/openapi/parsers/ext/blueprinter.rb', line 57

def build_schema
  result = { "type" => "object" }

  properties = {}
  properties.merge!(@identifier)
  properties.merge!(@schema.sort.to_h)

  result["properties"] = properties if properties.any?
  result = { "type" => "array", "items" => result } if @is_collection
  result
end

#visit_assoc_node(node) ⇒ Object



90
91
92
# File 'lib/rage/openapi/parsers/ext/blueprinter.rb', line 90

def visit_assoc_node(node)
  @context.keywords[node.key.value] = node.value.unescaped
end

#visit_call_node(node) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/rage/openapi/parsers/ext/blueprinter.rb', line 69

def visit_call_node(node)
  case node.name
  when :identifier
    context = with_context { visit(node.arguments) }
    @identifier[context.symbols.first] = { "type" => "string" }

  when :fields, :field
    context = with_context { visit(node.arguments) }

    if context.keywords["name"]
      @segment[context.keywords["name"]] = { "type" => "string" }
    elsif node.block
      @segment[context.symbols.first] = { "type" => "string" } if context.symbols.first
      @segment[context.strings.first] = { "type" => "string" } if context.strings.first
    else
      context.symbols.each { |symbol| @segment[symbol] = { "type" => "string" } }
      context.strings.each { |string| @segment[string] = { "type" => "string" } }
    end
  end
end

#visit_string_node(node) ⇒ Object



98
99
100
# File 'lib/rage/openapi/parsers/ext/blueprinter.rb', line 98

def visit_string_node(node)
  @context.strings << node.unescaped
end

#visit_symbol_node(node) ⇒ Object



94
95
96
# File 'lib/rage/openapi/parsers/ext/blueprinter.rb', line 94

def visit_symbol_node(node)
  @context.symbols << node.value
end