Class: Apigen::ObjectType
- Inherits:
-
Object
- Object
- Apigen::ObjectType
- Defined in:
- lib/apigen/models/object_type.rb
Overview
ObjectType represents an object type, with specific properties.
Instance Attribute Summary collapse
-
#properties ⇒ Object
readonly
Returns the value of attribute properties.
Instance Method Summary collapse
- #add(&block) ⇒ Object
-
#initialize ⇒ ObjectType
constructor
A new instance of ObjectType.
-
#method_missing(*args, &block) ⇒ Object
rubocop:disable Style/MethodMissingSuper.
- #property(property_name, property_shape, &block) ⇒ Object
- #remove(*property_names) ⇒ Object
- #repr(indent) ⇒ Object
-
#respond_to_missing?(_method_name, _include_private = false) ⇒ Boolean
rubocop:enable Style/MethodMissingSuper.
- #to_s ⇒ Object
- #validate(model_registry) ⇒ Object
Constructor Details
#initialize ⇒ ObjectType
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(*args, &block) ⇒ Object
rubocop:disable Style/MethodMissingSuper
27 28 29 |
# File 'lib/apigen/models/object_type.rb', line 27 def method_missing(*args, &block) property(*args, &block) end |
Instance Attribute Details
#properties ⇒ Object (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 |
#property(property_name, property_shape, &block) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 |
# 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) ⇒ 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
59 60 61 62 63 64 65 66 |
# 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) ⇒ Boolean
rubocop:enable Style/MethodMissingSuper
32 33 34 |
# File 'lib/apigen/models/object_type.rb', line 32 def respond_to_missing?(_method_name, _include_private = false) true end |
#to_s ⇒ Object
55 56 57 |
# File 'lib/apigen/models/object_type.rb', line 55 def to_s repr '' end |
#validate(model_registry) ⇒ Object
49 50 51 52 53 |
# 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 |