openstruct-from_hash.rb 0.2.1 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/openstruct-from_hash.rb +29 -24
- data/openstruct-from_hash.gemspec +2 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0105c584683b609b38620d26b039961b64633e57262a1deac1531e84b82c12c1
|
4
|
+
data.tar.gz: f48a224c1ba643e9aa17030ae9eb13c413503bc2983c64a0c9a88edabfaf7d10
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 10f753dc7d2c7b0b8ca2e6a88f28d858709abf78571bf72f02e1c7994714b6626d6ae2bbfe3b5ebf1b32cbedd8c4a646e4db40c0661ae52e1e66855e99d9960b
|
7
|
+
data.tar.gz: a97ccf8df386b4ddbdf3a74db61db06a31e4cfe7ea4d01b759fd5e056b920658a0fc585a28061fee600511d7023f2b926f625e0eb7792b534f45059745123d21
|
data/lib/openstruct-from_hash.rb
CHANGED
@@ -1,28 +1,33 @@
|
|
1
1
|
class OpenStruct
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
#
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
value
|
24
|
-
|
2
|
+
module FromHash
|
3
|
+
VERSION = '0.3.0'.freeze
|
4
|
+
|
5
|
+
#
|
6
|
+
# @example
|
7
|
+
# obj = OpenStruct.from_hash(person: {name: 'John'})
|
8
|
+
# obj.person.name # => 'John'
|
9
|
+
# obj.person.class # => OpenStruct
|
10
|
+
#
|
11
|
+
# @param [Hash] hash_obj
|
12
|
+
# A Hash object.
|
13
|
+
#
|
14
|
+
# @return [OpenStruct]
|
15
|
+
# An OpenStruct object initialized by visiting `hash_obj` with
|
16
|
+
# recursion.
|
17
|
+
#
|
18
|
+
def from_hash(hash_obj)
|
19
|
+
visited_object = {}
|
20
|
+
hash_obj.each do |key, value|
|
21
|
+
visited_object[key] = if Hash === value
|
22
|
+
from_hash(value)
|
23
|
+
elsif Array === value
|
24
|
+
value.map { |v| Hash === v ? from_hash(v) : v }
|
25
|
+
else
|
26
|
+
value
|
27
|
+
end
|
28
|
+
end
|
29
|
+
OpenStruct.new(visited_object)
|
25
30
|
end
|
26
|
-
OpenStruct.new(visited_object)
|
27
31
|
end
|
32
|
+
extend FromHash
|
28
33
|
end
|
@@ -1,6 +1,7 @@
|
|
1
|
+
require './lib/openstruct-from_hash'
|
1
2
|
Gem::Specification.new do |spec|
|
2
3
|
spec.name = 'openstruct-from_hash.rb'
|
3
|
-
spec.version =
|
4
|
+
spec.version = OpenStruct::FromHash::VERSION
|
4
5
|
spec.authors = ['Robert Gleeson']
|
5
6
|
spec.email = 'trebor.g@protonmail.com'
|
6
7
|
spec.files = `git ls-files`.each_line.map(&:chomp)
|