1/* -*- c-basic-offset: 2 -*- */
2/*
3 Copyright(C) 2013-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/variable.h>
25#include <mruby/data.h>
26
27#include "../grn_db.h"
28#include "mrb_ctx.h"
29#include "mrb_accessor.h"
30#include "mrb_converter.h"
31
32static struct mrb_data_type mrb_grn_accessor_type = {
33 "Groonga::Accessor",
34 NULL
35};
36
37static mrb_value
38mrb_grn_accessor_initialize(mrb_state *mrb, mrb_value self)
39{
40 mrb_value mrb_accessor_ptr;
41
42 mrb_get_args(mrb, "o", &mrb_accessor_ptr);
43 DATA_TYPE(self) = &mrb_grn_accessor_type;
44 DATA_PTR(self) = mrb_cptr(mrb_accessor_ptr);
45 return self;
46}
47
48static mrb_value
49mrb_grn_accessor_next(mrb_state *mrb, mrb_value self)
50{
51 grn_accessor *accessor;
52
53 accessor = DATA_PTR(self);
54 return grn_mrb_value_from_grn_obj(mrb, (grn_obj *)(accessor->next));
55}
56
57static mrb_value
58mrb_grn_accessor_have_next_p(mrb_state *mrb, mrb_value self)
59{
60 grn_accessor *accessor;
61
62 accessor = DATA_PTR(self);
63 return mrb_bool_value(accessor->next != NULL);
64}
65
66static mrb_value
67mrb_grn_accessor_object(mrb_state *mrb, mrb_value self)
68{
69 grn_accessor *accessor;
70
71 accessor = DATA_PTR(self);
72 return grn_mrb_value_from_grn_obj(mrb, accessor->obj);
73}
74
75static mrb_value
76mrb_grn_accessor_name(mrb_state *mrb, mrb_value self)
77{
78 grn_ctx *ctx = (grn_ctx *)mrb->ud;
79 grn_rc rc;
80 grn_obj *accessor;
81 grn_obj name;
82 mrb_value mrb_name;
83
84 accessor = DATA_PTR(self);
85 GRN_TEXT_INIT(&name, 0);
86 rc = grn_column_name_(ctx, accessor, &name);
87 if (rc == GRN_SUCCESS) {
88 mrb_name = mrb_str_new(mrb, GRN_TEXT_VALUE(&name), GRN_TEXT_LEN(&name));
89 GRN_OBJ_FIN(ctx, &name);
90 } else {
91 mrb_name = mrb_nil_value();
92 GRN_OBJ_FIN(ctx, &name);
93 grn_mrb_ctx_check(mrb);
94 }
95
96 return mrb_name;
97}
98
99void
100grn_mrb_accessor_init(grn_ctx *ctx)
101{
102 grn_mrb_data *data = &(ctx->impl->mrb);
103 mrb_state *mrb = data->state;
104 struct RClass *module = data->module;
105 struct RClass *klass;
106
107 klass = mrb_define_class_under(mrb, module, "Accessor", data->object_class);
108 MRB_SET_INSTANCE_TT(klass, MRB_TT_DATA);
109 mrb_define_method(mrb, klass, "initialize",
110 mrb_grn_accessor_initialize, MRB_ARGS_REQ(1));
111 mrb_define_method(mrb, klass, "next",
112 mrb_grn_accessor_next, MRB_ARGS_NONE());
113 mrb_define_method(mrb, klass, "have_next?",
114 mrb_grn_accessor_have_next_p, MRB_ARGS_NONE());
115 mrb_define_method(mrb, klass, "object",
116 mrb_grn_accessor_object, MRB_ARGS_NONE());
117
118 mrb_define_method(mrb, klass, "name",
119 mrb_grn_accessor_name, MRB_ARGS_NONE());
120}
121#endif
122