Class: RbsActivesupport::Include

Inherits:
Object
  • Object
show all
Defined in:
lib/rbs_activesupport/include.rb,
sig/rbs_activesupport/include.rbs

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context, module_path, options) ⇒ Include

Returns a new instance of Include.

Parameters:



14
15
16
17
18
# File 'lib/rbs_activesupport/include.rb', line 14

def initialize(context, module_path, options) #: void
  @context = context
  @module_path = module_path
  @options = options
end

Instance Attribute Details

#contextRBS::Namespace (readonly)

: RBS::Namespace

Returns:



7
8
9
# File 'lib/rbs_activesupport/include.rb', line 7

def context
  @context
end

#module_pathRBS::Namespace (readonly)

: RBS::Namespace

Returns:



8
9
10
# File 'lib/rbs_activesupport/include.rb', line 8

def module_path
  @module_path
end

#optionsHash[Symbol, untyped] (readonly)

: Hash[Symbol, untyped]

Returns:



9
10
11
# File 'lib/rbs_activesupport/include.rb', line 9

def options
  @options
end

Instance Method Details

#classmethods?Boolean

: boolish

Returns:



48
49
50
51
52
# File 'lib/rbs_activesupport/include.rb', line 48

def classmethods? #: boolish
  return false unless self.module

  self.module&.const_defined?(:ClassMethods)
end

#concern?Boolean

: boolish

Returns:



44
45
46
# File 'lib/rbs_activesupport/include.rb', line 44

def concern? #: boolish
  self.module&.singleton_class&.include?(ActiveSupport::Concern)
end

#explicit?Boolean

: bool

Returns:



54
55
56
# File 'lib/rbs_activesupport/include.rb', line 54

def explicit? #: bool
  !implicit?
end

#implicit?Boolean

: bool

Returns:



58
59
60
# File 'lib/rbs_activesupport/include.rb', line 58

def implicit? #: bool
  options.fetch(:implicit, false)
end

#method_calls_in_included_blockArray[Parser::MethodCall]

: Array

Returns:



70
71
72
73
74
75
76
# File 'lib/rbs_activesupport/include.rb', line 70

def method_calls_in_included_block #: Array[Parser::MethodCall]
  return [] unless module_name
  return [] unless parser

  method_calls = parser.method_calls[module_name] || []
  method_calls.select(&:included)
end

#moduleModule?

Returns:



35
36
37
38
39
40
41
42
# File 'lib/rbs_activesupport/include.rb', line 35

def module #: Module?
  return unless module_name

  modname = module_name.to_s.delete_suffix("::")
  return unless Object.const_defined?(modname)

  Object.const_get(modname)
end

#module_nameRBS::Namespace?

Returns:



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/rbs_activesupport/include.rb', line 21

def module_name #: RBS::Namespace?
  namespace = @context

  loop do
    modname = namespace + module_path
    return modname if Object.const_defined?(modname.to_s.delete_suffix("::"))

    break if namespace.empty?

    namespace = namespace.parent
  end
end

#nested_includesArray[Parser::MethodCall]

: Array

Returns:



62
63
64
65
66
67
68
# File 'lib/rbs_activesupport/include.rb', line 62

def nested_includes #: Array[Parser::MethodCall]
  return [] unless module_name
  return [] unless parser

  method_calls = parser.method_calls[module_name] || []
  method_calls.select { |call| call.name == :include && !call.included }
end

#parserParser?

Returns:



91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/rbs_activesupport/include.rb', line 91

def parser #: Parser?
  return nil unless module_name

  @parser ||= begin
    path, = Object.const_source_location(module_name.to_s.delete_suffix("::")) #: String?

    if path && File.exist?(path)
      Parser.new(parse_included_block: true).tap do |parser|
        parser.parse(File.read(path))
      end
    end
  end
end

#private?Boolean

: bool

Returns:



82
83
84
# File 'lib/rbs_activesupport/include.rb', line 82

def private? #: bool
  options.fetch(:private, false)
end

#public?Boolean

: bool

Returns:



78
79
80
# File 'lib/rbs_activesupport/include.rb', line 78

def public? #: bool
  !private?
end