dorian-to_struct 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/lib/dorian/core_ext/array/struct.rb +24 -0
- data/lib/dorian/core_ext/hash/struct.rb +28 -0
- data/lib/dorian/to_struct.rb +2 -0
- metadata +48 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 0d2ae1228e241d6efab9b69063436a06fa33bd77396b73fa9b1fad447123d38b
|
4
|
+
data.tar.gz: 0b14f6062c3d4195d1772410fa4507efe1ea9dd7fbe63a38c6a7f1e15b3d0fe2
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: bc44b0c1d25518ae064145a75653b04e329c6c04e0818f71b17394c9e5632838b7146d0760c382d77e22e1cfbec0f4ad057ab8c575e1fc22fa19b578f522e92f
|
7
|
+
data.tar.gz: 28e0262534e2dd8c1552735c776e655fe4268a49e732698f2ed127e4d57e9aa0326682f7ad44a0437938dfbb716414b2c828f734e1578a3858f55e5d0798b6c4
|
@@ -0,0 +1,24 @@
|
|
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
|
@@ -0,0 +1,28 @@
|
|
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).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
|
21
|
+
.new(*keys)
|
22
|
+
.new(
|
23
|
+
*values.map do |value|
|
24
|
+
value.respond_to?(:to_deep_struct) ? value.to_deep_struct : value
|
25
|
+
end
|
26
|
+
)
|
27
|
+
end
|
28
|
+
end
|
metadata
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: dorian-to_struct
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Dorian Marié
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2021-11-12 00:00:00.000000000 Z
|
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.
|
17
|
+
email: dorian@dorianmarie.fr
|
18
|
+
executables: []
|
19
|
+
extensions: []
|
20
|
+
extra_rdoc_files: []
|
21
|
+
files:
|
22
|
+
- lib/dorian/core_ext/array/struct.rb
|
23
|
+
- lib/dorian/core_ext/hash/struct.rb
|
24
|
+
- lib/dorian/to_struct.rb
|
25
|
+
homepage: https://github.com/dorianmariefr/to_struct
|
26
|
+
licenses:
|
27
|
+
- MIT
|
28
|
+
metadata: {}
|
29
|
+
post_install_message:
|
30
|
+
rdoc_options: []
|
31
|
+
require_paths:
|
32
|
+
- lib
|
33
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
34
|
+
requirements:
|
35
|
+
- - ">="
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
39
|
+
requirements:
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '0'
|
43
|
+
requirements: []
|
44
|
+
rubygems_version: 3.2.22
|
45
|
+
signing_key:
|
46
|
+
specification_version: 4
|
47
|
+
summary: Adds `#to_struct` and `#to_deep_struct` to Hash and Array
|
48
|
+
test_files: []
|