ree_lib 1.0.33 → 1.0.34
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -3
- data/lib/ree_lib/packages/ree_logger/package/ree_logger/multi_logger.rb +2 -2
- data/lib/ree_lib/packages/ree_object/Package.schema.json +7 -7
- data/lib/ree_lib/packages/ree_object/package/ree_object/functions/{as_json.rb → to_hash.rb} +73 -73
- data/lib/ree_lib/packages/ree_object/package/ree_object/functions/to_obj.rb +94 -94
- data/lib/ree_lib/packages/ree_object/schemas/ree_object/functions/dump_as_json.schema.json +1 -1
- data/lib/ree_lib/packages/ree_object/schemas/ree_object/functions/{as_json.schema.json → to_hash.schema.json} +4 -4
- data/lib/ree_lib/packages/ree_object/schemas/ree_object/functions/to_obj.schema.json +8 -8
- data/lib/ree_lib/packages/ree_object/spec/ree_object/functions/{as_json_spec.rb → to_hash_spec.rb} +171 -171
- data/lib/ree_lib/version.rb +1 -1
- metadata +5 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4791b1d4665fb6529a0eac40ce9cec637a08bc95896466d97bd2b23dece953ff
|
4
|
+
data.tar.gz: d698530862a8ae9d812efe51592eb0f06ab050547be07a23e600411d9398564f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ca10f0b35282bc9113376f0f18cdc7e06b5d9da80b4a0bba45e53f0f64bb17cd0d8f6a1f108a79a13067c7642d4ff97d69b86a3bf687d453d9ff9642a4331941
|
7
|
+
data.tar.gz: 79d127c9a1104ff11b21a6ddacdfcbfe126f467fb6f1b508dfd1910ab230d7d571d4ac92d33b44eb919b24b524b86088c9b0bc866818ba21bf41880ea4cdc331
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
ree_lib (1.0.
|
4
|
+
ree_lib (1.0.34)
|
5
5
|
binding_of_caller (~> 1.0.0)
|
6
6
|
i18n (~> 1.12.0)
|
7
7
|
loofah (~> 2.18.0)
|
@@ -36,8 +36,6 @@ GEM
|
|
36
36
|
crass (~> 1.0.2)
|
37
37
|
nokogiri (>= 1.5.9)
|
38
38
|
msgpack (1.6.0)
|
39
|
-
nokogiri (1.14.2-x86_64-darwin)
|
40
|
-
racc (~> 1.4)
|
41
39
|
nokogiri (1.14.2-x86_64-linux)
|
42
40
|
racc (~> 1.4)
|
43
41
|
oj (3.13.23)
|
@@ -1,7 +1,7 @@
|
|
1
1
|
class ReeLogger::MultiLogger < Logger
|
2
2
|
include Ree::LinkDSL
|
3
3
|
|
4
|
-
link :
|
4
|
+
link :to_hash, from: :ree_object
|
5
5
|
link :transform_values, from: :ree_hash
|
6
6
|
link 'ree_logger/log_event', -> { LogEvent }
|
7
7
|
link 'ree_logger/rate_limiter', -> { RateLimiter }
|
@@ -149,7 +149,7 @@ class ReeLogger::MultiLogger < Logger
|
|
149
149
|
args[name] = method_binding.local_variable_get(name)
|
150
150
|
end
|
151
151
|
|
152
|
-
args = transform_values(
|
152
|
+
args = transform_values(to_hash(args)) do |k, v|
|
153
153
|
if @filter_words.any? { k.to_s.include?(_1) }
|
154
154
|
'FILTERED'
|
155
155
|
else
|
@@ -15,13 +15,6 @@
|
|
15
15
|
|
16
16
|
],
|
17
17
|
"objects": [
|
18
|
-
{
|
19
|
-
"name": "as_json",
|
20
|
-
"schema": "packages/ree_object/schemas/ree_object/functions/as_json.schema.json",
|
21
|
-
"tags": [
|
22
|
-
"fn"
|
23
|
-
]
|
24
|
-
},
|
25
18
|
{
|
26
19
|
"name": "deep_dup",
|
27
20
|
"schema": "packages/ree_object/schemas/ree_object/functions/deep_dup.schema.json",
|
@@ -64,6 +57,13 @@
|
|
64
57
|
"fn"
|
65
58
|
]
|
66
59
|
},
|
60
|
+
{
|
61
|
+
"name": "to_hash",
|
62
|
+
"schema": "packages/ree_object/schemas/ree_object/functions/to_hash.schema.json",
|
63
|
+
"tags": [
|
64
|
+
"fn"
|
65
|
+
]
|
66
|
+
},
|
67
67
|
{
|
68
68
|
"name": "to_obj",
|
69
69
|
"schema": "packages/ree_object/schemas/ree_object/functions/to_obj.schema.json",
|
@@ -1,74 +1,74 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
class ReeObject::
|
4
|
-
include Ree::FnDSL
|
5
|
-
|
6
|
-
fn :
|
7
|
-
def_error { RecursiveObjectErr }
|
8
|
-
end
|
9
|
-
|
10
|
-
BASIC_TYPES = [
|
11
|
-
Date, Time, Numeric, String, FalseClass, TrueClass, NilClass, Symbol,
|
12
|
-
Module, Class
|
13
|
-
].freeze
|
14
|
-
|
15
|
-
contract(
|
16
|
-
Any => Or[Hash, ArrayOf[Any], *BASIC_TYPES]
|
17
|
-
).throws(RecursiveObjectErr)
|
18
|
-
def call(obj)
|
19
|
-
recursively_convert(obj, {}, {})
|
20
|
-
end
|
21
|
-
|
22
|
-
private
|
23
|
-
|
24
|
-
def recursively_convert(obj, acc, cache)
|
25
|
-
ancestors = obj.class.ancestors
|
26
|
-
|
27
|
-
if ancestors.intersection(BASIC_TYPES).size > 0
|
28
|
-
obj
|
29
|
-
elsif obj.is_a?(Array)
|
30
|
-
convert_array(obj, acc, cache)
|
31
|
-
elsif obj.is_a?(Hash)
|
32
|
-
convert_hash(obj, acc, cache)
|
33
|
-
elsif obj.respond_to?(:to_h)
|
34
|
-
convert_hash(obj.to_h, acc, cache)
|
35
|
-
else
|
36
|
-
convert_object(obj, acc, cache)
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
def convert_array(obj, acc, cache)
|
41
|
-
obj.map { |el| recursively_convert(el, {}, cache) }
|
42
|
-
end
|
43
|
-
|
44
|
-
def convert_hash(obj, acc, cache)
|
45
|
-
obj.each do |k, v|
|
46
|
-
key_sym = k.to_sym
|
47
|
-
acc[key_sym] = recursively_convert(v, {}, cache)
|
48
|
-
end
|
49
|
-
|
50
|
-
acc
|
51
|
-
end
|
52
|
-
|
53
|
-
def convert_object(obj, acc, cache)
|
54
|
-
return obj if obj.is_a?(Class) || obj.is_a?(Module)
|
55
|
-
|
56
|
-
if cache.key?(obj.object_id)
|
57
|
-
raise RecursiveObjectErr, "Recursive object found: #{obj}"
|
58
|
-
end
|
59
|
-
|
60
|
-
cache[obj.object_id] = acc
|
61
|
-
|
62
|
-
obj.instance_variables.each do |var|
|
63
|
-
key_name = var.to_s.delete("@")
|
64
|
-
key_sym = key_name.to_sym
|
65
|
-
|
66
|
-
key = key_sym
|
67
|
-
value = obj.instance_variable_get(var)
|
68
|
-
|
69
|
-
acc[key] = recursively_convert(value, {}, cache)
|
70
|
-
end
|
71
|
-
|
72
|
-
acc
|
73
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class ReeObject::ToHash
|
4
|
+
include Ree::FnDSL
|
5
|
+
|
6
|
+
fn :to_hash do
|
7
|
+
def_error { RecursiveObjectErr }
|
8
|
+
end
|
9
|
+
|
10
|
+
BASIC_TYPES = [
|
11
|
+
Date, Time, Numeric, String, FalseClass, TrueClass, NilClass, Symbol,
|
12
|
+
Module, Class
|
13
|
+
].freeze
|
14
|
+
|
15
|
+
contract(
|
16
|
+
Any => Or[Hash, ArrayOf[Any], *BASIC_TYPES]
|
17
|
+
).throws(RecursiveObjectErr)
|
18
|
+
def call(obj)
|
19
|
+
recursively_convert(obj, {}, {})
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def recursively_convert(obj, acc, cache)
|
25
|
+
ancestors = obj.class.ancestors
|
26
|
+
|
27
|
+
if ancestors.intersection(BASIC_TYPES).size > 0
|
28
|
+
obj
|
29
|
+
elsif obj.is_a?(Array)
|
30
|
+
convert_array(obj, acc, cache)
|
31
|
+
elsif obj.is_a?(Hash)
|
32
|
+
convert_hash(obj, acc, cache)
|
33
|
+
elsif obj.respond_to?(:to_h)
|
34
|
+
convert_hash(obj.to_h, acc, cache)
|
35
|
+
else
|
36
|
+
convert_object(obj, acc, cache)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def convert_array(obj, acc, cache)
|
41
|
+
obj.map { |el| recursively_convert(el, {}, cache) }
|
42
|
+
end
|
43
|
+
|
44
|
+
def convert_hash(obj, acc, cache)
|
45
|
+
obj.each do |k, v|
|
46
|
+
key_sym = k.to_sym
|
47
|
+
acc[key_sym] = recursively_convert(v, {}, cache)
|
48
|
+
end
|
49
|
+
|
50
|
+
acc
|
51
|
+
end
|
52
|
+
|
53
|
+
def convert_object(obj, acc, cache)
|
54
|
+
return obj if obj.is_a?(Class) || obj.is_a?(Module)
|
55
|
+
|
56
|
+
if cache.key?(obj.object_id)
|
57
|
+
raise RecursiveObjectErr, "Recursive object found: #{obj}"
|
58
|
+
end
|
59
|
+
|
60
|
+
cache[obj.object_id] = acc
|
61
|
+
|
62
|
+
obj.instance_variables.each do |var|
|
63
|
+
key_name = var.to_s.delete("@")
|
64
|
+
key_sym = key_name.to_sym
|
65
|
+
|
66
|
+
key = key_sym
|
67
|
+
value = obj.instance_variable_get(var)
|
68
|
+
|
69
|
+
acc[key] = recursively_convert(value, {}, cache)
|
70
|
+
end
|
71
|
+
|
72
|
+
acc
|
73
|
+
end
|
74
74
|
end
|
@@ -1,97 +1,97 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
class ReeObject::ToObj
|
4
|
-
include Ree::FnDSL
|
5
|
-
|
6
|
-
fn :to_obj do
|
7
|
-
link :as_json, import: -> { BASIC_TYPES }
|
8
|
-
link :slice, from: :ree_hash
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class ReeObject::ToObj
|
4
|
+
include Ree::FnDSL
|
5
|
+
|
6
|
+
fn :to_obj do
|
9
7
|
link :except, from: :ree_hash
|
8
|
+
link :slice, from: :ree_hash
|
9
|
+
link :to_hash, import: -> { BASIC_TYPES }
|
10
10
|
link 'ree_hash/contracts/hash_keys_contract', -> { HashKeysContract }
|
11
|
-
end
|
12
|
-
|
13
|
-
contract(
|
14
|
-
Any,
|
15
|
-
Ksplat[
|
16
|
-
include?: HashKeysContract,
|
17
|
-
exclude?: HashKeysContract,
|
18
|
-
global_exclude?: ArrayOf[Symbol]
|
19
|
-
] => Or[Object, ArrayOf[Object], *BASIC_TYPES]
|
20
|
-
).throws(ArgumentError)
|
21
|
-
def call(obj, **opts)
|
22
|
-
dump =
|
23
|
-
|
24
|
-
options = prepare_options(opts)
|
25
|
-
|
26
|
-
if opts[:include]
|
27
|
-
dump = slice(dump, options[:include])
|
28
|
-
end
|
29
|
-
|
30
|
-
if opts[:exclude]
|
31
|
-
dump = except(dump, options[:exclude])
|
32
|
-
end
|
33
|
-
|
34
|
-
if opts[:global_exclude]
|
35
|
-
dump = except(dump, global_except: options[:global_exclude])
|
36
|
-
end
|
37
|
-
|
38
|
-
ancestors = dump.class.ancestors
|
39
|
-
return dump if ancestors.intersection(BASIC_TYPES).size > 0
|
40
|
-
|
41
|
-
if dump.is_a?(Array)
|
42
|
-
build_array(dump)
|
43
|
-
else
|
44
|
-
recursively_assign(Object.new, dump)
|
45
|
-
end
|
46
|
-
end
|
47
|
-
|
48
|
-
private
|
49
|
-
|
50
|
-
def build_array(array)
|
51
|
-
array.map do |value|
|
52
|
-
if value.is_a?(Array)
|
53
|
-
build_array(value)
|
54
|
-
elsif value.is_a?(Hash)
|
55
|
-
recursively_assign(Object.new, value)
|
56
|
-
else
|
57
|
-
value
|
58
|
-
end
|
59
|
-
end
|
60
|
-
end
|
61
|
-
|
62
|
-
def recursively_assign(obj, hash)
|
63
|
-
hash.each do |key, value|
|
64
|
-
var = :"@#{key}"
|
65
|
-
|
66
|
-
obj.define_singleton_method key do
|
67
|
-
instance_variable_get(var)
|
68
|
-
end
|
69
|
-
|
70
|
-
if value.is_a?(Array)
|
71
|
-
obj.instance_variable_set(var, build_array(value))
|
72
|
-
elsif value.is_a?(Hash)
|
73
|
-
obj.instance_variable_set(var, recursively_assign(Object.new, value))
|
74
|
-
else
|
75
|
-
obj.instance_variable_set(var, value)
|
76
|
-
end
|
77
|
-
end
|
78
|
-
|
79
|
-
obj
|
80
|
-
end
|
81
|
-
|
82
|
-
def prepare_options(opts)
|
83
|
-
if opts[:include] && (opts[:exclude] || opts[:global_exclude])
|
84
|
-
intersection = opts_keys(opts[:include]).intersection(opts_keys(opts[:exclude] || opts[:global_exclude]))
|
85
|
-
|
86
|
-
if intersection.length > 0
|
87
|
-
raise ArgumentError, "Exclude and include have the same values: #{intersection}"
|
88
|
-
end
|
89
|
-
end
|
90
|
-
|
91
|
-
opts
|
92
|
-
end
|
93
|
-
|
94
|
-
def opts_keys(arr)
|
95
|
-
arr.reject { |e| e.is_a?(Hash) }
|
96
|
-
end
|
11
|
+
end
|
12
|
+
|
13
|
+
contract(
|
14
|
+
Any,
|
15
|
+
Ksplat[
|
16
|
+
include?: HashKeysContract,
|
17
|
+
exclude?: HashKeysContract,
|
18
|
+
global_exclude?: ArrayOf[Symbol]
|
19
|
+
] => Or[Object, ArrayOf[Object], *BASIC_TYPES]
|
20
|
+
).throws(ArgumentError)
|
21
|
+
def call(obj, **opts)
|
22
|
+
dump = to_hash(obj)
|
23
|
+
|
24
|
+
options = prepare_options(opts)
|
25
|
+
|
26
|
+
if opts[:include]
|
27
|
+
dump = slice(dump, options[:include])
|
28
|
+
end
|
29
|
+
|
30
|
+
if opts[:exclude]
|
31
|
+
dump = except(dump, options[:exclude])
|
32
|
+
end
|
33
|
+
|
34
|
+
if opts[:global_exclude]
|
35
|
+
dump = except(dump, global_except: options[:global_exclude])
|
36
|
+
end
|
37
|
+
|
38
|
+
ancestors = dump.class.ancestors
|
39
|
+
return dump if ancestors.intersection(BASIC_TYPES).size > 0
|
40
|
+
|
41
|
+
if dump.is_a?(Array)
|
42
|
+
build_array(dump)
|
43
|
+
else
|
44
|
+
recursively_assign(Object.new, dump)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
private
|
49
|
+
|
50
|
+
def build_array(array)
|
51
|
+
array.map do |value|
|
52
|
+
if value.is_a?(Array)
|
53
|
+
build_array(value)
|
54
|
+
elsif value.is_a?(Hash)
|
55
|
+
recursively_assign(Object.new, value)
|
56
|
+
else
|
57
|
+
value
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def recursively_assign(obj, hash)
|
63
|
+
hash.each do |key, value|
|
64
|
+
var = :"@#{key}"
|
65
|
+
|
66
|
+
obj.define_singleton_method key do
|
67
|
+
instance_variable_get(var)
|
68
|
+
end
|
69
|
+
|
70
|
+
if value.is_a?(Array)
|
71
|
+
obj.instance_variable_set(var, build_array(value))
|
72
|
+
elsif value.is_a?(Hash)
|
73
|
+
obj.instance_variable_set(var, recursively_assign(Object.new, value))
|
74
|
+
else
|
75
|
+
obj.instance_variable_set(var, value)
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
obj
|
80
|
+
end
|
81
|
+
|
82
|
+
def prepare_options(opts)
|
83
|
+
if opts[:include] && (opts[:exclude] || opts[:global_exclude])
|
84
|
+
intersection = opts_keys(opts[:include]).intersection(opts_keys(opts[:exclude] || opts[:global_exclude]))
|
85
|
+
|
86
|
+
if intersection.length > 0
|
87
|
+
raise ArgumentError, "Exclude and include have the same values: #{intersection}"
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
opts
|
92
|
+
end
|
93
|
+
|
94
|
+
def opts_keys(arr)
|
95
|
+
arr.reject { |e| e.is_a?(Hash) }
|
96
|
+
end
|
97
97
|
end
|
@@ -1,16 +1,16 @@
|
|
1
1
|
{
|
2
2
|
"schema_type": "object",
|
3
3
|
"schema_version": "1.1",
|
4
|
-
"name": "
|
5
|
-
"path": "packages/ree_object/package/ree_object/functions/
|
4
|
+
"name": "to_hash",
|
5
|
+
"path": "packages/ree_object/package/ree_object/functions/to_hash.rb",
|
6
6
|
"mount_as": "fn",
|
7
|
-
"class": "ReeObject::
|
7
|
+
"class": "ReeObject::ToHash",
|
8
8
|
"factory": null,
|
9
9
|
"methods": [
|
10
10
|
{
|
11
11
|
"doc": "",
|
12
12
|
"throws": [
|
13
|
-
"ReeObject::
|
13
|
+
"ReeObject::ToHash::RecursiveObjectErr"
|
14
14
|
],
|
15
15
|
"return": "Or[Hash, ArrayOf[Any], Date, Time, Numeric, String, FalseClass, TrueClass, NilClass, Symbol, Module, Class]",
|
16
16
|
"args": [
|
@@ -28,14 +28,6 @@
|
|
28
28
|
}
|
29
29
|
],
|
30
30
|
"links": [
|
31
|
-
{
|
32
|
-
"target": "as_json",
|
33
|
-
"package_name": "ree_object",
|
34
|
-
"as": "as_json",
|
35
|
-
"imports": [
|
36
|
-
"BASIC_TYPES"
|
37
|
-
]
|
38
|
-
},
|
39
31
|
{
|
40
32
|
"target": "except",
|
41
33
|
"package_name": "ree_hash",
|
@@ -51,6 +43,14 @@
|
|
51
43
|
"imports": [
|
52
44
|
|
53
45
|
]
|
46
|
+
},
|
47
|
+
{
|
48
|
+
"target": "to_hash",
|
49
|
+
"package_name": "ree_object",
|
50
|
+
"as": "to_hash",
|
51
|
+
"imports": [
|
52
|
+
"BASIC_TYPES"
|
53
|
+
]
|
54
54
|
}
|
55
55
|
]
|
56
56
|
}
|
data/lib/ree_lib/packages/ree_object/spec/ree_object/functions/{as_json_spec.rb → to_hash_spec.rb}
RENAMED
@@ -1,172 +1,172 @@
|
|
1
|
-
# frozen_string_literal = true
|
2
|
-
|
3
|
-
RSpec.describe :
|
4
|
-
link :
|
5
|
-
|
6
|
-
context "object" do
|
7
|
-
let(:klass) {
|
8
|
-
Class.new do
|
9
|
-
attr_reader :integer, :string, :array, :hash, :object
|
10
|
-
|
11
|
-
def initialize
|
12
|
-
@integer = 1
|
13
|
-
@string = 'string'
|
14
|
-
@array = [1, 'string', 3, { 'name' => 'John'}]
|
15
|
-
|
16
|
-
@hash = {
|
17
|
-
id: 1,
|
18
|
-
'test' => 2,
|
19
|
-
nested: {
|
20
|
-
some_value: 1,
|
21
|
-
another_value: 2
|
22
|
-
},
|
23
|
-
name: 'name'
|
24
|
-
}
|
25
|
-
|
26
|
-
@object = Object.new
|
27
|
-
@klass = Object
|
28
|
-
@module = Module
|
29
|
-
@object.instance_exec do
|
30
|
-
@name = 'John'
|
31
|
-
@last_name = 'Doe'
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|
35
|
-
}
|
36
|
-
|
37
|
-
context "Struct" do
|
38
|
-
it {
|
39
|
-
klass = Struct.new(:id, :name)
|
40
|
-
result =
|
41
|
-
|
42
|
-
expect(result).to eq({id: 1, name: 'John'})
|
43
|
-
}
|
44
|
-
end
|
45
|
-
|
46
|
-
context "OpenStruct" do
|
47
|
-
it {
|
48
|
-
require 'ostruct'
|
49
|
-
obj = OpenStruct.new(id: 1, name: 'John')
|
50
|
-
result =
|
51
|
-
|
52
|
-
expect(result).to eq({id: 1, name: 'John'})
|
53
|
-
}
|
54
|
-
end
|
55
|
-
|
56
|
-
context "basic types" do
|
57
|
-
it {
|
58
|
-
obj = Date.new
|
59
|
-
result =
|
60
|
-
expect(result).to eq(obj)
|
61
|
-
}
|
62
|
-
|
63
|
-
it {
|
64
|
-
obj = Time.new
|
65
|
-
result =
|
66
|
-
expect(result).to eq(obj)
|
67
|
-
}
|
68
|
-
|
69
|
-
it {
|
70
|
-
obj = 1
|
71
|
-
result =
|
72
|
-
expect(result).to eq(obj)
|
73
|
-
}
|
74
|
-
|
75
|
-
it {
|
76
|
-
obj = "string"
|
77
|
-
result =
|
78
|
-
expect(result).to eq(obj)
|
79
|
-
}
|
80
|
-
|
81
|
-
it {
|
82
|
-
obj = true
|
83
|
-
result =
|
84
|
-
expect(result).to eq(obj)
|
85
|
-
}
|
86
|
-
|
87
|
-
it {
|
88
|
-
obj = false
|
89
|
-
result =
|
90
|
-
expect(result).to eq(obj)
|
91
|
-
}
|
92
|
-
|
93
|
-
it {
|
94
|
-
obj = nil
|
95
|
-
result =
|
96
|
-
expect(result).to eq(obj)
|
97
|
-
}
|
98
|
-
|
99
|
-
it {
|
100
|
-
obj = :symbol
|
101
|
-
result =
|
102
|
-
expect(result).to eq(obj)
|
103
|
-
}
|
104
|
-
|
105
|
-
it {
|
106
|
-
obj = Object.new
|
107
|
-
result =
|
108
|
-
expect(result).to eq({})
|
109
|
-
}
|
110
|
-
|
111
|
-
it {
|
112
|
-
obj = Class
|
113
|
-
result =
|
114
|
-
expect(result).to eq(obj)
|
115
|
-
}
|
116
|
-
|
117
|
-
it {
|
118
|
-
obj = Module
|
119
|
-
result =
|
120
|
-
expect(result).to eq(obj)
|
121
|
-
}
|
122
|
-
end
|
123
|
-
|
124
|
-
context "object" do
|
125
|
-
it {
|
126
|
-
result =
|
127
|
-
|
128
|
-
expected = {
|
129
|
-
integer: 1,
|
130
|
-
string: 'string',
|
131
|
-
array: [1, 'string', 3, { name: 'John'}],
|
132
|
-
hash: {
|
133
|
-
id: 1,
|
134
|
-
test: 2,
|
135
|
-
nested: {
|
136
|
-
some_value: 1,
|
137
|
-
another_value: 2
|
138
|
-
},
|
139
|
-
name: 'name'
|
140
|
-
},
|
141
|
-
klass: Object,
|
142
|
-
module: Module,
|
143
|
-
object: {
|
144
|
-
name: 'John',
|
145
|
-
last_name: 'Doe'
|
146
|
-
}
|
147
|
-
}
|
148
|
-
|
149
|
-
expect(result).to eq(expected)
|
150
|
-
}
|
151
|
-
end
|
152
|
-
|
153
|
-
context "check for recursion" do
|
154
|
-
let(:obj_klass) {
|
155
|
-
Class.new do
|
156
|
-
def set(v)
|
157
|
-
@value = v
|
158
|
-
end
|
159
|
-
end
|
160
|
-
}
|
161
|
-
|
162
|
-
it {
|
163
|
-
obj = obj_klass.new
|
164
|
-
obj.set([obj])
|
165
|
-
|
166
|
-
expect {
|
167
|
-
|
168
|
-
}.to raise_error(ReeObject::
|
169
|
-
}
|
170
|
-
end
|
171
|
-
end
|
1
|
+
# frozen_string_literal = true
|
2
|
+
|
3
|
+
RSpec.describe :to_hash do
|
4
|
+
link :to_hash, from: :ree_object
|
5
|
+
|
6
|
+
context "object" do
|
7
|
+
let(:klass) {
|
8
|
+
Class.new do
|
9
|
+
attr_reader :integer, :string, :array, :hash, :object
|
10
|
+
|
11
|
+
def initialize
|
12
|
+
@integer = 1
|
13
|
+
@string = 'string'
|
14
|
+
@array = [1, 'string', 3, { 'name' => 'John'}]
|
15
|
+
|
16
|
+
@hash = {
|
17
|
+
id: 1,
|
18
|
+
'test' => 2,
|
19
|
+
nested: {
|
20
|
+
some_value: 1,
|
21
|
+
another_value: 2
|
22
|
+
},
|
23
|
+
name: 'name'
|
24
|
+
}
|
25
|
+
|
26
|
+
@object = Object.new
|
27
|
+
@klass = Object
|
28
|
+
@module = Module
|
29
|
+
@object.instance_exec do
|
30
|
+
@name = 'John'
|
31
|
+
@last_name = 'Doe'
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
}
|
36
|
+
|
37
|
+
context "Struct" do
|
38
|
+
it {
|
39
|
+
klass = Struct.new(:id, :name)
|
40
|
+
result = to_hash(klass.new(1, 'John'))
|
41
|
+
|
42
|
+
expect(result).to eq({id: 1, name: 'John'})
|
43
|
+
}
|
44
|
+
end
|
45
|
+
|
46
|
+
context "OpenStruct" do
|
47
|
+
it {
|
48
|
+
require 'ostruct'
|
49
|
+
obj = OpenStruct.new(id: 1, name: 'John')
|
50
|
+
result = to_hash(obj)
|
51
|
+
|
52
|
+
expect(result).to eq({id: 1, name: 'John'})
|
53
|
+
}
|
54
|
+
end
|
55
|
+
|
56
|
+
context "basic types" do
|
57
|
+
it {
|
58
|
+
obj = Date.new
|
59
|
+
result = to_hash(obj)
|
60
|
+
expect(result).to eq(obj)
|
61
|
+
}
|
62
|
+
|
63
|
+
it {
|
64
|
+
obj = Time.new
|
65
|
+
result = to_hash(obj)
|
66
|
+
expect(result).to eq(obj)
|
67
|
+
}
|
68
|
+
|
69
|
+
it {
|
70
|
+
obj = 1
|
71
|
+
result = to_hash(obj)
|
72
|
+
expect(result).to eq(obj)
|
73
|
+
}
|
74
|
+
|
75
|
+
it {
|
76
|
+
obj = "string"
|
77
|
+
result = to_hash(obj)
|
78
|
+
expect(result).to eq(obj)
|
79
|
+
}
|
80
|
+
|
81
|
+
it {
|
82
|
+
obj = true
|
83
|
+
result = to_hash(obj)
|
84
|
+
expect(result).to eq(obj)
|
85
|
+
}
|
86
|
+
|
87
|
+
it {
|
88
|
+
obj = false
|
89
|
+
result = to_hash(obj)
|
90
|
+
expect(result).to eq(obj)
|
91
|
+
}
|
92
|
+
|
93
|
+
it {
|
94
|
+
obj = nil
|
95
|
+
result = to_hash(obj)
|
96
|
+
expect(result).to eq(obj)
|
97
|
+
}
|
98
|
+
|
99
|
+
it {
|
100
|
+
obj = :symbol
|
101
|
+
result = to_hash(obj)
|
102
|
+
expect(result).to eq(obj)
|
103
|
+
}
|
104
|
+
|
105
|
+
it {
|
106
|
+
obj = Object.new
|
107
|
+
result = to_hash(obj)
|
108
|
+
expect(result).to eq({})
|
109
|
+
}
|
110
|
+
|
111
|
+
it {
|
112
|
+
obj = Class
|
113
|
+
result = to_hash(obj)
|
114
|
+
expect(result).to eq(obj)
|
115
|
+
}
|
116
|
+
|
117
|
+
it {
|
118
|
+
obj = Module
|
119
|
+
result = to_hash(obj)
|
120
|
+
expect(result).to eq(obj)
|
121
|
+
}
|
122
|
+
end
|
123
|
+
|
124
|
+
context "object" do
|
125
|
+
it {
|
126
|
+
result = to_hash(klass.new)
|
127
|
+
|
128
|
+
expected = {
|
129
|
+
integer: 1,
|
130
|
+
string: 'string',
|
131
|
+
array: [1, 'string', 3, { name: 'John'}],
|
132
|
+
hash: {
|
133
|
+
id: 1,
|
134
|
+
test: 2,
|
135
|
+
nested: {
|
136
|
+
some_value: 1,
|
137
|
+
another_value: 2
|
138
|
+
},
|
139
|
+
name: 'name'
|
140
|
+
},
|
141
|
+
klass: Object,
|
142
|
+
module: Module,
|
143
|
+
object: {
|
144
|
+
name: 'John',
|
145
|
+
last_name: 'Doe'
|
146
|
+
}
|
147
|
+
}
|
148
|
+
|
149
|
+
expect(result).to eq(expected)
|
150
|
+
}
|
151
|
+
end
|
152
|
+
|
153
|
+
context "check for recursion" do
|
154
|
+
let(:obj_klass) {
|
155
|
+
Class.new do
|
156
|
+
def set(v)
|
157
|
+
@value = v
|
158
|
+
end
|
159
|
+
end
|
160
|
+
}
|
161
|
+
|
162
|
+
it {
|
163
|
+
obj = obj_klass.new
|
164
|
+
obj.set([obj])
|
165
|
+
|
166
|
+
expect {
|
167
|
+
to_hash(obj)
|
168
|
+
}.to raise_error(ReeObject::ToHash::RecursiveObjectErr, /Recursive object found: /)
|
169
|
+
}
|
170
|
+
end
|
171
|
+
end
|
172
172
|
end
|
data/lib/ree_lib/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ree_lib
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.34
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ruslan Gatiyatov
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-
|
11
|
+
date: 2023-03-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: ree
|
@@ -1020,29 +1020,29 @@ files:
|
|
1020
1020
|
- lib/ree_lib/packages/ree_object/Package.schema.json
|
1021
1021
|
- lib/ree_lib/packages/ree_object/bin/console
|
1022
1022
|
- lib/ree_lib/packages/ree_object/package/ree_object.rb
|
1023
|
-
- lib/ree_lib/packages/ree_object/package/ree_object/functions/as_json.rb
|
1024
1023
|
- lib/ree_lib/packages/ree_object/package/ree_object/functions/deep_dup.rb
|
1025
1024
|
- lib/ree_lib/packages/ree_object/package/ree_object/functions/deep_freeze.rb
|
1026
1025
|
- lib/ree_lib/packages/ree_object/package/ree_object/functions/dump_as_json.rb
|
1027
1026
|
- lib/ree_lib/packages/ree_object/package/ree_object/functions/is_blank.rb
|
1028
1027
|
- lib/ree_lib/packages/ree_object/package/ree_object/functions/load_json_dump.rb
|
1029
1028
|
- lib/ree_lib/packages/ree_object/package/ree_object/functions/not_blank.rb
|
1029
|
+
- lib/ree_lib/packages/ree_object/package/ree_object/functions/to_hash.rb
|
1030
1030
|
- lib/ree_lib/packages/ree_object/package/ree_object/functions/to_obj.rb
|
1031
|
-
- lib/ree_lib/packages/ree_object/schemas/ree_object/functions/as_json.schema.json
|
1032
1031
|
- lib/ree_lib/packages/ree_object/schemas/ree_object/functions/deep_dup.schema.json
|
1033
1032
|
- lib/ree_lib/packages/ree_object/schemas/ree_object/functions/deep_freeze.schema.json
|
1034
1033
|
- lib/ree_lib/packages/ree_object/schemas/ree_object/functions/dump_as_json.schema.json
|
1035
1034
|
- lib/ree_lib/packages/ree_object/schemas/ree_object/functions/is_blank.schema.json
|
1036
1035
|
- lib/ree_lib/packages/ree_object/schemas/ree_object/functions/load_json_dump.schema.json
|
1037
1036
|
- lib/ree_lib/packages/ree_object/schemas/ree_object/functions/not_blank.schema.json
|
1037
|
+
- lib/ree_lib/packages/ree_object/schemas/ree_object/functions/to_hash.schema.json
|
1038
1038
|
- lib/ree_lib/packages/ree_object/schemas/ree_object/functions/to_obj.schema.json
|
1039
1039
|
- lib/ree_lib/packages/ree_object/spec/package_schema_spec.rb
|
1040
|
-
- lib/ree_lib/packages/ree_object/spec/ree_object/functions/as_json_spec.rb
|
1041
1040
|
- lib/ree_lib/packages/ree_object/spec/ree_object/functions/deep_dup_spec.rb
|
1042
1041
|
- lib/ree_lib/packages/ree_object/spec/ree_object/functions/deep_freeze_spec.rb
|
1043
1042
|
- lib/ree_lib/packages/ree_object/spec/ree_object/functions/dump_as_json_spec.rb
|
1044
1043
|
- lib/ree_lib/packages/ree_object/spec/ree_object/functions/is_blank_spec.rb
|
1045
1044
|
- lib/ree_lib/packages/ree_object/spec/ree_object/functions/not_blank_spec.rb
|
1045
|
+
- lib/ree_lib/packages/ree_object/spec/ree_object/functions/to_hash_spec.rb
|
1046
1046
|
- lib/ree_lib/packages/ree_object/spec/ree_object/functions/to_obj_spec.rb
|
1047
1047
|
- lib/ree_lib/packages/ree_object/spec/spec_helper.rb
|
1048
1048
|
- lib/ree_lib/packages/ree_string/.gitignore
|