Class: RBS::Definition

Inherits:
Object
  • Object
show all
Defined in:
lib/rbs/definition.rb

Defined Under Namespace

Modules: Ancestor Classes: Method, Variable

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(declaration:, self_type:, ancestors:) ⇒ Definition

Returns a new instance of Definition.



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/rbs/definition.rb', line 87

def initialize(declaration:, self_type:, ancestors:)
  unless declaration.is_a?(AST::Declarations::Class) ||
    declaration.is_a?(AST::Declarations::Module) ||
    declaration.is_a?(AST::Declarations::Interface) ||
    declaration.is_a?(AST::Declarations::Extension)
    raise "Declaration should be a class, module, or interface: #{declaration.name}"
  end

  unless (self_type.is_a?(Types::ClassSingleton) || self_type.is_a?(Types::Interface) || self_type.is_a?(Types::ClassInstance)) && self_type.name == declaration.name.absolute!
    raise "self_type should be the type of declaration: #{self_type}"
  end

  @self_type = self_type
  @declaration = declaration
  @methods = {}
  @instance_variables = {}
  @class_variables = {}
  @ancestors = ancestors
end

Instance Attribute Details

#ancestorsObject (readonly)

Returns the value of attribute ancestors.



85
86
87
# File 'lib/rbs/definition.rb', line 85

def ancestors
  @ancestors
end

#class_variablesObject (readonly)

Returns the value of attribute class_variables.



84
85
86
# File 'lib/rbs/definition.rb', line 84

def class_variables
  @class_variables
end

#declarationObject (readonly)

Returns the value of attribute declaration.



80
81
82
# File 'lib/rbs/definition.rb', line 80

def declaration
  @declaration
end

#instance_variablesObject (readonly)

Returns the value of attribute instance_variables.



83
84
85
# File 'lib/rbs/definition.rb', line 83

def instance_variables
  @instance_variables
end

#methodsObject (readonly)

Returns the value of attribute methods.



82
83
84
# File 'lib/rbs/definition.rb', line 82

def methods
  @methods
end

#self_typeObject (readonly)

Returns the value of attribute self_type.



81
82
83
# File 'lib/rbs/definition.rb', line 81

def self_type
  @self_type
end

Instance Method Details

#class?Boolean

Returns:

  • (Boolean)


111
112
113
# File 'lib/rbs/definition.rb', line 111

def class?
  declaration.is_a?(AST::Declarations::Class)
end

#class_type?Boolean

Returns:

  • (Boolean)


119
120
121
# File 'lib/rbs/definition.rb', line 119

def class_type?
  @self_type.is_a?(Types::ClassSingleton)
end

#each_type(&block) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/rbs/definition.rb', line 144

def each_type(&block)
  if block_given?
    methods.each_value do |method|
      if method.defined_in == self.declaration
        method.method_types.each do |method_type|
          method_type.each_type(&block)
        end
      end
    end

    instance_variables.each_value do |var|
      if var.declared_in == self.declaration
        yield var.type
      end
    end

    class_variables.each_value do |var|
      if var.declared_in == self.declaration
        yield var.type
      end
    end
  else
    enum_for :each_type
  end
end

#instance_type?Boolean

Returns:

  • (Boolean)


123
124
125
# File 'lib/rbs/definition.rb', line 123

def instance_type?
  @self_type.is_a?(Types::ClassInstance)
end

#interface_type?Boolean

Returns:

  • (Boolean)


127
128
129
# File 'lib/rbs/definition.rb', line 127

def interface_type?
  @self_type.is_a?(Types::Interface)
end

#module?Boolean

Returns:

  • (Boolean)


115
116
117
# File 'lib/rbs/definition.rb', line 115

def module?
  declaration.is_a?(AST::Declarations::Module)
end

#nameObject



107
108
109
# File 'lib/rbs/definition.rb', line 107

def name
  declaration.name
end

#type_paramsObject



131
132
133
# File 'lib/rbs/definition.rb', line 131

def type_params
  @self_type.args.map(&:name)
end

#type_params_declObject



135
136
137
138
139
140
141
142
# File 'lib/rbs/definition.rb', line 135

def type_params_decl
  case declaration
  when AST::Declarations::Extension
    nil
  else
    declaration.type_params
  end
end