dorian-to_struct 0.4.2 → 0.5.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: f4b1a2c571b4d37a72ee55b33edd7984afdeec58e907cb854b6efd3ac6c44ea2
4
- data.tar.gz: 764f762e68f4b2f98e73a9e087a76958874143e8e1ae9db429d929fcfdad24c4
3
+ metadata.gz: 107b93efe81a4c2a06696885a495af7b232e4e894afe4ed019fce897008ecb72
4
+ data.tar.gz: e8ef801dc9f6edb7fac6dc1def40376e9699b8f3bab9e6811e862b13e9d67702
5
5
  SHA512:
6
- metadata.gz: ed1019db245750a950f3f69f291deae3fe26b1d45b9287dd1f35dd110ca6c5dbefe6ace1ce6f786eaf3116a2df2a07a4758066de532599617c8339b3d55c73f3
7
- data.tar.gz: 49e77fc65a0fa57c17d3b3cad31a4ff5051921dace0d71d10baf2699074e29b5dcf313d323a4ffc94f446f6744b2915bdc99873bf98fd3464ee73ebdeb10d226
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
@@ -1,49 +1,3 @@
1
1
  # frozen_string_literal: true
2
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
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,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.4.2
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dorian Marié
@@ -17,6 +17,7 @@ extensions: []
17
17
  extra_rdoc_files: []
18
18
  files:
19
19
  - lib/dorian-to_struct.rb
20
+ - lib/dorian/to_struct.rb
20
21
  homepage: https://github.com/dorianmariecom/dorian-to_struct
21
22
  licenses:
22
23
  - MIT