Class: Taro::Declaration
- Inherits:
-
Object
- Object
- Taro::Declaration
- Defined in:
- lib/taro/declaration.rb
Overview
Framework-agnostic, abstract class. Descendants must implement #endpoint and (only for openapi export) #routes. See Taro::Rails::Declaration for an example.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#desc ⇒ Object
readonly
Returns the value of attribute desc.
-
#params ⇒ Object
readonly
Returns the value of attribute params.
-
#return_defs ⇒ Object
readonly
Returns the value of attribute return_defs.
-
#return_descriptions ⇒ Object
readonly
Returns the value of attribute return_descriptions.
-
#summary ⇒ Object
readonly
Returns the value of attribute summary.
-
#tags ⇒ Object
readonly
Returns the value of attribute tags.
Instance Method Summary collapse
- #add_info(summary, desc: nil, tags: nil) ⇒ Object
- #add_param(param_name, **attributes) ⇒ Object
- #add_return(nesting = nil) ⇒ Object
- #endpoint ⇒ Object
-
#initialize(for_klass = nil) ⇒ Declaration
constructor
A new instance of Declaration.
- #inspect ⇒ Object
- #polymorphic_route? ⇒ Boolean
-
#returns ⇒ Object
Return types are evaluated lazily to avoid unnecessary autoloading of all types in dev/test envs.
- #routes ⇒ Object
Constructor Details
#initialize(for_klass = nil) ⇒ Declaration
Returns a new instance of Declaration.
7 8 9 10 11 12 13 |
# File 'lib/taro/declaration.rb', line 7 def initialize(for_klass = nil) @params = Class.new(Taro::Types::InputType) @return_defs = {} @return_descriptions = {} Taro::CommonReturns.for(for_klass).each { |rd| add_return_def(rd) } end |
Instance Attribute Details
#desc ⇒ Object (readonly)
Returns the value of attribute desc.
5 6 7 |
# File 'lib/taro/declaration.rb', line 5 def desc @desc end |
#params ⇒ Object (readonly)
Returns the value of attribute params.
5 6 7 |
# File 'lib/taro/declaration.rb', line 5 def params @params end |
#return_defs ⇒ Object (readonly)
Returns the value of attribute return_defs.
5 6 7 |
# File 'lib/taro/declaration.rb', line 5 def return_defs @return_defs end |
#return_descriptions ⇒ Object (readonly)
Returns the value of attribute return_descriptions.
5 6 7 |
# File 'lib/taro/declaration.rb', line 5 def return_descriptions @return_descriptions end |
#summary ⇒ Object (readonly)
Returns the value of attribute summary.
5 6 7 |
# File 'lib/taro/declaration.rb', line 5 def summary @summary end |
#tags ⇒ Object (readonly)
Returns the value of attribute tags.
5 6 7 |
# File 'lib/taro/declaration.rb', line 5 def @tags end |
Instance Method Details
#add_info(summary, desc: nil, tags: nil) ⇒ Object
15 16 17 18 19 20 |
# File 'lib/taro/declaration.rb', line 15 def add_info(summary, desc: nil, tags: nil) summary.is_a?(String) || raise(Taro::ArgumentError, 'api summary must be a String') @summary = summary @desc = desc @tags = Array() if end |
#add_param(param_name, **attributes) ⇒ Object
22 23 24 25 26 27 |
# File 'lib/taro/declaration.rb', line 22 def add_param(param_name, **attributes) if attributes[:type] == 'Integer' attributes[:type] = 'Taro::Types::Scalar::IntegerParamType' end @params.field(param_name, **attributes) end |
#add_return(nesting = nil) ⇒ Object
29 30 31 32 |
# File 'lib/taro/declaration.rb', line 29 def add_return(nesting = nil, **) return_def = Taro::ReturnDef.new(nesting:, **) add_return_def(return_def) end |
#endpoint ⇒ Object
44 45 46 |
# File 'lib/taro/declaration.rb', line 44 def endpoint raise NotImplementedError, "implement ##{__method__} in subclass" end |
#inspect ⇒ Object
52 53 54 |
# File 'lib/taro/declaration.rb', line 52 def inspect "#<#{self.class} (#{endpoint || 'not finalized'})>" end |
#polymorphic_route? ⇒ Boolean
48 49 50 |
# File 'lib/taro/declaration.rb', line 48 def polymorphic_route? routes.size > 1 end |
#returns ⇒ Object
Return types are evaluated lazily to avoid unnecessary autoloading of all types in dev/test envs.
36 37 38 |
# File 'lib/taro/declaration.rb', line 36 def returns @returns ||= evaluate_return_defs end |
#routes ⇒ Object
40 41 42 |
# File 'lib/taro/declaration.rb', line 40 def routes raise NotImplementedError, "implement ##{__method__} in subclass" end |