class Bluepine::Generators::OpenAPI::PropertyGenerator

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

Public Class Methods

generate(attr, options = {})
Alias for: visit
visit(attr, options = {}) click to toggle source
# File lib/bluepine/generators/open_api/property_generator.rb, line 9
def visit(attr, options = {})
  new.visit(attr, options)
end
Also aliased as: generate

Public Instance Methods

normalize_attribute(object, options = {}) click to toggle source
# 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 = {}) click to toggle source
Calls superclass method Bluepine::Attributes::Visitor#visit
# 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 = {}) click to toggle source
# 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 = {}) click to toggle source

catch-all

# File lib/bluepine/generators/open_api/property_generator.rb, line 26
def visit_attribute(attr, options = {})
  build(attr, options)
end
visit_object(attr, options = {}) click to toggle source
# 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) click to toggle source

Handle SchemaAttribute

# 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

Private Instance Methods

build(attr, options = {}) click to toggle source
# File lib/bluepine/generators/open_api/property_generator.rb, line 77
def build(attr, options = {})
  assert_kind_of Bluepine::Attributes::Attribute, attr

  # build base property
  {
    type: attr.native_type,
  }.tap do |property|
    property[:description]     = attr.description if attr.description.present?
    property[:default]         = attr.default if attr.default
    property[:enum]            = attr.in if attr.in
    property[:nullable]        = attr.null if attr.null
    property[:format]          = attr.format if attr.format
    property[:pattern]         = build_pattern(attr.match) if attr.match
    property["x-omise-schema"] = options[:schema] if options[:schema].present?
  end
end
build_pattern(value) click to toggle source
# File lib/bluepine/generators/open_api/property_generator.rb, line 103
def build_pattern(value)
  return value.source if value.respond_to?(:source)

  value
end
build_ref(attr, options = {}) click to toggle source

create $ref

# File lib/bluepine/generators/open_api/property_generator.rb, line 95
def build_ref(attr, options = {})
  ref = options[:as] || attr

  {
    "$ref": "#/components/schemas/#{ref}",
  }
end