Class: Apigen::ModelRegistry
- Inherits:
-
Object
- Object
- Apigen::ModelRegistry
- Defined in:
- lib/apigen/models/registry.rb
Overview
ModelRegistry is where all model definitions are stored.
Instance Attribute Summary collapse
-
#models ⇒ Object
readonly
Returns the value of attribute models.
Instance Method Summary collapse
- #check_type(type) ⇒ Object
-
#initialize ⇒ ModelRegistry
constructor
A new instance of ModelRegistry.
- #model(name, &block) ⇒ Object
- #to_s ⇒ Object
- #validate ⇒ Object
Constructor Details
#initialize ⇒ ModelRegistry
11 12 13 |
# File 'lib/apigen/models/registry.rb', line 11 def initialize @models = {} end |
Instance Attribute Details
#models ⇒ Object (readonly)
Returns the value of attribute models.
9 10 11 |
# File 'lib/apigen/models/registry.rb', line 9 def models @models end |
Instance Method Details
#check_type(type) ⇒ Object
33 34 35 |
# File 'lib/apigen/models/registry.rb', line 33 def check_type(type) type.validate self end |
#model(name, &block) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/apigen/models/registry.rb', line 15 def model(name, &block) error = if @models.key? name "Model :#{name} is declared twice." elsif !block_given? 'You must pass a block when calling `model`.' end raise error unless error.nil? model = Apigen::Model.new name model.instance_eval(&block) @models[model.name] = model end |
#to_s ⇒ Object
37 38 39 40 41 |
# File 'lib/apigen/models/registry.rb', line 37 def to_s @models.map do |key, model| "#{key}: #{model}" end.join "\n" end |
#validate ⇒ Object
27 28 29 30 31 |
# File 'lib/apigen/models/registry.rb', line 27 def validate @models.each do |_key, model| model.validate self end end |