Sha256: 1055604de63771b35019c996596f298f461f239a88f91d9272edded00d90b33a

Contents?: true

Size: 993 Bytes

Versions: 2

Compression:

Stored size: 993 Bytes

Contents

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

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

    extend FFI::Library

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

    private

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

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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
libvirt-ruby-1.0.1 lib/libvirt-ruby.rb
libvirt-ruby-1.0.0 lib/libvirt-ruby.rb