In Files

  • rbs-1.0.4/lib/rbs/definition_builder/method_builder.rb

Class/Module Index [+]

Quicksearch

RBS::DefinitionBuilder::MethodBuilder

Attributes

env[R]
instance_methods[R]
interface_methods[R]
singleton_methods[R]

Public Class Methods

new(env:) click to toggle source
 
               # File rbs-1.0.4/lib/rbs/definition_builder/method_builder.rb, line 83
def initialize(env))
  @env = env

  @instance_methods = {}
  @singleton_methods = {}
  @interface_methods = {}
end
            

Public Instance Methods

build_alias(methods, type, member:, accessibility:) click to toggle source
 
               # File rbs-1.0.4/lib/rbs/definition_builder/method_builder.rb, line 169
def build_alias(methods, type, member,, accessibility))
  defn = methods.methods[member.new_name] ||= Methods::Definition.empty(type: type, name: member.new_name)

  defn.originals << member
  defn.accessibilities << accessibility
end
            
build_attribute(methods, type, member:, accessibility:) click to toggle source
 
               # File rbs-1.0.4/lib/rbs/definition_builder/method_builder.rb, line 176
def build_attribute(methods, type, member,, accessibility))
  if member.is_a?(AST::Members::AttrReader) || member.is_a?(AST::Members::AttrAccessor)
    defn = methods.methods[member.name] ||= Methods::Definition.empty(type: type, name: member.name)

    defn.accessibilities << accessibility
    defn.originals << member
  end

  if member.is_a?(AST::Members::AttrWriter) || member.is_a?(AST::Members::AttrAccessor)
    defn = methods.methods[:"#{member.name}="] ||= Methods::Definition.empty(type: type, name: :"#{member.name}=")

    defn.accessibilities << accessibility
    defn.originals << member
  end
end
            
build_instance(type_name) click to toggle source
 
               # File rbs-1.0.4/lib/rbs/definition_builder/method_builder.rb, line 91
def build_instance(type_name)
  instance_methods[type_name] ||=
    begin
      entry = env.class_decls[type_name]
      args = Types::Variable.build(entry.type_params.each.map(&:name))
      type = Types::ClassInstance.new(name: type_name, args: args, location: nil)
      Methods.new(type: type).tap do |methods|
        entry.decls.each do |d|
          each_member_with_accessibility(d.decl.members) do |member, accessibility|
            case member
            when AST::Members::MethodDefinition
              if member.instance?
                build_method(methods, type, member: member, accessibility: accessibility)
              end
            when AST::Members::AttrReader, AST::Members::AttrWriter, AST::Members::AttrAccessor
              if member.kind == :instance
                build_attribute(methods, type, member: member, accessibility: accessibility)
              end
            when AST::Members::Alias
              if member.kind == :instance
                build_alias(methods, type, member: member, accessibility: accessibility)
              end
            end
          end
        end
      end.validate!
    end
end
            
build_interface(type_name) click to toggle source
 
               # File rbs-1.0.4/lib/rbs/definition_builder/method_builder.rb, line 149
def build_interface(type_name)
  interface_methods[type_name] ||=
    begin
      entry = env.interface_decls[type_name]
      args = Types::Variable.build(entry.decl.type_params.each.map(&:name))
      type = Types::Interface.new(name: type_name, args: args, location: nil)

      Methods.new(type: type).tap do |methods|
        entry.decl.members.each do |member|
          case member
          when AST::Members::MethodDefinition
            build_method(methods, type, member: member, accessibility: :public)
          when AST::Members::Alias
            build_alias(methods, type, member: member, accessibility: :public)
          end
        end
      end.validate!
    end
end
            
build_method(methods, type, member:, accessibility:) click to toggle source
 
               # File rbs-1.0.4/lib/rbs/definition_builder/method_builder.rb, line 192
def build_method(methods, type, member,, accessibility))
  defn = methods.methods[member.name] ||= Methods::Definition.empty(type: type, name: member.name)

  if member.overload?
    defn.overloads << member
  else
    defn.accessibilities << accessibility
    defn.originals << member
  end
end
            
build_singleton(type_name) click to toggle source
 
               # File rbs-1.0.4/lib/rbs/definition_builder/method_builder.rb, line 120
def build_singleton(type_name)
  singleton_methods[type_name] ||=
    begin
      entry = env.class_decls[type_name]
      type = Types::ClassSingleton.new(name: type_name, location: nil)

      Methods.new(type: type).tap do |methods|
        entry.decls.each do |d|
          each_member_with_accessibility(d.decl.members) do |member, accessibility|
            case member
            when AST::Members::MethodDefinition
              if member.singleton?
                build_method(methods, type, member: member, accessibility: accessibility)
              end
            when AST::Members::AttrReader, AST::Members::AttrWriter, AST::Members::AttrAccessor
              if member.kind == :singleton
                build_attribute(methods, type, member: member, accessibility: accessibility)
              end
            when AST::Members::Alias
              if member.kind == :singleton
                build_alias(methods, type, member: member, accessibility: accessibility)
              end
            end
          end
        end
      end.validate!
    end
end
            
each_member_with_accessibility(members, accessibility: :public) click to toggle source
 
               # File rbs-1.0.4/lib/rbs/definition_builder/method_builder.rb, line 203
def each_member_with_accessibility(members, accessibility: :public)
  members.each do |member|
    case member
    when AST::Members::Public
      accessibility = :public
    when AST::Members::Private
      accessibility = :private
    else
      yield member, accessibility
    end
  end
end