Class: RaaP::Value::Module

Inherits:
BasicObject
Defined in:
lib/raap/value/module.rb

Overview

FIXME: consider self_types HINT: intersection?

Instance Method Summary collapse

Constructor Details

#initialize(type, size: 3) ⇒ Module

Returns a new instance of Module.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/raap/value/module.rb', line 8

def initialize(type, size: 3)
  @type = type.is_a?(::String) ? ::RaaP::RBS.parse_type(type) : type
  @size = size
  unless @type.instance_of?(::RBS::Types::ClassInstance)
    ::Kernel.raise ::TypeError, "not a module type: #{@type}"
  end

  one_instance_ancestors = ::RaaP::RBS.builder.ancestor_builder.one_instance_ancestors(@type.name.absolute!).self_types
  @self_type = if one_instance_ancestors.nil? || one_instance_ancestors.empty?
                 ::Object.new
               else
                 one_instance_ancestors.map do |a_instance|
                   if a_instance.args.empty?
                     # : BasicObject
                     a_instance.name.absolute!.to_s
                   else
                     # : _Each[Integer]
                     args = a_instance.args.zip(@type.args).map do |_var, instance|
                       if instance
                         instance.to_s
                       else
                         'untyped'
                       end
                     end
                     "#{a_instance.name}[#{args.map(&:to_s).join(', ')}]"
                   end
                 end.then do |ts|
                   if !ts.include?('::BasicObject') || ts.any? { |t| t.split('::').last&.start_with?('_') }
                     ts.unshift('Object')
                   end
                   Type.new(ts.uniq.join(' & ')).pick(size: size)
                 end
               end
  const = ::Object.const_get(@type.name.absolute!.to_s)
  BindCall.extend(self, const)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, **kwargs, &block) ⇒ Object



45
46
47
# File 'lib/raap/value/module.rb', line 45

def method_missing(name, *args, **kwargs, &block)
  @self_type.__send__(name, *args, **kwargs, &block)
end

Instance Method Details

#classObject



58
# File 'lib/raap/value/module.rb', line 58

def class = Value::Module

#inspectObject



57
# File 'lib/raap/value/module.rb', line 57

def inspect = "#<module #{@type} : #{BindCall.class(@self_type)} size=#{@size}>"

#respond_to?(name, include_all = false) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
52
53
54
55
# File 'lib/raap/value/module.rb', line 49

def respond_to?(name, include_all = false)
  if BindCall.instance_of?(@self_type, ::BasicObject)
    BindCall.respond_to?(@self_type, name, include_all)
  else
    @self_type.respond_to?(name, include_all)
  end
end