Class: RubyPythonBridge::RubyPyModule

Inherits:
RubyPyObject show all
Defined in:
lib/rubypython/wrapper_extensions.rb,
ext/rubypython_bridge/rp_object.c

Overview

A wrapper class for Python Modules.

Methods calls are delegated to the equivalent Python methods/functions. Attribute references return either the equivalent attribute converted to a native Ruby type, or wrapped reference to a Python object. RubyPyModule instances should be created through the use of RubyPython.import.

Instance Method Summary collapse

Methods inherited from RubyPyObject

#__name, #free_pobj, #inspect

Constructor Details

#initialize(mname) ⇒ Object

:nodoc:



129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'ext/rubypython_bridge/rp_object.c', line 129

VALUE rp_mod_init(VALUE self, VALUE mname)
{
	PObj* cself;
	Data_Get_Struct(self,PObj,cself);
	cself->pObject=rp_get_module(mname);
	VALUE rDict;
	PyObject *pModuleDict;
	pModuleDict=PyModule_GetDict(cself->pObject);
	Py_XINCREF(pModuleDict);
	rDict=rp_cla_from_class(pModuleDict);
	rb_iv_set(self,"@pdict",rDict);
	return self;
}

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(args) ⇒ Object

:nodoc:



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'ext/rubypython_bridge/rp_object.c', line 153

VALUE rp_mod_delegate(VALUE self,VALUE args)
{
	VALUE name,name_string,rDict,result;
	PObj *pDict;
	PyObject *pCalled;
	if(!rp_has_attr(self,rb_ary_entry(args,0)))
	{		
		int argc;
		
		VALUE *argv;
		argc=RARRAY(args)->len;
		argv=ALLOC_N(VALUE,argc);
		MEMCPY(argv,RARRAY(args)->ptr,VALUE,argc);
		return rb_call_super(argc,argv);
	}
	name=rb_ary_shift(args);
	name_string=rb_funcall(name,rb_intern("to_s"),0);
	
	rDict=rb_iv_get(self,"@pdict");
	Data_Get_Struct(rDict,PObj,pDict);
	pCalled=PyDict_GetItemString(pDict->pObject,STR2CSTR(name_string));
	result=ptor_obj_no_destruct(pCalled);
	if(rb_obj_is_instance_of(result,cRubyPyFunction))
	{
		return rp_call_func(pCalled,args);
	}
	return result;
	
}