1/* -*- c-basic-offset: 2 -*- */
2/*
3 Copyright(C) 2014-2015 Brazil
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License version 2.1 as published by the Free Software Foundation.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17*/
18
19#include "../grn_ctx_impl.h"
20
21#ifdef GRN_WITH_MRUBY
22#include <mruby.h>
23#include <mruby/class.h>
24#include <mruby/data.h>
25
26#include "mrb_ctx.h"
27#include "mrb_array.h"
28
29static struct mrb_data_type mrb_grn_array_type = {
30 "Groonga::Array",
31 NULL
32};
33
34static mrb_value
35mrb_grn_array_class_create(mrb_state *mrb, mrb_value klass)
36{
37 grn_ctx *ctx = (grn_ctx *)mrb->ud;
38 char *name;
39 mrb_int name_length;
40 const char *path = NULL;
41 mrb_value mrb_value_type;
42 grn_obj *value_type = NULL;
43 grn_obj *array;
44
45 mrb_get_args(mrb, "so", &name, &name_length, &mrb_value_type);
46 if (!mrb_nil_p(mrb_value_type)) {
47 value_type = DATA_PTR(mrb_value_type);
48 }
49
50 array = grn_table_create(ctx,
51 name, name_length,
52 path,
53 GRN_TABLE_NO_KEY,
54 NULL,
55 value_type);
56 grn_mrb_ctx_check(mrb);
57
58 return mrb_funcall(mrb, klass, "new", 1, mrb_cptr_value(mrb, array));
59}
60
61static mrb_value
62mrb_grn_array_initialize(mrb_state *mrb, mrb_value self)
63{
64 mrb_value mrb_array_ptr;
65
66 mrb_get_args(mrb, "o", &mrb_array_ptr);
67 DATA_TYPE(self) = &mrb_grn_array_type;
68 DATA_PTR(self) = mrb_cptr(mrb_array_ptr);
69 return self;
70}
71
72void
73grn_mrb_array_init(grn_ctx *ctx)
74{
75 grn_mrb_data *data = &(ctx->impl->mrb);
76 mrb_state *mrb = data->state;
77 struct RClass *module = data->module;
78 struct RClass *table_class;
79 struct RClass *klass;
80
81 table_class = mrb_class_get_under(mrb, module, "Table");
82 klass = mrb_define_class_under(mrb, module, "Array", table_class);
83 MRB_SET_INSTANCE_TT(klass, MRB_TT_DATA);
84
85 mrb_define_class_method(mrb, klass, "create",
86 mrb_grn_array_class_create,
87 MRB_ARGS_REQ(2));
88
89 mrb_define_method(mrb, klass, "initialize",
90 mrb_grn_array_initialize, MRB_ARGS_REQ(1));
91}
92#endif
93