class Apigen::Model

Model represents a data model with a specific name, e.g. “User” with an object type.

Attributes

name[R]

Public Class Methods

new(name) click to toggle source
# File lib/apigen/models/model.rb, line 20
def initialize(name)
  @name = name
  @type = nil
  @description = nil
end
type(shape = nil, &block) click to toggle source
# File lib/apigen/models/model.rb, line 31
def self.type(shape = nil, &block)
  return type if shape.nil?
  case shape
  when :object
    object = ObjectType.new
    object.instance_eval(&block)
    object
  when :array
    array = ArrayType.new
    array.instance_eval(&block)
    array
  when :oneof
    oneof = OneofType.new
    oneof.instance_eval(&block)
    oneof
  when :enum
    enum = EnumType.new
    enum.instance_eval(&block)
    enum
  else
    raise "A block should not be provided with :#{shape}." if block_given?
    primary_or_reference_type(shape)
  end
end

Private Class Methods

primary_or_reference_type(shape) click to toggle source
# File lib/apigen/models/model.rb, line 56
                     def self.primary_or_reference_type(shape)
  if PrimaryType.primary?(shape)
    PrimaryType.new(shape)
  else
    ReferenceType.new(shape)
  end
end

Public Instance Methods

to_s() click to toggle source
# File lib/apigen/models/model.rb, line 79
def to_s
  @type.to_s
end
type(shape = nil, &block) click to toggle source
# File lib/apigen/models/model.rb, line 26
def type(shape = nil, &block)
  return @type unless shape
  @type = Model.type shape, &block
end
update_object_properties(&block) click to toggle source
# File lib/apigen/models/model.rb, line 74
def update_object_properties(&block)
  raise "#{@name} is not an object type" unless @type.is_a? ObjectType
  @type.instance_eval(&block)
end
validate(model_registry) click to toggle source
# File lib/apigen/models/model.rb, line 64
def validate(model_registry)
  error = if !@name
            'One of the models is missing a name.'
          elsif !@type
            "Use `type :model_type [block]` to assign a type to :#{@name}."
          end
  raise error unless error.nil?
  model_registry.check_type @type
end