Module: RbPod::Collection
- Defined in:
- ext/rbpod/collection.c
Class Method Summary collapse
-
.included(other) ⇒ Object
:nodoc:.
Instance Method Summary collapse
-
#[](index) ⇒ Object?
Given an integer
index
, return the item at that position in the collection. -
#each(argv) ⇒ Object
Iterate over the collection, passing each item to a given block.
-
#first ⇒ Object
Return the first item of the collection.
-
#last ⇒ Object
Return the last item of the collection.
-
#size ⇒ Integer
Return the total length of all items in the collection.
Class Method Details
.included(other) ⇒ Object
:nodoc:
128 129 130 131 132 133 134 135 136 137 138 |
# File 'ext/rbpod/collection.c', line 128
static VALUE rbpod_collection_included(VALUE self, VALUE other)
{
/* Collections should be Enumerable and Comparable. */
rb_include_module(other, rb_mEnumerable);
rb_include_module(other, rb_mComparable);
/* Override Enumerable with our methods. */
rb_extend_object(other, self);
return self;
}
|
Instance Method Details
#[](index) ⇒ Object?
Given an integer index
, return the item at that position in the collection.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'ext/rbpod/collection.c', line 11
static VALUE rbpod_collection_get(VALUE self, VALUE key)
{
GList *current = NULL, *collection = TYPED_DATA_PTR(self, GList);
VALUE klass = rb_funcall(self, rb_intern("type"), 0);
if (FIXNUM_P(key) == FALSE) {
return Qnil;
}
current = g_list_nth(collection, FIX2INT(key));
if (current == NULL) {
return Qnil;
}
return rb_class_new_instance_with_data(0, NULL, klass, current->data);
}
|
#each(*args) ⇒ Enumerator #each(*args) {|item| ... } ⇒ Enumerator
Iterate over the collection, passing each item to a given block. If no block was supplied, return an enumerator for the collection.
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'ext/rbpod/collection.c', line 88
static VALUE rbpod_collection_each(VALUE self, VALUE argv)
{
GList *current = NULL, *collection = TYPED_DATA_PTR(self, GList);
VALUE klass, item, arguments;
/* Return an enumerator if a block was not supplied. */
RETURN_ENUMERATOR(self, 0, 0);
/* What sort of items are we casting this data to? */
klass = rb_funcall(self, rb_intern("type"), 0);
/* Create a shallow copy of the passed arguments. */
arguments = rb_ary_dup(argv);
/* Prepend an empty element as a placeholder. */
rb_ary_unshift(arguments, Qnil);
/* If we were supplied a block, enumerate the entire list. */
for (current = collection; current != NULL; current = g_list_next(current)) {
/* TODO: Find a better workaround than this or Data_Wrap_Struct. */
item = rb_class_new_instance_with_data(0, NULL, klass, current->data);
rb_ary_store(arguments, 0, item);
rb_yield_splat(arguments);
}
return self;
}
|
#first ⇒ Object
Return the first item of the collection.
54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'ext/rbpod/collection.c', line 54
static VALUE rbpod_collection_first(VALUE self)
{
VALUE klass = rb_funcall(self, rb_intern("type"), 0);
GList *collection = TYPED_DATA_PTR(self, GList);
GList *current = g_list_first(collection);
if (current == NULL) {
return Qnil;
}
return rb_class_new_instance_with_data(0, NULL, klass, current->data);
}
|
#last ⇒ Object
Return the last item of the collection.
35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'ext/rbpod/collection.c', line 35
static VALUE rbpod_collection_last(VALUE self)
{
VALUE klass = rb_funcall(self, rb_intern("type"), 0);
GList *collection = TYPED_DATA_PTR(self, GList);
GList *current = g_list_last(collection);
if (current == NULL) {
return Qnil;
}
return rb_class_new_instance_with_data(0, NULL, klass, current->data);
}
|
#size ⇒ Integer
Return the total length of all items in the collection.
73 74 75 76 77 |
# File 'ext/rbpod/collection.c', line 73
static VALUE rbpod_collection_size(VALUE self)
{
GList *collection = TYPED_DATA_PTR(self, GList);
return INT2NUM(g_list_length(collection));
}
|