dorian-to_struct 0.4.0 → 0.5.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 -0
- data/lib/dorian-to_struct.rb +1 -47
- metadata +5 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 107b93efe81a4c2a06696885a495af7b232e4e894afe4ed019fce897008ecb72
|
4
|
+
data.tar.gz: e8ef801dc9f6edb7fac6dc1def40376e9699b8f3bab9e6811e862b13e9d67702
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 683d819c51fd4e88d8a43afd0060948fceb14d982d9063da06eb97b2b8266cdcf764dd00b946abef39bdd30e4a97ebb09e7c73caf83b9b8fb972ed7eee2b0def
|
7
|
+
data.tar.gz: 8cef551c61f4bba8b5508f7d0a522e5aa19b3da677b69e0709a106232e1b60cdc781ee2bbe5f9e04b6de42821610af52c828505369ea09d5ade51aa6499e9b1d
|
@@ -0,0 +1,47 @@
|
|
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
|
+
def to_struct
|
9
|
+
map { |item| item.respond_to?(:to_struct) ? item.to_struct : item }
|
10
|
+
end
|
11
|
+
|
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
|
+
def to_deep_struct
|
18
|
+
map do |item|
|
19
|
+
item.respond_to?(:to_deep_struct) ? item.to_deep_struct : item
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
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
|
+
def to_struct
|
32
|
+
Struct.new(*keys.map(&:to_sym)).new(*values)
|
33
|
+
end
|
34
|
+
|
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
|
+
def to_deep_struct
|
41
|
+
Struct.new(*keys.map(&:to_sym)).new(
|
42
|
+
*values.map do |value|
|
43
|
+
value.respond_to?(:to_deep_struct) ? value.to_deep_struct : value
|
44
|
+
end
|
45
|
+
)
|
46
|
+
end
|
47
|
+
end
|
data/lib/dorian-to_struct.rb
CHANGED
@@ -1,49 +1,3 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
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
|
3
|
+
require_relative "dorian/to_struct"
|
metadata
CHANGED
@@ -1,25 +1,23 @@
|
|
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.5.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-08-18 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
|
-
description:
|
14
|
-
Adds `#to_struct` and `#to_deep_struct` to Hash and Array
|
15
|
-
|
16
|
-
e.g. user.first_name, user.events.first.name, etc.
|
13
|
+
description: makes structs
|
17
14
|
email: dorian@dorianmarie.com
|
18
15
|
executables: []
|
19
16
|
extensions: []
|
20
17
|
extra_rdoc_files: []
|
21
18
|
files:
|
22
19
|
- lib/dorian-to_struct.rb
|
20
|
+
- lib/dorian/to_struct.rb
|
23
21
|
homepage: https://github.com/dorianmariecom/dorian-to_struct
|
24
22
|
licenses:
|
25
23
|
- MIT
|
@@ -43,5 +41,5 @@ requirements: []
|
|
43
41
|
rubygems_version: 3.5.11
|
44
42
|
signing_key:
|
45
43
|
specification_version: 4
|
46
|
-
summary:
|
44
|
+
summary: makes structs
|
47
45
|
test_files: []
|