Class: Rugged::Config

Inherits:
Object
  • Object
show all
Defined in:
ext/rugged/rugged_config.c

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.new(rb_path) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'ext/rugged/rugged_config.c', line 42

static VALUE rb_git_config_new(VALUE klass, VALUE rb_path)
{
	git_config *config = NULL;
	int error, i;

	if (TYPE(rb_path) == T_ARRAY) {
		error = git_config_new(&config);
		rugged_exception_check(error);

		for (i = 0; i < RARRAY_LEN(rb_path); ++i) {
			VALUE f = rb_ary_entry(rb_path, i);
			Check_Type(f, T_STRING);
			error = git_config_add_file_ondisk(config, StringValueCStr(f), i + 1);
			rugged_exception_check(error);
		}
	} else if (TYPE(rb_path) == T_STRING) {
		error = git_config_open_ondisk(&config, StringValueCStr(rb_path));
		rugged_exception_check(error);
	} else {
		rb_raise(rb_eTypeError, "Expecting a filename or an array of filenames");
	}

	return rugged_config_new(klass, Qnil, config);
}

.open_globalObject



205
206
207
208
209
210
211
212
213
214
# File 'ext/rugged/rugged_config.c', line 205

static VALUE rb_git_config_open_global(VALUE klass)
{
	git_config *cfg;
	int error;

	error = git_config_open_global(&cfg);
	rugged_exception_check(error);

	return rugged_config_new(klass, Qnil, cfg);
}

Instance Method Details

#[](rb_key) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'ext/rugged/rugged_config.c', line 67

static VALUE rb_git_config_get(VALUE self, VALUE rb_key)
{
	git_config *config;
	const char *value;
	int error;

	Data_Get_Struct(self, git_config, config);
	Check_Type(rb_key, T_STRING);

	error = git_config_get_string(config, StringValueCStr(rb_key), &value);
	if (error == GIT_ENOTFOUND)
		return Qnil;

	rugged_exception_check(error);

	/* Return all config values as ASCII strings */
	return rugged_str_new2(value, NULL);
}

#[]=(rb_key, rb_val) ⇒ Object



86
87
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
115
116
117
118
# File 'ext/rugged/rugged_config.c', line 86

static VALUE rb_git_config_store(VALUE self, VALUE rb_key, VALUE rb_val)
{
	git_config *config;
	const char *key;
	int error;

	Data_Get_Struct(self, git_config, config);
	Check_Type(rb_key, T_STRING);

	key = StringValueCStr(rb_key);

	switch (TYPE(rb_val)) {
	case T_STRING:
		error = git_config_set_string(config, key, StringValueCStr(rb_val));
		break;

	case T_TRUE:
	case T_FALSE:
		error = git_config_set_bool(config, key, (rb_val == Qtrue));
		break;

	case T_FIXNUM:
		error = git_config_set_int32(config, key, FIX2INT(rb_val));
		break;

	default:
		rb_raise(rb_eTypeError,
			"Invalid value; config files can only store string, bool or int keys");
	}

	rugged_exception_check(error);
	return Qnil;
}

#delete(rb_key) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'ext/rugged/rugged_config.c', line 120

static VALUE rb_git_config_delete(VALUE self, VALUE rb_key)
{
	git_config *config;
	int error;
	VALUE result;

	Data_Get_Struct(self, git_config, config);
	Check_Type(rb_key, T_STRING);

	error = git_config_delete(config, StringValueCStr(rb_key));
	if (error == GIT_ENOTFOUND)
		return Qfalse;

	rugged_exception_check(error);
	return Qtrue;
}

#eachObject



176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'ext/rugged/rugged_config.c', line 176

static VALUE rb_git_config_each_pair(VALUE self)
{
	git_config *config;
	int error;

	Data_Get_Struct(self, git_config, config);

	if (!rb_block_given_p())
		return rb_funcall(self, rb_intern("to_enum"), 1, CSTR2SYM("each_pair"));

	error = git_config_foreach(config, &cb_config__each_pair, (void *)rb_block_proc());
	rugged_exception_check(error);
	return Qnil;
}

#each_keyObject



161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'ext/rugged/rugged_config.c', line 161

static VALUE rb_git_config_each_key(VALUE self)
{
	git_config *config;
	int error;

	Data_Get_Struct(self, git_config, config);

	if (!rb_block_given_p())
		return rb_funcall(self, rb_intern("to_enum"), 1, CSTR2SYM("each_key"));

	error = git_config_foreach(config, &cb_config__each_key, (void *)rb_block_proc());
	rugged_exception_check(error);
	return Qnil;
}

#each_pairObject



176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'ext/rugged/rugged_config.c', line 176

static VALUE rb_git_config_each_pair(VALUE self)
{
	git_config *config;
	int error;

	Data_Get_Struct(self, git_config, config);

	if (!rb_block_given_p())
		return rb_funcall(self, rb_intern("to_enum"), 1, CSTR2SYM("each_pair"));

	error = git_config_foreach(config, &cb_config__each_pair, (void *)rb_block_proc());
	rugged_exception_check(error);
	return Qnil;
}

#get(rb_key) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'ext/rugged/rugged_config.c', line 67

static VALUE rb_git_config_get(VALUE self, VALUE rb_key)
{
	git_config *config;
	const char *value;
	int error;

	Data_Get_Struct(self, git_config, config);
	Check_Type(rb_key, T_STRING);

	error = git_config_get_string(config, StringValueCStr(rb_key), &value);
	if (error == GIT_ENOTFOUND)
		return Qnil;

	rugged_exception_check(error);

	/* Return all config values as ASCII strings */
	return rugged_str_new2(value, NULL);
}

#store(rb_key, rb_val) ⇒ Object



86
87
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
115
116
117
118
# File 'ext/rugged/rugged_config.c', line 86

static VALUE rb_git_config_store(VALUE self, VALUE rb_key, VALUE rb_val)
{
	git_config *config;
	const char *key;
	int error;

	Data_Get_Struct(self, git_config, config);
	Check_Type(rb_key, T_STRING);

	key = StringValueCStr(rb_key);

	switch (TYPE(rb_val)) {
	case T_STRING:
		error = git_config_set_string(config, key, StringValueCStr(rb_val));
		break;

	case T_TRUE:
	case T_FALSE:
		error = git_config_set_bool(config, key, (rb_val == Qtrue));
		break;

	case T_FIXNUM:
		error = git_config_set_int32(config, key, FIX2INT(rb_val));
		break;

	default:
		rb_raise(rb_eTypeError,
			"Invalid value; config files can only store string, bool or int keys");
	}

	rugged_exception_check(error);
	return Qnil;
}

#to_hashObject



191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'ext/rugged/rugged_config.c', line 191

static VALUE rb_git_config_to_hash(VALUE self)
{
	git_config *config;
	int error;
	VALUE hash;

	Data_Get_Struct(self, git_config, config);
	hash = rb_hash_new();

	error = git_config_foreach(config, &cb_config__to_hash, (void *)hash);
	rugged_exception_check(error);
	return hash;
}