123456789_123456789_123456789_123456789_123456789_

Class: RBS::Definition

Relationships & Source Files
Namespace Children
Modules:
Classes:
Inherits: Object
Defined in: lib/rbs/definition.rb

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(type_name:, entry:, self_type:, ancestors:) ⇒ Definition

[ GitHub ]

  
# File 'lib/rbs/definition.rb', line 266

def initialize(type_name:, entry:, self_type:, ancestors:)
  case entry
  when Environment::ClassEntry, Environment::ModuleEntry
    # ok
  else
    unless entry.decl.is_a?(AST::Declarations::Interface)
      raise "Declaration should be a class, module, or interface: #{type_name}"
    end
  end

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

  @type_name = type_name
  @self_type = self_type
  @entry = entry
  @methods = {}
  @instance_variables = {}
  @class_variables = {}
  @ancestors = ancestors
end

Instance Attribute Details

#ancestors (readonly)

[ GitHub ]

  
# File 'lib/rbs/definition.rb', line 260

attr_reader :ancestors

#class?Boolean (readonly)

[ GitHub ]

  
# File 'lib/rbs/definition.rb', line 289

def class?
  entry.is_a?(Environment::ClassEntry)
end

#class_type?Boolean (readonly)

[ GitHub ]

  
# File 'lib/rbs/definition.rb', line 306

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

#class_variables (readonly)

[ GitHub ]

  
# File 'lib/rbs/definition.rb', line 264

attr_reader :class_variables

#entry (readonly)

[ GitHub ]

  
# File 'lib/rbs/definition.rb', line 259

attr_reader :entry

#instance_type?Boolean (readonly)

[ GitHub ]

  
# File 'lib/rbs/definition.rb', line 310

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

#instance_variables (readonly)

[ GitHub ]

  
# File 'lib/rbs/definition.rb', line 263

attr_reader :instance_variables

#interface?Boolean (readonly)

[ GitHub ]

  
# File 'lib/rbs/definition.rb', line 297

def interface?
  case en = entry
  when Environment::SingleEntry
    en.decl.is_a?(AST::Declarations::Interface)
  else
    false
  end
end

#interface_type?Boolean (readonly)

[ GitHub ]

  
# File 'lib/rbs/definition.rb', line 314

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

#methods (readonly)

[ GitHub ]

  
# File 'lib/rbs/definition.rb', line 262

attr_reader :methods

#module?Boolean (readonly)

[ GitHub ]

  
# File 'lib/rbs/definition.rb', line 293

def module?
  entry.is_a?(Environment::ModuleEntry)
end

#self_type (readonly)

[ GitHub ]

  
# File 'lib/rbs/definition.rb', line 261

attr_reader :self_type

#type_name (readonly)

[ GitHub ]

  
# File 'lib/rbs/definition.rb', line 258

attr_reader :type_name

Instance Method Details

#each_type(&block)

[ GitHub ]

  
# File 'lib/rbs/definition.rb', line 351

def each_type(&block)
  if block
    methods.each_value do |method|
      if method.defined_in == type_name
        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 == type_name
        yield var.type
      end
    end

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

#map_method_type(&block)

[ GitHub ]

  
# File 'lib/rbs/definition.rb', line 341

def map_method_type(&block)
  definition = self.class.new(type_name: type_name, self_type: self_type, ancestors: ancestors, entry: entry)

  definition.methods.merge!(methods.transform_values {|method| method.map_method_type(&block) })
  definition.instance_variables.merge!(instance_variables)
  definition.class_variables.merge!(class_variables)

  definition
end

#sub(s)

[ GitHub ]

  
# File 'lib/rbs/definition.rb', line 331

def sub(s)
  definition = self.class.new(type_name: type_name, self_type: _ = self_type.sub(s), ancestors: ancestors, entry: entry)

  definition.methods.merge!(methods.transform_values {|method| method.sub(s) })
  definition.instance_variables.merge!(instance_variables.transform_values {|v| v.sub(s) })
  definition.class_variables.merge!(class_variables.transform_values {|v| v.sub(s) })

  definition
end

#type_params

[ GitHub ]

  
# File 'lib/rbs/definition.rb', line 318

def type_params
  type_params_decl.each.map(&:name)
end

#type_params_decl

[ GitHub ]

  
# File 'lib/rbs/definition.rb', line 322

def type_params_decl
  case en = entry
  when Environment::ClassEntry, Environment::ModuleEntry
    en.type_params
  when Environment::SingleEntry
    en.decl.type_params
  end
end