sin_deep_merge 0.0.2 → 1.0.0
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/sin_deep_merge/sin_deep_merge.c +34 -9
- metadata +8 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3bb7df970ff6b8fc3a6f34f10d72fe39667ce8e24cab4d3861efa18e8148370c
|
4
|
+
data.tar.gz: 8a8f34ab4a057967869cd813506e06b8b9a67d792fa7b4f2f532288850d557c2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0164c6eb5e5cf99174c26d49ac44638367a0f4373804d6a5c7afc85b149ba51cb6abf32b30644f98c2b0e345a1264d4e40037ca2f84fcf06897c6c4bb0e06a9b
|
7
|
+
data.tar.gz: 1cda779bfbf3e02582d535f4a8b645178a148952a97c55896a82eeb0d9751e82d118dd9f1343e23c170953468ba21185b06090383d936e17aa870e3c343e6efe
|
@@ -1,11 +1,16 @@
|
|
1
1
|
#include <ruby.h>
|
2
2
|
|
3
|
+
static ID id_call;
|
4
|
+
static ID id_to_hash;
|
5
|
+
|
3
6
|
typedef struct {
|
4
7
|
VALUE hash;
|
5
8
|
VALUE block;
|
9
|
+
int destructive;
|
10
|
+
int block_given;
|
6
11
|
} deep_merge_context;
|
7
12
|
|
8
|
-
static VALUE deep_merge_hashes(VALUE self, VALUE other, VALUE block);
|
13
|
+
static VALUE deep_merge_hashes(VALUE self, VALUE other, VALUE block, int destructive);
|
9
14
|
|
10
15
|
static int deep_merge_iter(VALUE key, VALUE other_val, VALUE data) {
|
11
16
|
deep_merge_context *ctx = (deep_merge_context *)data;
|
@@ -14,10 +19,16 @@ static int deep_merge_iter(VALUE key, VALUE other_val, VALUE data) {
|
|
14
19
|
if (current_val == Qundef) {
|
15
20
|
rb_hash_aset(ctx->hash, key, other_val);
|
16
21
|
} else if (RB_TYPE_P(current_val, T_HASH) && RB_TYPE_P(other_val, T_HASH)) {
|
17
|
-
VALUE merged
|
22
|
+
VALUE merged;
|
23
|
+
if (ctx->destructive) {
|
24
|
+
merged = deep_merge_hashes(current_val, other_val, ctx->block, 1);
|
25
|
+
} else {
|
26
|
+
merged = deep_merge_hashes(rb_obj_dup(current_val), other_val, ctx->block, 0);
|
27
|
+
}
|
18
28
|
rb_hash_aset(ctx->hash, key, merged);
|
19
|
-
} else if (
|
20
|
-
VALUE
|
29
|
+
} else if (ctx->block_given) {
|
30
|
+
VALUE args[3] = {key, current_val, other_val};
|
31
|
+
VALUE result = rb_proc_call(ctx->block, rb_ary_new_from_values(3, args));
|
21
32
|
rb_hash_aset(ctx->hash, key, result);
|
22
33
|
} else {
|
23
34
|
rb_hash_aset(ctx->hash, key, other_val);
|
@@ -26,8 +37,9 @@ static int deep_merge_iter(VALUE key, VALUE other_val, VALUE data) {
|
|
26
37
|
return ST_CONTINUE;
|
27
38
|
}
|
28
39
|
|
29
|
-
static VALUE deep_merge_hashes(VALUE self, VALUE other, VALUE block) {
|
30
|
-
|
40
|
+
static VALUE deep_merge_hashes(VALUE self, VALUE other, VALUE block, int destructive) {
|
41
|
+
int block_given = !NIL_P(block);
|
42
|
+
deep_merge_context ctx = {self, block, destructive, block_given};
|
31
43
|
|
32
44
|
rb_hash_foreach(other, deep_merge_iter, (VALUE)&ctx);
|
33
45
|
|
@@ -37,22 +49,35 @@ static VALUE deep_merge_hashes(VALUE self, VALUE other, VALUE block) {
|
|
37
49
|
static VALUE hash_deep_merge_bang(int argc, VALUE *argv, VALUE self) {
|
38
50
|
VALUE other;
|
39
51
|
rb_scan_args(argc, argv, "1", &other);
|
40
|
-
other = rb_funcall(other,
|
52
|
+
other = rb_funcall(other, id_to_hash, 0);
|
41
53
|
VALUE block = Qnil;
|
42
54
|
if (rb_block_given_p()) {
|
43
55
|
block = rb_block_proc();
|
44
56
|
}
|
45
57
|
|
46
|
-
deep_merge_hashes(self, other, block);
|
58
|
+
deep_merge_hashes(self, other, block, 1);
|
47
59
|
|
48
60
|
return self;
|
49
61
|
}
|
50
62
|
|
51
63
|
static VALUE hash_deep_merge(int argc, VALUE *argv, VALUE self) {
|
52
|
-
|
64
|
+
VALUE other;
|
65
|
+
rb_scan_args(argc, argv, "1", &other);
|
66
|
+
other = rb_funcall(other, id_to_hash, 0);
|
67
|
+
VALUE block = Qnil;
|
68
|
+
if (rb_block_given_p()) {
|
69
|
+
block = rb_block_proc();
|
70
|
+
}
|
71
|
+
|
72
|
+
VALUE duplicated = rb_obj_dup(self);
|
73
|
+
deep_merge_hashes(duplicated, other, block, 0);
|
74
|
+
|
75
|
+
return duplicated;
|
53
76
|
}
|
54
77
|
|
55
78
|
void Init_sin_deep_merge(void) {
|
79
|
+
id_call = rb_intern("call");
|
80
|
+
id_to_hash = rb_intern("to_hash");
|
56
81
|
rb_define_method(rb_cHash, "deep_merge", RUBY_METHOD_FUNC(hash_deep_merge), -1);
|
57
82
|
rb_define_method(rb_cHash, "deep_merge!", RUBY_METHOD_FUNC(hash_deep_merge_bang), -1);
|
58
83
|
}
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sin_deep_merge
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Masahiro
|
8
8
|
bindir: exe
|
9
9
|
cert_chain: []
|
10
|
-
date:
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
11
11
|
dependencies: []
|
12
12
|
description: Merge deeply nested hashes faster than DeepMerge or ActiveSupport
|
13
13
|
email:
|
@@ -19,15 +19,15 @@ extra_rdoc_files: []
|
|
19
19
|
files:
|
20
20
|
- ext/sin_deep_merge/extconf.rb
|
21
21
|
- ext/sin_deep_merge/sin_deep_merge.c
|
22
|
-
homepage: https://github.com/cadenza-tech/sin_deep_merge/tree/
|
22
|
+
homepage: https://github.com/cadenza-tech/sin_deep_merge/tree/v1.0.0
|
23
23
|
licenses:
|
24
24
|
- MIT
|
25
25
|
metadata:
|
26
|
-
homepage_uri: https://github.com/cadenza-tech/sin_deep_merge/tree/
|
27
|
-
source_code_uri: https://github.com/cadenza-tech/sin_deep_merge/tree/
|
28
|
-
changelog_uri: https://github.com/cadenza-tech/sin_deep_merge/blob/
|
26
|
+
homepage_uri: https://github.com/cadenza-tech/sin_deep_merge/tree/v1.0.0
|
27
|
+
source_code_uri: https://github.com/cadenza-tech/sin_deep_merge/tree/v1.0.0
|
28
|
+
changelog_uri: https://github.com/cadenza-tech/sin_deep_merge/blob/v1.0.0/CHANGELOG.md
|
29
29
|
bug_tracker_uri: https://github.com/cadenza-tech/sin_deep_merge/issues
|
30
|
-
documentation_uri: https://rubydoc.info/gems/sin_deep_merge/0.0
|
30
|
+
documentation_uri: https://rubydoc.info/gems/sin_deep_merge/1.0.0
|
31
31
|
funding_uri: https://patreon.com/CadenzaTech
|
32
32
|
rubygems_mfa_required: 'true'
|
33
33
|
rdoc_options: []
|
@@ -44,7 +44,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
44
44
|
- !ruby/object:Gem::Version
|
45
45
|
version: '0'
|
46
46
|
requirements: []
|
47
|
-
rubygems_version: 3.6.
|
47
|
+
rubygems_version: 3.6.9
|
48
48
|
specification_version: 4
|
49
49
|
summary: Merge deeply nested hashes faster than DeepMerge or ActiveSupport
|
50
50
|
test_files: []
|