dorian-to_struct 0.5.2 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/dorian/to_struct.rb +21 -31
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 327b80cca7bb92ea6e524ebb20322dfd09d7bb4cf1b68ada7409c1924cd40202
4
- data.tar.gz: cb83811568782d78214a77ae66c0865fe0120aad20bc7d94d6546c6eed18ac90
3
+ metadata.gz: 1df7e372e064769577c0f935aba5a031c018cd20773d0179fd235d6389f42b03
4
+ data.tar.gz: a36461a79045aadc2d2db55cd8ddf865fee2084e6394a40638622397b6c6140b
5
5
  SHA512:
6
- metadata.gz: 4cc12eeb2b1d7a1b0cc286303fc333016bb284cedc9642a105b6cfc2cc50d754c60ce9c89e1b6982ffb645d760b7cd9835c5ea8769b5077b32851060a83f3326
7
- data.tar.gz: 0a01a394e4c64059b9764b38d7a3b0c3068ef014d65d6a59fbeb6d685bd5eba001e2f9cb0744ffc80f2b25dbd879fffa222ee8ce97d4f0a931f17ef624a05c9e
6
+ metadata.gz: 3b2f021e76fc46741709ab82571d463edbfd3d5450dbe605e60641845419fd696f1a077e1b877ae2a6c54e34786bda3a2de58316db5c05cb1cdde87d06f4f444
7
+ data.tar.gz: 066f280876341a13d489a0f5ecc66115702c7197c8eb920091bb96f76768c8d319bcc3b500696b0899fe0953ec7feaed37de45cc7a9d9be3a8464cf5fbc72d38
@@ -1,47 +1,37 @@
1
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
2
  def to_struct
9
- map { |item| item.respond_to?(:to_struct) ? item.to_struct : item }
3
+ map(&:to_struct)
10
4
  end
11
5
 
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
6
  def to_deep_struct
18
- map do |item|
19
- item.respond_to?(:to_deep_struct) ? item.to_deep_struct : item
20
- end
7
+ map(&:to_deep_struct)
21
8
  end
22
9
  end
23
10
 
24
11
  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
12
  def to_struct
32
- Struct.new(*keys.map(&:to_sym)).new(*values)
13
+ Struct.new(
14
+ *keys.map(&:to_sym)
15
+ ).new(
16
+ *values
17
+ )
33
18
  end
34
19
 
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
20
  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
21
+ Struct.new(
22
+ *keys.map(&:to_sym)
23
+ ).new(
24
+ *values.map(&:to_deep_struct)
45
25
  )
46
26
  end
47
27
  end
28
+
29
+ class Object
30
+ def to_struct
31
+ self
32
+ end
33
+
34
+ def to_deep_struct
35
+ self
36
+ end
37
+ 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.5.2
4
+ version: 0.6.0
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-22 00:00:00.000000000 Z
11
+ date: 2024-08-24 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: makes structs
14
14
  email: dorian@dorianmarie.com