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.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/ext/fat/fat.c +6 -16
  3. data/fat.gemspec +1 -1
  4. data/test/fat_test.rb +6 -1
  5. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 64e614f67b483c476550fd7b144dbb8e03ae010c
4
- data.tar.gz: 0f4ace7f1c241251e27eaa37eccba1d9e29cdef6
3
+ metadata.gz: e703ba437bab9b2607d4aabe4ad6b95f195910b5
4
+ data.tar.gz: e8b2024b26dd761b98ad7fdc168f95030a0bb54e
5
5
  SHA512:
6
- metadata.gz: 3e5bee3db122f8470ec1403273f72274192affdb2479d129798447d4535420f555f49e7fe6997830e59828686f5f4922913e83803fef0e89aeb680cbfdf55156
7
- data.tar.gz: 8e0241de729d4daa1f03711c519b73929dfdbcf14dafef0d282a10b2ead4181509081dd34de380e9a4eb8db6f589fef18fbe0fd4974b4f9a9d552d532a59f936
6
+ metadata.gz: 97db99398e768a5203d462de5ec9d184b07091e63a9aa9f8d1190a114029bd90367c2c912e55e63524c141fa9b46a7086b0010c8f288b49c4833bf7e856f7c53
7
+ data.tar.gz: 3b91dced2d19e27d3af4508557d51853216bf98dd1e6f36808611898fb99d0fa4e643508a24e4936e0c02d11d158c2c40056a90099f3a9e12d8df61257c78ecc
@@ -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 default_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, extract_default(keywords));
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, extract_default(keywords));
44
+ return fat(hash, fields, keywords);
46
45
  }
47
46
 
48
- static VALUE fat(VALUE hash, VALUE fields, VALUE default_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(default_value)) {
56
- return default_value;
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
- }
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "fat"
3
- s.version = "2.0.0"
3
+ s.version = "2.0.1"
4
4
  s.summary = "C extension to find values in nested hashes without pain"
5
5
  s.description = s.summary
6
6
  s.authors = ["Lucas Tolchinsky"]
@@ -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 "default", Fat.at(hash, "foo", "bar", "wat", default: "default")
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
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fat
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lucas Tolchinsky