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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0f86d72174db71ca316daedc141b1f1fec4e054b5314e5851b5aee51f0f02d5e
4
- data.tar.gz: e25960ef3be771806d62ed9fbfa5a4891381914d214354d87476cb82a5a99cc3
3
+ metadata.gz: 3bb7df970ff6b8fc3a6f34f10d72fe39667ce8e24cab4d3861efa18e8148370c
4
+ data.tar.gz: 8a8f34ab4a057967869cd813506e06b8b9a67d792fa7b4f2f532288850d557c2
5
5
  SHA512:
6
- metadata.gz: 9e253d1cf691d416a70bc6d7359a13915693de31887b0f07a02fa2f94667afba49d452dc7ef09e476705616d418e830c232b5c1574d0c7802e876da4865f071d
7
- data.tar.gz: 9d263593d442a3613136719ffaf07d7c3047fa0624904b235d555b8709edbf45faae1de133f70b4ab48b28e524c8eef9f6d94977524a5a75eb8fb636eda7fcf6
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 = deep_merge_hashes(rb_obj_dup(current_val), other_val, ctx->block);
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 (!NIL_P(ctx->block)) {
20
- VALUE result = rb_funcall(ctx->block, rb_intern("call"), 3, key, current_val, other_val);
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
- deep_merge_context ctx = {self, block};
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, rb_intern("to_hash"), 0);
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
- return hash_deep_merge_bang(argc, argv, rb_obj_dup(self));
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.2
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: 2025-03-18 00:00:00.000000000 Z
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/v0.0.2
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/v0.0.2
27
- source_code_uri: https://github.com/cadenza-tech/sin_deep_merge/tree/v0.0.2
28
- changelog_uri: https://github.com/cadenza-tech/sin_deep_merge/blob/v0.0.2/CHANGELOG.md
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.2
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.2
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: []