1/* -*- c-basic-offset: 2 -*- */
2/*
3 Copyright(C) 2014-2016 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_procedure.h"
27
28#include "mrb_operator.h"
29
30static struct mrb_data_type mrb_grn_procedure_type = {
31 "Groonga::Procedure",
32 NULL
33};
34
35static mrb_value
36mrb_grn_procedure_initialize(mrb_state *mrb, mrb_value self)
37{
38 mrb_value mrb_procedure_ptr;
39
40 mrb_get_args(mrb, "o", &mrb_procedure_ptr);
41 DATA_TYPE(self) = &mrb_grn_procedure_type;
42 DATA_PTR(self) = mrb_cptr(mrb_procedure_ptr);
43 return self;
44}
45
46static mrb_value
47mrb_grn_procedure_selector_p(mrb_state *mrb, mrb_value self)
48{
49 grn_ctx *ctx = (grn_ctx *)mrb->ud;
50 grn_obj *proc = DATA_PTR(self);
51
52 return mrb_bool_value(grn_obj_is_selector_proc(ctx, proc));
53}
54
55static mrb_value
56mrb_grn_procedure_selector_only_p(mrb_state *mrb, mrb_value self)
57{
58 grn_ctx *ctx = (grn_ctx *)mrb->ud;
59 grn_obj *proc = DATA_PTR(self);
60
61 return mrb_bool_value(grn_obj_is_selector_only_proc(ctx, proc));
62}
63
64static mrb_value
65mrb_grn_procedure_scorer_p(mrb_state *mrb, mrb_value self)
66{
67 grn_ctx *ctx = (grn_ctx *)mrb->ud;
68 grn_obj *proc = DATA_PTR(self);
69
70 return mrb_bool_value(grn_obj_is_scorer_proc(ctx, proc));
71}
72
73static mrb_value
74mrb_grn_procedure_get_selector_operator(mrb_state *mrb, mrb_value self)
75{
76 grn_ctx *ctx = (grn_ctx *)mrb->ud;
77 grn_obj *proc = DATA_PTR(self);
78 grn_operator selector_op;
79
80 selector_op = grn_proc_get_selector_operator(ctx, proc);
81 return grn_mrb_value_from_operator(mrb, selector_op);
82}
83
84void
85grn_mrb_procedure_init(grn_ctx *ctx)
86{
87 grn_mrb_data *data = &(ctx->impl->mrb);
88 mrb_state *mrb = data->state;
89 struct RClass *module = data->module;
90 struct RClass *object_class = data->object_class;
91 struct RClass *klass;
92
93 klass = mrb_define_class_under(mrb, module, "Procedure", object_class);
94 MRB_SET_INSTANCE_TT(klass, MRB_TT_DATA);
95 mrb_define_method(mrb, klass, "initialize",
96 mrb_grn_procedure_initialize, MRB_ARGS_REQ(1));
97
98 mrb_define_method(mrb, klass, "selector?",
99 mrb_grn_procedure_selector_p, MRB_ARGS_NONE());
100 mrb_define_method(mrb, klass, "selector_only?",
101 mrb_grn_procedure_selector_only_p, MRB_ARGS_NONE());
102 mrb_define_method(mrb, klass, "scorer?",
103 mrb_grn_procedure_scorer_p, MRB_ARGS_NONE());
104
105 mrb_define_method(mrb, klass, "selector_operator",
106 mrb_grn_procedure_get_selector_operator, MRB_ARGS_NONE());
107}
108#endif
109