dorian-to_struct 0.2.2 → 0.3.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f1cda01a7c0a27a312357aa4ca3e4f2d298be70f40addbc211ba4397523b25a5
4
- data.tar.gz: 670052320db935fd912619b6f83007a1cf1a951f4e82439b189b75eac0e18409
3
+ metadata.gz: 0545e1b659630cc14f747e25821a18b08eefb2388defcb19f2909d812516c2ca
4
+ data.tar.gz: a75e3a131ca41aef38cdd0ed3d399981915fe1789eb8c085fa65302b409b42c6
5
5
  SHA512:
6
- metadata.gz: 55126ce614849a9cdebebd63b6533f330c121fd3a5c0f510be91de1ad030f7f965a30b91c631a95791e366908497f66d6b0045ba946ce72f1665c53af8d6a6a6
7
- data.tar.gz: 96d8be2a7adfce566c0a665f18df0532ee3087eadaf65e6512f6e4cd25af1f57d7e45ab85514cd63dfc1229e41848b777b7535c45f9a98e581689553f690fea7
6
+ metadata.gz: 10746e0204d8dd152695f5947157741accce58fa669ad769bf843886224546dd77d9634f631b828f83da21856b28a23faa64b382b914bcbd46c946f521ca5f90
7
+ data.tar.gz: b8b44335f31e7a770df51be87b02b59f0f2a3746c4982326f919dfe8dc70d2ef2a4bcca9c947854c6a3393e1f76e15660cd3e9ffeb48a9e13a5ce57f851f8a4a
@@ -1,3 +1,49 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "dorian/to_struct"
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,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.2.2
4
+ version: 0.3.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-03-09 00:00:00.000000000 Z
11
+ date: 2024-04-03 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: |-
14
14
  Adds `#to_struct` and `#to_deep_struct` to Hash and Array
@@ -20,10 +20,7 @@ extensions: []
20
20
  extra_rdoc_files: []
21
21
  files:
22
22
  - lib/dorian-to_struct.rb
23
- - lib/dorian/core_ext/array/struct.rb
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
@@ -1,4 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative "core_ext/hash/struct"
4
- require_relative "core_ext/array/struct"