Class: Parlour::Types::Proc

Inherits:
Type
  • Object
show all
Defined in:
lib/parlour/types.rb

Overview

A type which can be called as a function.

Defined Under Namespace

Classes: Parameter

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Type

#hash, to_type, #to_type

Constructor Details

#initialize(parameters, return_type) ⇒ Proc

Returns a new instance of Proc.



533
534
535
536
# File 'lib/parlour/types.rb', line 533

def initialize(parameters, return_type)
  @parameters = parameters
  @return_type = return_type && to_type(return_type)
end

Instance Attribute Details

#parametersObject (readonly)

Returns the value of attribute parameters.



544
545
546
# File 'lib/parlour/types.rb', line 544

def parameters
  @parameters
end

#return_typeObject (readonly)

Returns the value of attribute return_type.



547
548
549
# File 'lib/parlour/types.rb', line 547

def return_type
  @return_type
end

Instance Method Details

#==(other) ⇒ Object



539
540
541
# File 'lib/parlour/types.rb', line 539

def ==(other)
  Proc === other && parameters == other.parameters && return_type == other.return_type
end

#describeObject



568
569
570
571
572
573
574
# File 'lib/parlour/types.rb', line 568

def describe
  # For simplicity, use RBS with pre-described parameter types
  rbs_params = parameters.map do |param|
    RbsGenerator::Parameter.new(param.name, type: param.type.describe, required: param.default.nil?)
  end
  "(#{rbs_params.map(&:to_rbs_param).join(', ')}) -> #{return_type&.describe || 'void'}"
end

#generate_rbiObject



550
551
552
553
554
555
556
557
# File 'lib/parlour/types.rb', line 550

def generate_rbi
  rbi_params = parameters.map do |param|
    RbiGenerator::Parameter.new(param.name, type: param.type, default: param.default)
  end
  "T.proc.params(#{rbi_params.map(&:to_sig_param).join(', ')}).#{
    @return_type ? "returns(#{@return_type.generate_rbi})" : 'void'
  }"
end

#generate_rbsObject



560
561
562
563
564
565
# File 'lib/parlour/types.rb', line 560

def generate_rbs
  rbs_params = parameters.map do |param|
    RbsGenerator::Parameter.new(param.name, type: param.type, required: param.default.nil?)
  end
  "(#{rbs_params.map(&:to_rbs_param).join(', ')}) -> #{return_type&.generate_rbs || 'void'}"
end