Sha256: 09801a6aea26b588833df4ea52be8fbb80abc745bea6fec7d15db817e0b44f09

Contents?: true

Size: 920 Bytes

Versions: 1

Compression:

Stored size: 920 Bytes

Contents

require "libvirt-ruby/version"
require "ffi"

module Libvirt
  autoload :Exceptions, 'libvirt-ruby/exceptions'

  class Base
    extend FFI::Library

    def method_missing(method, *args)
      args.flatten!
      return_type = args.delete(args.last)
      raise Libvirt::Exceptions::NoReturnParameter unless return_type
      dispatcher(method, args, return_type)
    end

    private

    def dispatcher(method, args = [], return_type = nil)
      raise Libvirt::Exceptions::WrongCall unless not_direct_call?
      begin
        ffi_lib "libvirt"
        attach_function method.to_s, method.to_s, args, return_type
      rescue FFI::NotFoundError
        raise Libvirt::Exceptions::InvalidFunction.new(method.to_s)
      rescue LoadError
        raise Libvirt::Exceptions::MissingLib
      end
    end

    def not_direct_call?
      caller[1][/`.*'/] and caller[1][/`.*'/][1..-2] == 'method_missing'
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
libvirt-ruby-2.0.0 lib/libvirt-ruby.rb