dorian-to_struct 0.4.2 → 0.5.1
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 +18 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b1b7fed4587dddb8df16482d9d812ae7656a0632bcbe0e6b04f71c781b391be7
|
4
|
+
data.tar.gz: 18d380b30f9cdf56be110ea61a2d0cc734ff283f05201a90c26ad12e738900ed
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3555827f5e2c1b8f51b8ffd3ad453c18614a1d1a67ed9f38db7ad52fe39b8b2392199fa865b2277ff15d4fc2fd239b32e001f8fb9289bc93b1e0dca9f13fa55a
|
7
|
+
data.tar.gz: 064bd8a1eff196e75f163862fa3c7e1cceb758eda8bc9c183929cc43cd3455a4eb7a5e5463d638459bd2958f6f2b1a0b3bb3a9a5c3e8eadf255f5faf288ca8cd
|
@@ -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,15 +1,29 @@
|
|
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.1
|
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-
|
12
|
-
dependencies:
|
11
|
+
date: 2024-08-22 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: dorian-arguments
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
13
27
|
description: makes structs
|
14
28
|
email: dorian@dorianmarie.com
|
15
29
|
executables: []
|
@@ -17,6 +31,7 @@ extensions: []
|
|
17
31
|
extra_rdoc_files: []
|
18
32
|
files:
|
19
33
|
- lib/dorian-to_struct.rb
|
34
|
+
- lib/dorian/to_struct.rb
|
20
35
|
homepage: https://github.com/dorianmariecom/dorian-to_struct
|
21
36
|
licenses:
|
22
37
|
- MIT
|