Sha256: cd76d57fb9a2a5d04acb06cb0b392f262e2c2628d64a7980c252f3ff20ae508b

Contents?: true

Size: 1.41 KB

Versions: 13

Compression:

Stored size: 1.41 KB

Contents

module Neovim
  # @abstract Superclass for all +nvim+ remote objects.
  #
  # @see Buffer
  # @see Window
  # @see Tabpage
  class RemoteObject
    attr_reader :index

    def initialize(index, session)
      @index = index
      @session = session
      @api = session.api
    end

    # Serialize object to MessagePack.
    #
    # @param packer [MessagePack::Packer]
    # @return [String]
    def to_msgpack(packer)
      packer.pack(@index)
    end

    # Intercept method calls and delegate to appropriate RPC methods.
    def method_missing(method_name, *args)
      if func = @api.function(qualify(method_name))
        func.call(@session, @index, *args)
      else
        super
      end
    end

    # Extend +respond_to?+ to support RPC methods.
    def respond_to?(method_name)
      super || rpc_methods.include?(method_name.to_sym)
    end

    # Extend +methods+ to include RPC methods
    def methods
      super | rpc_methods
    end

    # Extend +==+ to only look at class and index.
    def ==(other)
      other.class.equal?(self.class) && @index == other.index
    end

    private

    def rpc_methods
      @api.functions_with_prefix(function_prefix).map do |func|
        func.name.sub(/\A#{function_prefix}/, "").to_sym
      end
    end

    def function_prefix
      "#{self.class.to_s.split("::").last.downcase}_"
    end

    def qualify(method_name)
      :"#{function_prefix}#{method_name}"
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
neovim-0.4.0 lib/neovim/remote_object.rb
neovim-0.3.3 lib/neovim/remote_object.rb
neovim-0.3.2 lib/neovim/remote_object.rb
neovim-0.3.1 lib/neovim/remote_object.rb
neovim-0.3.0 lib/neovim/remote_object.rb
neovim-0.2.5 lib/neovim/remote_object.rb
neovim-0.2.4 lib/neovim/remote_object.rb
neovim-0.2.3 lib/neovim/remote_object.rb
neovim-0.2.2 lib/neovim/remote_object.rb
neovim-0.2.1 lib/neovim/remote_object.rb
neovim-0.2.0 lib/neovim/remote_object.rb
neovim-0.1.0 lib/neovim/remote_object.rb
neovim-0.0.6 lib/neovim/remote_object.rb