Implemented allocate and initialize for LmProxy in the ruby bindings
authorMikael Hallendal <micke@imendio.com>
Thu, 10 Apr 2008 14:36:19 +0200
changeset 376 d54d6310c136
parent 375 1f0f637e3ae8
child 377 52b0968e0235
Implemented allocate and initialize for LmProxy in the ruby bindings
bindings/ruby/rblm-connection.c
bindings/ruby/rblm-proxy.c
--- a/bindings/ruby/rblm-connection.c	Thu Apr 10 14:13:14 2008 +0200
+++ b/bindings/ruby/rblm-connection.c	Thu Apr 10 14:36:19 2008 +0200
@@ -26,7 +26,7 @@
 }
 
 VALUE
-conn_allocate(VALUE klass)
+conn_allocate (VALUE klass)
 {
 	return Data_Wrap_Struct (klass, NULL, conn_free, NULL);
 }
--- a/bindings/ruby/rblm-proxy.c	Thu Apr 10 14:13:14 2008 +0200
+++ b/bindings/ruby/rblm-proxy.c	Thu Apr 10 14:36:19 2008 +0200
@@ -2,9 +2,68 @@
 
 VALUE lm_cProxy;
 
+static LmProxy *
+rb_lm_proxy_from_ruby_object (VALUE obj)
+{
+	LmProxy *proxy;
+
+	if (!rb_lm__is_kind_of (obj, lm_cProxy)) {
+		rb_raise (rb_eTypeError, "no a LmProxy");
+	}
+
+	Data_Get_Struct (obj, LmProxy, proxy);
+
+	return proxy;
+}
+
+void
+proxy_free (LmProxy *proxy)
+{
+	lm_proxy_unref (proxy);
+}
+
+VALUE
+proxy_allocate (VALUE klass)
+{
+	return Data_Wrap_Struct (klass, NULL, proxy_free, NULL);
+}
+
+VALUE
+proxy_initialize (int argc, VALUE *argv, VALUE self)
+{
+	LmProxy *proxy;
+	VALUE    type, server, port;
+	char    *server_str = NULL;
+
+	rb_scan_args (argc, argv, "12", &type, &server, &port);
+
+	proxy = lm_proxy_new (FIX2INT (type));
+
+	if (!NIL_P (server)) {
+		if (!rb_respond_to (server, rb_intern ("to_s"))) {
+			rb_raise (rb_eArgError, "server should respond to to_s");
+		} else {
+			VALUE str_val = rb_funcall (server, rb_intern ("to_s"), 0);
+			lm_proxy_set_server (proxy, StringValuePtr (str_val));
+		}
+	}
+
+	if (!NIL_P (port)) {
+		lm_proxy_set_port (proxy, NUM2UINT (port));
+	}
+
+	DATA_PTR (self) = proxy;
+
+	return self;
+}
+
 extern void
 Init_lm_proxy (VALUE lm_mLM)
 {
 	lm_cProxy = rb_define_class_under (lm_mLM, "Proxy", rb_cObject);
+
+	rb_define_alloc_func (lm_cProxy, proxy_allocate);
+
+	rb_define_method (lm_cProxy, "initialize", proxy_initialize, -1);
 }