#include "reflex/ruby/selector.h" #include #include "defs.h" RUCY_DEFINE_VALUE_OR_ARRAY_FROM_TO(Reflex::Selector) #define THIS to< Reflex::Selector*>(self) #define C_THIS to(self) #define CHECK RUCY_CHECK_OBJ(Reflex::Selector, self) static VALUE alloc(VALUE klass) { return new_type(klass); } static VALUE initialize_copy(VALUE self, VALUE obj) { CHECK; *THIS = to(obj); return self; } static VALUE set_name(VALUE self, VALUE name) { CHECK; THIS->set_name(name.is_nil() ? NULL : name.c_str()); } static VALUE get_name(VALUE self) { CHECK; const char* name = C_THIS->name(); return name ? value(name) : nil(); } static VALUE add_tag(VALUE self, VALUE tag) { CHECK; THIS->add_tag(tag.c_str()); } static VALUE remove_tag(VALUE self, VALUE tag) { CHECK; THIS->remove_tag(tag.c_str()); } static VALUE clear_tags(VALUE self) { CHECK; THIS->clear_tags(); } static VALUE has_tag(VALUE self, VALUE tag) { CHECK; return value(C_THIS->has_tag(tag.c_str())); } static VALUE each_tag(VALUE self) { CHECK; Value ret; Reflex::Selector::const_iterator end = C_THIS->end(); for (Reflex::Selector::const_iterator it = C_THIS->begin(); it != end; ++it) ret = rb_yield(value(*it)); return ret; } static VALUE is_empty(VALUE self) { CHECK; return value(C_THIS->empty()); } static VALUE contains(VALUE self, VALUE selector) { CHECK; return value(C_THIS->contains(to(selector))); } static VALUE equal(VALUE self, VALUE selector) { CHECK; return value(*C_THIS == to(selector)); } static Class cSelector; void Init_reflex_selector () { Module mReflex = rb_define_module("Reflex"); cSelector = rb_define_class_under(mReflex, "Selector", rb_cObject); rb_define_alloc_func(cSelector, alloc); rb_define_private_method(cSelector, "initialize_copy", RUBY_METHOD_FUNC(initialize_copy), 1); rb_define_method(cSelector, "name=", RUBY_METHOD_FUNC(set_name), 1); rb_define_method(cSelector, "name", RUBY_METHOD_FUNC(get_name), 0); rb_define_method(cSelector, "add_tag", RUBY_METHOD_FUNC(add_tag), 1); rb_define_method(cSelector, "remove_tag", RUBY_METHOD_FUNC(remove_tag), 1); rb_define_method(cSelector, "clear_tags", RUBY_METHOD_FUNC(clear_tags), 0); cSelector.define_method("tag?", has_tag); rb_define_method(cSelector, "each_tag", RUBY_METHOD_FUNC(each_tag), 0); cSelector.define_method("empty?", is_empty); rb_define_method(cSelector, "contains", RUBY_METHOD_FUNC(contains), 1); cSelector.define_method("==", equal); } namespace Rucy { template <> Reflex::Selector value_to (int argc, const Value* argv, bool convert) { if (argc == 1 && argv->is_array()) { argc = argv->size(); argv = argv->as_array(); } assert(argc > 0 && argv); if (convert) { if (argc == 1 && (argv->is_s() || argv->is_sym())) return Reflex::Selector(argv[0].c_str()); } if (argc != 1) argument_error(__FILE__, __LINE__); return value_to(*argv, convert); } }// Rucy namespace Reflex { Class selector_class () { return cSelector; } }// Reflex