Class: Parlour::Types::Proc
Overview
A type which can be called as a function.
Defined Under Namespace
Classes: Parameter
Instance Attribute Summary collapse
-
#parameters ⇒ Object
readonly
Returns the value of attribute parameters.
-
#return_type ⇒ Object
readonly
Returns the value of attribute return_type.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #describe ⇒ Object
- #generate_rbi ⇒ Object
- #generate_rbs ⇒ Object
-
#initialize(parameters, return_type) ⇒ Proc
constructor
A new instance of Proc.
Methods inherited from 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
#parameters ⇒ Object (readonly)
Returns the value of attribute parameters.
544 545 546 |
# File 'lib/parlour/types.rb', line 544 def parameters @parameters end |
#return_type ⇒ Object (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 |
#describe ⇒ Object
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_rbi ⇒ Object
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_rbs ⇒ Object
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 |