simple-immutable 1.0.5 → 1.0.6
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/.rubocop.yml +3 -0
- data/Makefile +1 -0
- data/VERSION +1 -1
- data/lib/simple/immutable.rb +34 -11
- data/test/immutable_test.rb +0 -1
- data/test/immutable_w_null_record_test.rb +63 -0
- metadata +8 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8b64e7bb0ef88ead17af2b5d1bca5ea4dc3a19c566a441ee8fe519afe7ac47c3
|
4
|
+
data.tar.gz: 4dedaf9317e816780dc86427b99a2b070a1ec46da22ca56446e8ceb120ba615a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz: '
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: '0029d88bfa9788f39948eacc0707af394d2702b31800ec86ef83e76f9e1b4dbeb672ce619877a5f0e9f09da93b9b1edcdf3a0dc471c84b73fd1fd8415f168a6e'
|
7
|
+
data.tar.gz: abd71b228155f72f7953868cd445722c013b3ce9067d9ecc2075cd5c518a12905a0cd3ed768d7adec505291b53f6f383ce0fc6b63e2b1181ff0887f2fdf7c09c
|
data/.rubocop.yml
CHANGED
data/Makefile
CHANGED
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.0.
|
1
|
+
1.0.6
|
data/lib/simple/immutable.rb
CHANGED
@@ -6,14 +6,14 @@ class Simple::Immutable
|
|
6
6
|
|
7
7
|
# turns an object, which can be a hash or array of hashes, arrays, and scalars
|
8
8
|
# into an object which you can use to access with dot methods.
|
9
|
-
def self.create(object, max_depth
|
9
|
+
def self.create(object, max_depth: 8, null_record: nil)
|
10
10
|
case object
|
11
11
|
when Array
|
12
12
|
raise ArgumentError, "Object nested too deep (or inner loop?)" if max_depth < 0
|
13
13
|
|
14
|
-
object.map { |obj| create obj, max_depth - 1 }
|
14
|
+
object.map { |obj| create obj, null_record: null_record, max_depth: max_depth - 1 }
|
15
15
|
when Hash
|
16
|
-
new(object)
|
16
|
+
new(object, null_record)
|
17
17
|
else
|
18
18
|
object
|
19
19
|
end
|
@@ -22,7 +22,14 @@ class Simple::Immutable
|
|
22
22
|
def self.raw_data(immutable)
|
23
23
|
case immutable
|
24
24
|
when SELF
|
25
|
-
immutable.instance_variable_get :@hsh
|
25
|
+
hsh = immutable.instance_variable_get :@hsh
|
26
|
+
null_record = immutable.instance_variable_get :@null_record
|
27
|
+
|
28
|
+
if null_record
|
29
|
+
null_record.merge(hsh)
|
30
|
+
else
|
31
|
+
hsh
|
32
|
+
end
|
26
33
|
when Array
|
27
34
|
immutable.map { |e| raw_data(e) }
|
28
35
|
else
|
@@ -37,19 +44,35 @@ class Simple::Immutable
|
|
37
44
|
|
38
45
|
private
|
39
46
|
|
40
|
-
def initialize(hsh)
|
47
|
+
def initialize(hsh, null_record = nil)
|
41
48
|
@hsh = hsh
|
49
|
+
@null_record = null_record
|
50
|
+
end
|
51
|
+
|
52
|
+
# rubycop:disable Lint/IneffectiveAccessModifier
|
53
|
+
|
54
|
+
# fetches key from a hsh, regardless of it being a Symbol or a String.
|
55
|
+
# Yields the block if the key cannot be found.
|
56
|
+
def self.fetch_symbol_or_string_from_hash(hsh, key, &block)
|
57
|
+
if hsh
|
58
|
+
hsh.fetch(key.to_sym) do
|
59
|
+
hsh.fetch(key.to_s, &block)
|
60
|
+
end
|
61
|
+
else
|
62
|
+
yield
|
63
|
+
end
|
42
64
|
end
|
43
65
|
|
44
66
|
def method_missing(sym, *args, &block)
|
45
67
|
if args.empty? && !block
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
nil
|
68
|
+
value = SELF.fetch_symbol_or_string_from_hash(@hsh, sym) do
|
69
|
+
SELF.fetch_symbol_or_string_from_hash(@null_record, sym) do
|
70
|
+
# STDERR.puts "Missing attribute #{sym} for Immutable w/#{@hsh.inspect}"
|
71
|
+
super
|
72
|
+
end
|
52
73
|
end
|
74
|
+
|
75
|
+
return SELF.create(value)
|
53
76
|
end
|
54
77
|
|
55
78
|
super
|
data/test/immutable_test.rb
CHANGED
@@ -0,0 +1,63 @@
|
|
1
|
+
# rubocop:disable Metrics/AbcSize
|
2
|
+
|
3
|
+
require "test-unit"
|
4
|
+
require_relative "../lib/simple-immutable"
|
5
|
+
|
6
|
+
class Simple::Immutable::WithNullRecordTestCase < Test::Unit::TestCase
|
7
|
+
Immutable = ::Simple::Immutable
|
8
|
+
|
9
|
+
def hsh
|
10
|
+
{
|
11
|
+
a: "a-value",
|
12
|
+
"b": "b-value",
|
13
|
+
"child": {
|
14
|
+
name: "childname",
|
15
|
+
grandchild: {
|
16
|
+
name: "grandchildname"
|
17
|
+
}
|
18
|
+
},
|
19
|
+
"children": [
|
20
|
+
"anna",
|
21
|
+
"arthur",
|
22
|
+
{
|
23
|
+
action: {
|
24
|
+
keep_your_mouth_shut: true
|
25
|
+
}
|
26
|
+
}
|
27
|
+
]
|
28
|
+
}
|
29
|
+
end
|
30
|
+
|
31
|
+
def null_record
|
32
|
+
{
|
33
|
+
foo: "foo-value",
|
34
|
+
"bar" => "bar-value"
|
35
|
+
}
|
36
|
+
end
|
37
|
+
|
38
|
+
def immutable
|
39
|
+
Immutable.create hsh, null_record: null_record
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_hash_access
|
43
|
+
assert_equal "a-value", immutable.a
|
44
|
+
assert_equal "b-value", immutable.b
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_null_record_access
|
48
|
+
assert_equal "foo-value", immutable.foo
|
49
|
+
assert_equal "bar-value", immutable.bar
|
50
|
+
end
|
51
|
+
|
52
|
+
def test_missing_keys
|
53
|
+
assert_raise(NoMethodError) do
|
54
|
+
immutable.unknown
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def test_raw_data
|
59
|
+
# raw data does contain null_record
|
60
|
+
expected = hsh.merge(null_record)
|
61
|
+
assert_equal(expected, Immutable.raw_data(immutable))
|
62
|
+
end
|
63
|
+
end
|
metadata
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simple-immutable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- radiospiel
|
8
8
|
- mediapeers GmbH
|
9
|
-
autorequire:
|
9
|
+
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2021-07-13 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: Immutable ruby objects implementing dot and [] accessors.
|
15
15
|
email: eno@radiospiel.org
|
@@ -33,10 +33,11 @@ files:
|
|
33
33
|
- scripts/release.rb
|
34
34
|
- simple-immutable.gemspec
|
35
35
|
- test/immutable_test.rb
|
36
|
+
- test/immutable_w_null_record_test.rb
|
36
37
|
homepage: http://github.com/radiospiel/simple-immutable
|
37
38
|
licenses: []
|
38
39
|
metadata: {}
|
39
|
-
post_install_message:
|
40
|
+
post_install_message:
|
40
41
|
rdoc_options: []
|
41
42
|
require_paths:
|
42
43
|
- lib
|
@@ -51,9 +52,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
51
52
|
- !ruby/object:Gem::Version
|
52
53
|
version: '0'
|
53
54
|
requirements: []
|
54
|
-
rubygems_version: 3.
|
55
|
-
signing_key:
|
55
|
+
rubygems_version: 3.1.4
|
56
|
+
signing_key:
|
56
57
|
specification_version: 4
|
57
58
|
summary: Immutable ruby objects
|
58
59
|
test_files:
|
59
60
|
- test/immutable_test.rb
|
61
|
+
- test/immutable_w_null_record_test.rb
|