Class: Bluepine::Generators::OpenAPI::PropertyGenerator

Inherits:
Attributes::Visitor show all
Includes:
Assertions
Defined in:
lib/bluepine/generators/open_api/property_generator.rb

Overview

Generate property based on Open API Spec (shared for both Omise/Open API specs)

Constant Summary

Constants included from Assertions

Assertions::Error, Assertions::KeyError, Assertions::SubsetError

Constants inherited from Attributes::Visitor

Attributes::Visitor::MethodNotFound

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Assertions

#assert, #assert_in, #assert_kind_of, #assert_not, #assert_subset_of, included

Methods included from Functions

#compose, #compose_result, #curry, #result

Class Method Details

.generateObject



13
14
15
# File 'lib/bluepine/generators/open_api/property_generator.rb', line 13

def visit(attr, options = {})
  new.visit(attr, options)
end

.visit(attr, options = {}) ⇒ Object



9
10
11
# File 'lib/bluepine/generators/open_api/property_generator.rb', line 9

def visit(attr, options = {})
  new.visit(attr, options)
end

Instance Method Details

#normalize_attribute(object, options = {}) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/bluepine/generators/open_api/property_generator.rb', line 67

def normalize_attribute(object, options = {})
  return build_ref(object, options)    if object.kind_of?(Symbol)
  return object                        if object.respond_to?(:native_type)

  # object is string (native types e.g. "integer", "boolean" etc)
  Bluepine::Attributes.create(object.to_sym, object)
end

#visit(attr, options = {}) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/bluepine/generators/open_api/property_generator.rb', line 16

def visit(attr, options = {})
  attr = normalize_attribute(attr, options)

  # handle case when attr is a Symbol (reference)
  return attr unless attr.respond_to?(:native_type)

  super
end

#visit_array(attr, options = {}) ⇒ Object



30
31
32
33
34
# File 'lib/bluepine/generators/open_api/property_generator.rb', line 30

def visit_array(attr, options = {})
  build(attr, options).tap do |property|
    property[:items] = attr.of ? visit(attr.of, options) : {}
  end
end

#visit_attribute(attr, options = {}) ⇒ Object

catch-all



26
27
28
# File 'lib/bluepine/generators/open_api/property_generator.rb', line 26

def visit_attribute(attr, options = {})
  build(attr, options)
end

#visit_object(attr, options = {}) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/bluepine/generators/open_api/property_generator.rb', line 36

def visit_object(attr, options = {})
  build(attr, options).tap do |property|
    required = []
    attr.attributes.values.each_with_object(property) do |attribute, object|

      # Adds to required list
      required << attribute.name if attribute.required

      object[:properties] ||= {}
      object[:properties][attribute.name] = visit(attribute, options) if attribute.serializable?
    end

    # additional options
    property[:required] = required unless required.empty?
  end
end

#visit_schema(attr, options) ⇒ Object

Handle SchemaAttribute



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/bluepine/generators/open_api/property_generator.rb', line 54

def visit_schema(attr, options)
  return build_ref(attr.of) unless attr.expandable

  # SchemaAttribute#of may contains array of references
  # e.g. of = [:user, :customer]
  refs = Array(attr.of).map { |of| build_ref(of) }
  refs << visit("string")

  {
    "oneOf": refs,
  }
end