fat 2.0.0 → 2.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/ext/fat/fat.c +6 -16
- data/fat.gemspec +1 -1
- data/test/fat_test.rb +6 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e703ba437bab9b2607d4aabe4ad6b95f195910b5
|
4
|
+
data.tar.gz: e8b2024b26dd761b98ad7fdc168f95030a0bb54e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 97db99398e768a5203d462de5ec9d184b07091e63a9aa9f8d1190a114029bd90367c2c912e55e63524c141fa9b46a7086b0010c8f288b49c4833bf7e856f7c53
|
7
|
+
data.tar.gz: 3b91dced2d19e27d3af4508557d51853216bf98dd1e6f36808611898fb99d0fa4e643508a24e4936e0c02d11d158c2c40056a90099f3a9e12d8df61257c78ecc
|
data/ext/fat/fat.c
CHANGED
@@ -9,14 +9,13 @@ void Init_fat();
|
|
9
9
|
static VALUE singleton_method_at(int argc, VALUE *argv, VALUE self);
|
10
10
|
static VALUE method_at(int argc, VALUE *argv, VALUE hash);
|
11
11
|
|
12
|
-
static VALUE fat(VALUE hash, VALUE fields, VALUE
|
12
|
+
static VALUE fat(VALUE hash, VALUE fields, VALUE keywords);
|
13
13
|
|
14
14
|
// Helpers
|
15
15
|
static inline VALUE fields_upto_index(VALUE fields, long index);
|
16
16
|
static inline long compute_error_message_length(VALUE fields, long index);
|
17
17
|
static inline void copy_error_message(VALUE fields, long index, char* error_message_pointer);
|
18
18
|
static inline VALUE sym_to_str(VALUE sym);
|
19
|
-
static inline VALUE extract_default(VALUE keywords);
|
20
19
|
|
21
20
|
void Init_fat(void) {
|
22
21
|
Fat = rb_define_module("Fat");
|
@@ -33,7 +32,7 @@ static VALUE singleton_method_at(int argc, VALUE *argv, VALUE self) {
|
|
33
32
|
|
34
33
|
rb_scan_args(argc, argv, "1*:", &hash, &fields, &keywords);
|
35
34
|
|
36
|
-
return fat(hash, fields,
|
35
|
+
return fat(hash, fields, keywords);
|
37
36
|
}
|
38
37
|
|
39
38
|
static VALUE method_at(int argc, VALUE *argv, VALUE hash) {
|
@@ -42,18 +41,18 @@ static VALUE method_at(int argc, VALUE *argv, VALUE hash) {
|
|
42
41
|
|
43
42
|
rb_scan_args(argc, argv, "*:", &fields, &keywords);
|
44
43
|
|
45
|
-
return fat(hash, fields,
|
44
|
+
return fat(hash, fields, keywords);
|
46
45
|
}
|
47
46
|
|
48
|
-
static VALUE fat(VALUE hash, VALUE fields, VALUE
|
47
|
+
static VALUE fat(VALUE hash, VALUE fields, VALUE keywords) {
|
49
48
|
VALUE value = hash;
|
50
49
|
|
51
50
|
for (long i = 0; i < RARRAY_LEN(fields); i++) {
|
52
51
|
value = rb_hash_aref(value, RARRAY_AREF(fields, i));
|
53
52
|
|
54
53
|
if (NIL_P(value)) {
|
55
|
-
if (!NIL_P(
|
56
|
-
return
|
54
|
+
if (!NIL_P(keywords)) {
|
55
|
+
return rb_hash_aref(keywords, ID2SYM(rb_intern("default")));
|
57
56
|
} else {
|
58
57
|
rb_raise(rb_eFatError, "%s is nil", RSTRING_PTR(fields_upto_index(fields, i)));
|
59
58
|
}
|
@@ -125,12 +124,3 @@ static inline VALUE sym_to_str(VALUE sym) {
|
|
125
124
|
return rb_id2str(SYM2ID(sym));
|
126
125
|
}
|
127
126
|
|
128
|
-
static inline VALUE extract_default(VALUE keywords) {
|
129
|
-
VALUE result = Qnil;
|
130
|
-
|
131
|
-
if (!NIL_P(keywords)) {
|
132
|
-
result = rb_hash_aref(keywords, ID2SYM(rb_intern("default")));
|
133
|
-
}
|
134
|
-
|
135
|
-
return result;
|
136
|
-
}
|
data/fat.gemspec
CHANGED
data/test/fat_test.rb
CHANGED
@@ -26,7 +26,7 @@ scope do
|
|
26
26
|
|
27
27
|
test "return default value" do |hash|
|
28
28
|
assert_equal "default", Fat.at(hash, "foo", "wat", "baz", default: "default")
|
29
|
-
assert_equal
|
29
|
+
assert_equal nil, Fat.at(hash, "foo", "bar", "wat", default: nil)
|
30
30
|
end
|
31
31
|
|
32
32
|
test "include the module" do |hash|
|
@@ -34,6 +34,11 @@ scope do
|
|
34
34
|
|
35
35
|
assert hash.respond_to?(:at)
|
36
36
|
assert_equal :found, hash.at("foo", "bar", "baz")
|
37
|
+
|
38
|
+
exception = assert_raise(Fat::FatError) { hash.at("foo", "wat", "baz") }
|
39
|
+
assert_equal "foo.wat is nil", exception.message
|
40
|
+
|
41
|
+
assert_equal nil, hash.at("foo", "bar", "wat", default: nil)
|
37
42
|
end
|
38
43
|
end
|
39
44
|
|