dorian-to_struct 0.2.2 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f1cda01a7c0a27a312357aa4ca3e4f2d298be70f40addbc211ba4397523b25a5
4
- data.tar.gz: 670052320db935fd912619b6f83007a1cf1a951f4e82439b189b75eac0e18409
3
+ metadata.gz: 0b5697bbb3176e50562befdb3e9c7076b2e3f0c1f30fcb192f9602783515e558
4
+ data.tar.gz: df36a098c08b76d60a464850f6c611eeef12a22963720a01b2b46c1c9f04ff01
5
5
  SHA512:
6
- metadata.gz: 55126ce614849a9cdebebd63b6533f330c121fd3a5c0f510be91de1ad030f7f965a30b91c631a95791e366908497f66d6b0045ba946ce72f1665c53af8d6a6a6
7
- data.tar.gz: 96d8be2a7adfce566c0a665f18df0532ee3087eadaf65e6512f6e4cd25af1f57d7e45ab85514cd63dfc1229e41848b777b7535c45f9a98e581689553f690fea7
6
+ metadata.gz: 91974c47006a7bb695b90ce6a2bd924b56baf8a76eb97d55c1bcc24b08dd347252c1c44f2782e8e687a5136eae7057088d117ff9ee274b37150d83d71d66c5a6
7
+ data.tar.gz: 53eae6009090aa26873dbbfaa3c897284ed6d8448ea2027f6910e3b034b77a27a4bd0a90e466e1a14368ebd48a66723b822cd4eeaf71c8f05853bb5a52c28726
@@ -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,7 +1,7 @@
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.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dorian Marié
@@ -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"