dorian-to_struct 0.2.1 → 0.3.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 +47 -1
- metadata +4 -7
- data/lib/dorian/core_ext/array/struct.rb +0 -24
- data/lib/dorian/core_ext/hash/struct.rb +0 -26
- data/lib/dorian/to_struct.rb +0 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0b5697bbb3176e50562befdb3e9c7076b2e3f0c1f30fcb192f9602783515e558
|
4
|
+
data.tar.gz: df36a098c08b76d60a464850f6c611eeef12a22963720a01b2b46c1c9f04ff01
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 91974c47006a7bb695b90ce6a2bd924b56baf8a76eb97d55c1bcc24b08dd347252c1c44f2782e8e687a5136eae7057088d117ff9ee274b37150d83d71d66c5a6
|
7
|
+
data.tar.gz: 53eae6009090aa26873dbbfaa3c897284ed6d8448ea2027f6910e3b034b77a27a4bd0a90e466e1a14368ebd48a66723b822cd4eeaf71c8f05853bb5a52c28726
|
data/lib/dorian-to_struct.rb
CHANGED
@@ -1,3 +1,49 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
3
|
+
class Array
|
4
|
+
# Returns a new struct with the same key/value pairs
|
5
|
+
#
|
6
|
+
# users = [{ first_name: "Dorian", last_name: "Marié" }].to_struct
|
7
|
+
# users.first.first_name # => "Dorian"
|
8
|
+
# users.first.birthdate # => NoMethodError: undefined method `birthdate' for <struct...>
|
9
|
+
#
|
10
|
+
def to_struct
|
11
|
+
map { |item| item.respond_to?(:to_struct) ? item.to_struct : item }
|
12
|
+
end
|
13
|
+
|
14
|
+
# Returns a new struct with the same key/value pairs
|
15
|
+
#
|
16
|
+
# users = [{ name: { first: "Dorian", last: "Marié" }].to_struct
|
17
|
+
# users.first.name.first # => "Dorian"
|
18
|
+
#
|
19
|
+
def to_deep_struct
|
20
|
+
map do |item|
|
21
|
+
item.respond_to?(:to_deep_struct) ? item.to_deep_struct : item
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
class Hash
|
27
|
+
# Returns a new struct with the same key/value pairs
|
28
|
+
#
|
29
|
+
# user = { first_name: "Dorian", last_name: "Marié" }.to_struct
|
30
|
+
# user.first_name # => "Dorian"
|
31
|
+
# user.birthdate # => NoMethodError: undefined method `birthdate' for <struct...>
|
32
|
+
#
|
33
|
+
def to_struct
|
34
|
+
Struct.new(*keys.map(&:to_sym)).new(*values)
|
35
|
+
end
|
36
|
+
|
37
|
+
# Returns a new struct with the same key/value pairs
|
38
|
+
#
|
39
|
+
# user = { name: "Dorian", events: [{ name: "Party" }] }.to_deep_struct
|
40
|
+
# user.events.first.name # => "Party"
|
41
|
+
#
|
42
|
+
def to_deep_struct
|
43
|
+
Struct.new(*keys.map(&:to_sym)).new(
|
44
|
+
*values.map do |value|
|
45
|
+
value.respond_to?(:to_deep_struct) ? value.to_deep_struct : value
|
46
|
+
end
|
47
|
+
)
|
48
|
+
end
|
49
|
+
end
|
metadata
CHANGED
@@ -1,29 +1,26 @@
|
|
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.3.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-
|
11
|
+
date: 2024-03-09 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: |-
|
14
14
|
Adds `#to_struct` and `#to_deep_struct` to Hash and Array
|
15
15
|
|
16
16
|
e.g. user.first_name, user.events.first.name, etc.
|
17
|
-
email: dorian@dorianmarie.
|
17
|
+
email: dorian@dorianmarie.com
|
18
18
|
executables: []
|
19
19
|
extensions: []
|
20
20
|
extra_rdoc_files: []
|
21
21
|
files:
|
22
22
|
- lib/dorian-to_struct.rb
|
23
|
-
|
24
|
-
- lib/dorian/core_ext/hash/struct.rb
|
25
|
-
- lib/dorian/to_struct.rb
|
26
|
-
homepage: https://github.com/dorianmariecom/to_struct
|
23
|
+
homepage: https://github.com/dorianmariecom/dorian-to_struct
|
27
24
|
licenses:
|
28
25
|
- MIT
|
29
26
|
metadata:
|
@@ -1,24 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
class Array
|
4
|
-
# Returns a new struct with the same key/value pairs
|
5
|
-
#
|
6
|
-
# users = [{ first_name: "Dorian", last_name: "Marié" }].to_struct
|
7
|
-
# users.first.first_name # => "Dorian"
|
8
|
-
# users.first.birthdate # => NoMethodError: undefined method `birthdate' for <struct...>
|
9
|
-
#
|
10
|
-
def to_struct
|
11
|
-
map { |item| item.respond_to?(:to_struct) ? item.to_struct : item }
|
12
|
-
end
|
13
|
-
|
14
|
-
# Returns a new struct with the same key/value pairs
|
15
|
-
#
|
16
|
-
# users = [{ name: { first: "Dorian", last: "Marié" }].to_struct
|
17
|
-
# users.first.name.first # => "Dorian"
|
18
|
-
#
|
19
|
-
def to_deep_struct
|
20
|
-
map do |item|
|
21
|
-
item.respond_to?(:to_deep_struct) ? item.to_deep_struct : item
|
22
|
-
end
|
23
|
-
end
|
24
|
-
end
|
@@ -1,26 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
class Hash
|
4
|
-
# Returns a new struct with the same key/value pairs
|
5
|
-
#
|
6
|
-
# user = { first_name: "Dorian", last_name: "Marié" }.to_struct
|
7
|
-
# user.first_name # => "Dorian"
|
8
|
-
# user.birthdate # => NoMethodError: undefined method `birthdate' for <struct...>
|
9
|
-
#
|
10
|
-
def to_struct
|
11
|
-
Struct.new(*keys.map(&:to_sym)).new(*values)
|
12
|
-
end
|
13
|
-
|
14
|
-
# Returns a new struct with the same key/value pairs
|
15
|
-
#
|
16
|
-
# user = { name: "Dorian", events: [{ name: "Party" }] }.to_deep_struct
|
17
|
-
# user.events.first.name # => "Party"
|
18
|
-
#
|
19
|
-
def to_deep_struct
|
20
|
-
Struct.new(*keys.map(&:to_sym)).new(
|
21
|
-
*values.map do |value|
|
22
|
-
value.respond_to?(:to_deep_struct) ? value.to_deep_struct : value
|
23
|
-
end
|
24
|
-
)
|
25
|
-
end
|
26
|
-
end
|
data/lib/dorian/to_struct.rb
DELETED