Class: Types::Method

Inherits:
Object
  • Object
show all
Includes:
Generic
Defined in:
lib/types/method.rb

Overview

A method type attached to a receiver type.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Generic

#|

Constructor Details

#initialize(receiver_type, argument_types, return_type) ⇒ Method

Returns a new instance of Method.



30
31
32
33
34
# File 'lib/types/method.rb', line 30

def initialize(receiver_type, argument_types, return_type)
	@receiver_type = receiver_type
	@argument_types = argument_types
	@return_type = return_type
end

Instance Attribute Details

#argument_typesObject (readonly)

Returns the value of attribute argument_types.



37
38
39
# File 'lib/types/method.rb', line 37

def argument_types
  @argument_types
end

#receiver_typeObject (readonly)

Returns the value of attribute receiver_type.



36
37
38
# File 'lib/types/method.rb', line 36

def receiver_type
  @receiver_type
end

#return_typeObject (readonly)

Returns the value of attribute return_type.



38
39
40
# File 'lib/types/method.rb', line 38

def return_type
  @return_type
end

Instance Method Details

#composite?Boolean

Returns:



40
41
42
# File 'lib/types/method.rb', line 40

def composite?
	true
end

#parse(input) ⇒ Object



62
63
64
65
66
67
68
69
70
71
# File 'lib/types/method.rb', line 62

def parse(input)
	case input
	when ::String
		receiver_type.instance_method(input)
	when ::Proc
		input
	else
		raise ArgumentError, "Cannot coerce #{input.inpsect} into Method!"
	end
end

#to_sObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/types/method.rb', line 44

def to_s
	buffer = ::String.new
	
	if @argument_types&.any?
		buffer << "Method(#{@receiver_type}, #{@argument_types.join(', ')}"
	else
		buffer << "Method(#{@receiver_type}, "
	end
	
	if @return_type
		buffer << "returns: #{@return_type})"
	else
		buffer << ")"
	end
	
	return buffer
end