class Yoda::Model::FunctionSignatures::TypeBuilder

Attributes

parameters[R]

@return [ParameterList]

tag_list[R]

@return [Array<Store::Objects::Tag>]

Public Class Methods

new(parameters, tag_list) click to toggle source

@param parameters [ParameterList] @param tag_list [Array<Store::Objects::Tag>] @param lexical_scope [Array<Store::Objects::Tag>]

# File lib/yoda/model/function_signatures/type_builder.rb, line 14
def initialize(parameters, tag_list)
  @parameters = parameters
  @tag_list = tag_list
end

Public Instance Methods

type() click to toggle source

@return [Types::FunctionType]

# File lib/yoda/model/function_signatures/type_builder.rb, line 20
def type
  @type ||= begin
    if !type_tags.empty?
      parsed_type = parse_type_tag(type_tags.first)
      parsed_type.is_a?(Types::FunctionType) ? parsed_type : Types::FunctionType.new(return_type: parsed_type)
    else
      Types::FunctionType.new(return_type: return_types.first || Types::UnknownType.new('nodoc'), **parameter_options)
    end
  end
end
type_of(param) click to toggle source

@param param [String] @return [Types::Base]

# File lib/yoda/model/function_signatures/type_builder.rb, line 33
def type_of(param)
  param_type_table[param] || Types::UnknownType.new('nodoc')
end

Private Instance Methods

convert_lexical_scope_literals(lexical_scope_literals) click to toggle source

@param lexical_scope_literals [Array<String>] @param [Array<Path>]

# File lib/yoda/model/function_signatures/type_builder.rb, line 95
def convert_lexical_scope_literals(lexical_scope_literals)
  lexical_scope_literals.map { |literal| Path.new(literal) } + [Path.new('Object')]
end
param_tags() click to toggle source

@return [Array<Store::Objects::Tag>]

# File lib/yoda/model/function_signatures/type_builder.rb, line 71
def param_tags
  @param_tags ||= tag_list.select { |tag| tag.tag_name == 'param' }
end
param_type_table() click to toggle source

@return [{ String => Types::Base }]

# File lib/yoda/model/function_signatures/type_builder.rb, line 89
def param_type_table
  @param_type_table ||= param_tags.map(&:name).zip(parse_yard_type_tags(param_tags)).group_by(&:first).map { |k, v| [k, Types::UnionType.new(v.map(&:last))] }.to_h
end
parameter_options() click to toggle source

@return [Hash]

# File lib/yoda/model/function_signatures/type_builder.rb, line 47
def parameter_options
  @parameter_options ||= {
    required_parameters: parameters.required_parameters.map(&method(:type_of)),
    optional_parameters: parameters.optional_parameters.map(&:first).map(&method(:type_of)),
    rest_parameter: parameters.rest_parameter ? type_of(parameters.rest_parameter) : nil,
    post_parameters: parameters.post_parameters.map(&method(:type_of)),
    required_keyword_parameters: parameters.required_keyword_parameters.map(&method(:type_of)),
    optional_keyword_parameters: parameters.optional_keyword_parameters.map(&:first).map(&method(:type_of)),
    keyword_rest_parameter: parameters.keyword_rest_parameter ? type_of(parameters.keyword_rest_parameter) : nil,
    block_parameter: parameters.block_parameter ? type_of(parameters.block_parameter) : nil,
  }
end
parse_type_tag(tag) click to toggle source

@param type_tag [Store::Objects::Tag] @return [Types::FunctionType]

# File lib/yoda/model/function_signatures/type_builder.rb, line 41
def parse_type_tag(tag)
  parsed_type = Parsing::TypeParser.new.safe_parse(tag.text).change_root(convert_lexical_scope_literals(tag.lexical_scope))
  parsed_type.is_a?(Types::FunctionType) ? parsed_type : Types::FunctionType.new(return_type: parsed_type)
end
parse_yard_type_tags(tags) click to toggle source

@param tags [Array<Store::Objects::Tag>] @return [Array<Types::Base>]

# File lib/yoda/model/function_signatures/type_builder.rb, line 82
def parse_yard_type_tags(tags)
  tags.map do |tag|
    tag.yard_types.empty? ? Types::UnknownType.new('nodoc') : Types.parse_type_strings(tag.yard_types).change_root(convert_lexical_scope_literals(tag.lexical_scope))
  end
end
return_tags() click to toggle source

@return [Array<Store::Objects::Tag>]

# File lib/yoda/model/function_signatures/type_builder.rb, line 61
def return_tags
  @return_tag ||= tag_list.select { |tag| tag.tag_name == 'return' }
end
return_types() click to toggle source

@return [Array<Types::Base>]

# File lib/yoda/model/function_signatures/type_builder.rb, line 76
def return_types
  @return_types ||= parse_yard_type_tags(return_tags)
end
type_tags() click to toggle source

@return [Array<Store::Objects::Tag>]

# File lib/yoda/model/function_signatures/type_builder.rb, line 66
def type_tags
  @type_tag ||= tag_list.select { |tag| tag.tag_name == 'type' }
end