1/* -*- c-basic-offset: 2 -*- */
2/*
3 Copyright(C) 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#include <groonga/command.h>
21
22#ifdef GRN_WITH_MRUBY
23#include <mruby.h>
24#include <mruby/class.h>
25#include <mruby/data.h>
26#include <mruby/value.h>
27#include <mruby/string.h>
28
29#include "mrb_ctx.h"
30#include "mrb_converter.h"
31#include "mrb_command_input.h"
32
33static struct mrb_data_type mrb_grn_command_input_type = {
34 "Groonga::CommandInput",
35 NULL
36};
37
38static mrb_value
39mrb_grn_command_input_initialize(mrb_state *mrb, mrb_value self)
40{
41 mrb_value mrb_command_input_ptr;
42
43 mrb_get_args(mrb, "o", &mrb_command_input_ptr);
44 DATA_TYPE(self) = &mrb_grn_command_input_type;
45 DATA_PTR(self) = mrb_cptr(mrb_command_input_ptr);
46 return self;
47}
48
49static mrb_value
50mrb_grn_command_input_array_reference(mrb_state *mrb, mrb_value self)
51{
52 grn_ctx *ctx = (grn_ctx *)mrb->ud;
53 grn_command_input *input;
54 mrb_value mrb_key_or_offset;
55 grn_obj *argument;
56
57 input = DATA_PTR(self);
58 mrb_get_args(mrb, "o", &mrb_key_or_offset);
59
60 switch (mrb_type(mrb_key_or_offset)) {
61 case MRB_TT_FIXNUM :
62 {
63 mrb_int offset = mrb_fixnum(mrb_key_or_offset);
64 argument = grn_command_input_at(ctx, input, offset);
65 }
66 break;
67 case MRB_TT_SYMBOL :
68 {
69 mrb_sym mrb_key_symbol;
70 const char *key;
71 mrb_int key_length;
72
73 mrb_key_symbol = mrb_symbol(mrb_key_or_offset);
74 key = mrb_sym2name_len(mrb, mrb_key_symbol, &key_length);
75 argument = grn_command_input_get(ctx, input, key, key_length);
76 }
77 break;
78 case MRB_TT_STRING :
79 {
80 mrb_value mrb_key = mrb_key_or_offset;
81 argument = grn_command_input_get(ctx, input,
82 RSTRING_PTR(mrb_key),
83 RSTRING_LEN(mrb_key));
84 }
85 break;
86 default :
87 mrb_raisef(mrb, E_ARGUMENT_ERROR,
88 "must be offset (as integer) or key (as symbol or string): %S",
89 mrb_key_or_offset);
90 break;
91 }
92
93 if (!argument) {
94 return mrb_nil_value();
95 }
96
97 if (GRN_TEXT_LEN(argument) == 0) {
98 return mrb_nil_value();
99 }
100
101 return mrb_str_new_static(mrb,
102 GRN_TEXT_VALUE(argument),
103 GRN_TEXT_LEN(argument));
104}
105
106static mrb_value
107mrb_grn_command_input_get_arguments(mrb_state *mrb, mrb_value self)
108{
109 grn_ctx *ctx = (grn_ctx *)mrb->ud;
110 grn_command_input *input;
111 grn_obj *arguments;
112
113 input = DATA_PTR(self);
114 arguments = grn_command_input_get_arguments(ctx, input);
115
116 return grn_mrb_value_from_grn_obj(mrb, arguments);
117}
118
119void
120grn_mrb_command_input_init(grn_ctx *ctx)
121{
122 grn_mrb_data *data = &(ctx->impl->mrb);
123 mrb_state *mrb = data->state;
124 struct RClass *module = data->module;
125 struct RClass *klass;
126
127 klass = mrb_define_class_under(mrb, module, "CommandInput", mrb->object_class);
128 MRB_SET_INSTANCE_TT(klass, MRB_TT_DATA);
129
130 mrb_define_method(mrb, klass, "initialize",
131 mrb_grn_command_input_initialize, MRB_ARGS_REQ(1));
132
133 mrb_define_method(mrb, klass, "[]",
134 mrb_grn_command_input_array_reference, MRB_ARGS_REQ(1));
135
136 mrb_define_method(mrb, klass, "arguments",
137 mrb_grn_command_input_get_arguments, MRB_ARGS_NONE());
138}
139#endif
140