dorian-to_struct 0.5.2 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/dorian/to_struct.rb +21 -31
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1df7e372e064769577c0f935aba5a031c018cd20773d0179fd235d6389f42b03
|
4
|
+
data.tar.gz: a36461a79045aadc2d2db55cd8ddf865fee2084e6394a40638622397b6c6140b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3b2f021e76fc46741709ab82571d463edbfd3d5450dbe605e60641845419fd696f1a077e1b877ae2a6c54e34786bda3a2de58316db5c05cb1cdde87d06f4f444
|
7
|
+
data.tar.gz: 066f280876341a13d489a0f5ecc66115702c7197c8eb920091bb96f76768c8d319bcc3b500696b0899fe0953ec7feaed37de45cc7a9d9be3a8464cf5fbc72d38
|
data/lib/dorian/to_struct.rb
CHANGED
@@ -1,47 +1,37 @@
|
|
1
1
|
class Array
|
2
|
-
# Returns a new struct with the same key/value pairs
|
3
|
-
#
|
4
|
-
# users = [{ first_name: "Dorian", last_name: "Marié" }].to_struct
|
5
|
-
# users.first.first_name # => "Dorian"
|
6
|
-
# users.first.birthdate # => NoMethodError: undefined method `birthdate' for <struct...>
|
7
|
-
#
|
8
2
|
def to_struct
|
9
|
-
map
|
3
|
+
map(&:to_struct)
|
10
4
|
end
|
11
5
|
|
12
|
-
# Returns a new struct with the same key/value pairs
|
13
|
-
#
|
14
|
-
# users = [{ name: { first: "Dorian", last: "Marié" }].to_struct
|
15
|
-
# users.first.name.first # => "Dorian"
|
16
|
-
#
|
17
6
|
def to_deep_struct
|
18
|
-
map
|
19
|
-
item.respond_to?(:to_deep_struct) ? item.to_deep_struct : item
|
20
|
-
end
|
7
|
+
map(&:to_deep_struct)
|
21
8
|
end
|
22
9
|
end
|
23
10
|
|
24
11
|
class Hash
|
25
|
-
# Returns a new struct with the same key/value pairs
|
26
|
-
#
|
27
|
-
# user = { first_name: "Dorian", last_name: "Marié" }.to_struct
|
28
|
-
# user.first_name # => "Dorian"
|
29
|
-
# user.birthdate # => NoMethodError: undefined method `birthdate' for <struct...>
|
30
|
-
#
|
31
12
|
def to_struct
|
32
|
-
Struct.new(
|
13
|
+
Struct.new(
|
14
|
+
*keys.map(&:to_sym)
|
15
|
+
).new(
|
16
|
+
*values
|
17
|
+
)
|
33
18
|
end
|
34
19
|
|
35
|
-
# Returns a new struct with the same key/value pairs
|
36
|
-
#
|
37
|
-
# user = { name: "Dorian", events: [{ name: "Party" }] }.to_deep_struct
|
38
|
-
# user.events.first.name # => "Party"
|
39
|
-
#
|
40
20
|
def to_deep_struct
|
41
|
-
Struct.new(
|
42
|
-
*
|
43
|
-
|
44
|
-
|
21
|
+
Struct.new(
|
22
|
+
*keys.map(&:to_sym)
|
23
|
+
).new(
|
24
|
+
*values.map(&:to_deep_struct)
|
45
25
|
)
|
46
26
|
end
|
47
27
|
end
|
28
|
+
|
29
|
+
class Object
|
30
|
+
def to_struct
|
31
|
+
self
|
32
|
+
end
|
33
|
+
|
34
|
+
def to_deep_struct
|
35
|
+
self
|
36
|
+
end
|
37
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dorian-to_struct
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dorian Marié
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-08-
|
11
|
+
date: 2024-08-24 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: makes structs
|
14
14
|
email: dorian@dorianmarie.com
|