class Apigen::ObjectType
ObjectType
represents an object type, with specific properties.
Attributes
properties[R]
Public Class Methods
new()
click to toggle source
# File lib/apigen/models/object_type.rb, line 12 def initialize @properties = {} end
Public Instance Methods
add(&block)
click to toggle source
# File lib/apigen/models/object_type.rb, line 16 def add(&block) instance_eval(&block) end
method_missing(*args, &block)
click to toggle source
rubocop:disable Style/MethodMissingSuper
# File lib/apigen/models/object_type.rb, line 27 def method_missing(*args, &block) property(*args, &block) end
property(property_name, property_shape, &block)
click to toggle source
# File lib/apigen/models/object_type.rb, line 36 def property(property_name, property_shape, &block) ensure_correctness(property_name, property_shape) 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
remove(*property_names)
click to toggle source
# 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)
click to toggle source
# File lib/apigen/models/object_type.rb, line 59 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)
click to toggle source
rubocop:enable Style/MethodMissingSuper
# File lib/apigen/models/object_type.rb, line 32 def respond_to_missing?(_method_name, _include_private = false) true end
to_s()
click to toggle source
# File lib/apigen/models/object_type.rb, line 55 def to_s repr '' end
validate(model_registry)
click to toggle source
# File lib/apigen/models/object_type.rb, line 49 def validate(model_registry) @properties.each do |_key, property| model_registry.check_type property.type end end
Private Instance Methods
ensure_correctness(property_name, property_shape)
click to toggle source
# File lib/apigen/models/object_type.rb, line 70 def ensure_correctness(property_name, property_shape) error = if @properties.key? property_name "Property :#{property_name} is defined multiple times." elsif !property_shape.is_a? Symbol "Property type must be a symbol, found #{property_shape}." end raise error unless error.nil? end
property_repr(indent, key, property)
click to toggle source
# File lib/apigen/models/object_type.rb, line 79 def property_repr(indent, key, property) type_repr = if property.type.respond_to? :repr property.type.repr(indent + ' ') else property.type.to_s end type_repr += '?' unless property.required? "#{key}: #{type_repr}" end