dorian-to_struct 0.5.1 → 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 +3 -17
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b1b7fed4587dddb8df16482d9d812ae7656a0632bcbe0e6b04f71c781b391be7
4
- data.tar.gz: 18d380b30f9cdf56be110ea61a2d0cc734ff283f05201a90c26ad12e738900ed
3
+ metadata.gz: 1df7e372e064769577c0f935aba5a031c018cd20773d0179fd235d6389f42b03
4
+ data.tar.gz: a36461a79045aadc2d2db55cd8ddf865fee2084e6394a40638622397b6c6140b
5
5
  SHA512:
6
- metadata.gz: 3555827f5e2c1b8f51b8ffd3ad453c18614a1d1a67ed9f38db7ad52fe39b8b2392199fa865b2277ff15d4fc2fd239b32e001f8fb9289bc93b1e0dca9f13fa55a
7
- data.tar.gz: 064bd8a1eff196e75f163862fa3c7e1cceb758eda8bc9c183929cc43cd3455a4eb7a5e5463d638459bd2958f6f2b1a0b3bb3a9a5c3e8eadf255f5faf288ca8cd
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,29 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dorian-to_struct
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.1
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
12
- dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: dorian-arguments
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - ">="
18
- - !ruby/object:Gem::Version
19
- version: '0'
20
- type: :runtime
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - ">="
25
- - !ruby/object:Gem::Version
26
- version: '0'
11
+ date: 2024-08-24 00:00:00.000000000 Z
12
+ dependencies: []
27
13
  description: makes structs
28
14
  email: dorian@dorianmarie.com
29
15
  executables: []