Class: Apigen::ObjectType

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

Overview

ObjectType represents an object type, with specific properties.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeObjectType

Returns a new instance of ObjectType.



12
13
14
# File 'lib/apigen/models/object_type.rb', line 12

def initialize
  @properties = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(property_name, property_shape, &block) ⇒ Object

rubocop:disable Style/MethodMissingSuper



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/apigen/models/object_type.rb', line 27

def method_missing(property_name, property_shape, &block)
  raise "Property :#{property_name} is defined multiple times." if @properties.key? property_name
  raise "Property type must be a symbol, found #{property_shape}." unless property_shape.is_a? Symbol
  if property_shape.to_s.end_with? '?'
    property_shape = property_shape[0..-2].to_sym
    required = false
  else
    required = true
  end
  property = ObjectProperty.new(Apigen::Model.type(property_shape, &block))
  property.required = required
  @properties[property_name] = property
end

Instance Attribute Details

#propertiesObject (readonly)

Returns the value of attribute properties.



10
11
12
# File 'lib/apigen/models/object_type.rb', line 10

def properties
  @properties
end

Instance Method Details

#add(&block) ⇒ Object



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

def add(&block)
  instance_eval(&block)
end

#remove(*property_names) ⇒ Object



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

def remove(*property_names)
  property_names.each do |property_name|
    raise "Cannot remove nonexistent property :#{property_name}." unless @properties.delete(property_name)
  end
end

#repr(indent) ⇒ Object



56
57
58
59
60
61
62
63
# File 'lib/apigen/models/object_type.rb', line 56

def repr(indent)
  repr = '{'
  @properties.each do |key, property|
    repr += "\n#{indent}  #{property_repr(indent, key, property)}"
  end
  repr += "\n#{indent}}"
  repr
end

#respond_to_missing?(_method_name, _include_private = false) ⇒ Boolean

rubocop:enable Style/MethodMissingSuper

Returns:

  • (Boolean)


42
43
44
# File 'lib/apigen/models/object_type.rb', line 42

def respond_to_missing?(_method_name, _include_private = false)
  true
end

#to_sObject



52
53
54
# File 'lib/apigen/models/object_type.rb', line 52

def to_s
  repr ''
end

#validate(model_registry) ⇒ Object



46
47
48
49
50
# File 'lib/apigen/models/object_type.rb', line 46

def validate(model_registry)
  @properties.each do |_key, property|
    model_registry.check_type property.type
  end
end