ext/slim_attrib_ext.c in slim-attributes-0.6.0 vs ext/slim_attrib_ext.c in slim-attributes-0.6.4

- old
+ new

@@ -1,10 +1,15 @@ // Author: Stephen Sykes // http://pennysmalls.com #include "ruby.h" + +#ifdef HAVE_RUBY_ST_H +#include "ruby/st.h" +#else #include "st.h" +#endif #include <mysql.h> #include <errmsg.h> #include <mysqld_error.h> @@ -150,18 +155,22 @@ // // Note: this method currently ignores any columns that have been assigned to using // []= before calling dup (the original values will be seen in the dup). This works ok // for active record usage, but perhaps could cause unexpected behaviour if model // attributes are dupped by the user after changing them. -static VALUE dup(VALUE obj) { +static VALUE slim_dup(VALUE obj) { VALUE frh, field_indexes; int nf, i; char *row_info_space; if (REAL_HASH_EXISTS) return rb_obj_dup(rb_ivar_get(obj, real_hash_id)); +#ifdef RHASH_SIZE + nf = RHASH_SIZE(field_indexes); +#else nf = RHASH(field_indexes)->tbl->num_entries; +#endif row_info_space = ruby_xmalloc(nf); // dup needs its own set of flags if (!row_info_space) rb_raise(rb_eNoMemError, "out of memory"); memcpy(row_info_space, GetCharPtr(rb_ivar_get(obj, row_info_id)), nf); for (i=0; i < nf; i++) row_info_space[i] &= ~SLIM_IS_SET; // remove any set flags frh = rb_class_new_instance(0, NULL, cRowHash); // make the new row data object @@ -175,12 +184,17 @@ // Calls to model property methods in AR cause a call to has_key?, so it // is implemented here in C for speed. static VALUE has_key(VALUE obj, VALUE name) { VALUE field_indexes; +#ifdef RHASH_TBL + if (REAL_HASH_EXISTS) return (st_lookup(RHASH_TBL(rb_ivar_get(obj, real_hash_id)), name, 0) ? Qtrue : Qfalse); + else return (st_lookup(RHASH_TBL(field_indexes), name, 0) ? Qtrue : Qfalse); +#else if (REAL_HASH_EXISTS) return (st_lookup(RHASH(rb_ivar_get(obj, real_hash_id))->tbl, name, 0) ? Qtrue : Qfalse); else return (st_lookup(RHASH(field_indexes)->tbl, name, 0) ? Qtrue : Qfalse); +#endif } void Init_slim_attrib_ext() { int i; char col_name[16]; @@ -193,10 +207,10 @@ cClass = rb_define_class("CObjects", cRowHash); // set up methods rb_define_private_method(cRowHash, "fetch_by_index", (VALUE(*)(ANYARGS))fetch_by_index, 1); rb_define_method(cRowHash, "[]", (VALUE(*)(ANYARGS))slim_fetch, 1); rb_define_method(cRowHash, "[]=", (VALUE(*)(ANYARGS))set_element, 2); - rb_define_method(cRowHash, "dup", (VALUE(*)(ANYARGS))dup, 0); + rb_define_method(cRowHash, "dup", (VALUE(*)(ANYARGS))slim_dup, 0); rb_define_method(cRowHash, "has_key?", (VALUE(*)(ANYARGS))has_key, 1); // set up some symbols that we will need pointers_id = rb_intern("@pointers"); row_info_id = rb_intern("@row_info"); field_indexes_id = rb_intern("@field_indexes");