Class: Taro::Declaration

Inherits:
Object
  • Object
show all
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

Rails::Declaration

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#descObject (readonly)

Returns the value of attribute desc.



5
6
7
# File 'lib/taro/declaration.rb', line 5

def desc
  @desc
end

#paramsObject (readonly)

Returns the value of attribute params.



5
6
7
# File 'lib/taro/declaration.rb', line 5

def params
  @params
end

#return_defsObject (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_descriptionsObject (readonly)

Returns the value of attribute return_descriptions.



5
6
7
# File 'lib/taro/declaration.rb', line 5

def return_descriptions
  @return_descriptions
end

#summaryObject (readonly)

Returns the value of attribute summary.



5
6
7
# File 'lib/taro/declaration.rb', line 5

def summary
  @summary
end

#tagsObject (readonly)

Returns the value of attribute tags.



5
6
7
# File 'lib/taro/declaration.rb', line 5

def tags
  @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(tags) if tags
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

#endpointObject

Raises:

  • (NotImplementedError)


44
45
46
# File 'lib/taro/declaration.rb', line 44

def endpoint
  raise NotImplementedError, "implement ##{__method__} in subclass"
end

#inspectObject



52
53
54
# File 'lib/taro/declaration.rb', line 52

def inspect
  "#<#{self.class} (#{endpoint || 'not finalized'})>"
end

#polymorphic_route?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/taro/declaration.rb', line 48

def polymorphic_route?
  routes.size > 1
end

#returnsObject

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

#routesObject

Raises:

  • (NotImplementedError)


40
41
42
# File 'lib/taro/declaration.rb', line 40

def routes
  raise NotImplementedError, "implement ##{__method__} in subclass"
end