Class: Apigen::Model

Inherits:
Object
  • Object
show all
Defined in:
lib/apigen/models/model.rb

Overview

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Model

Returns a new instance of Model.



20
21
22
23
24
# File 'lib/apigen/models/model.rb', line 20

def initialize(name)
  @name = name
  @type = nil
  @description = nil
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



16
17
18
# File 'lib/apigen/models/model.rb', line 16

def name
  @name
end

Class Method Details

.type(shape = nil, &block) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# 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

Instance Method Details

#to_sObject



79
80
81
# File 'lib/apigen/models/model.rb', line 79

def to_s
  @type.to_s
end

#type(shape = nil, &block) ⇒ Object



26
27
28
29
# 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) ⇒ Object



74
75
76
77
# 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) ⇒ Object



64
65
66
67
68
69
70
71
72
# 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