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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7ecbbbc34455b4034bacf91a42917e6cb43e93f181c89ba71c496b6b960841bf
4
- data.tar.gz: e24d52488b0de1a58eaa23f767c30b0964eea331cadcb2e9495c7e3f0e38fdda
3
+ metadata.gz: 0105c584683b609b38620d26b039961b64633e57262a1deac1531e84b82c12c1
4
+ data.tar.gz: f48a224c1ba643e9aa17030ae9eb13c413503bc2983c64a0c9a88edabfaf7d10
5
5
  SHA512:
6
- metadata.gz: 2a9c91b2dd8563bf288a148e216c044790b4cb2fbe91e272d48e4b9c2d07be37ae1c1e12a9dbe0ea79a25bc92fcdb89595d021fd8057933acef426a6acad0375
7
- data.tar.gz: b2918d0434e3c0c4bece874effcf324c2f70fcf0ff55fe5dd717b436a48f8ada9e6998da60668ac4ac5b39164aaa3c4c0b17e5234bacb4c416c0bc63a21cbc4d
6
+ metadata.gz: 10f753dc7d2c7b0b8ca2e6a88f28d858709abf78571bf72f02e1c7994714b6626d6ae2bbfe3b5ebf1b32cbedd8c4a646e4db40c0661ae52e1e66855e99d9960b
7
+ data.tar.gz: a97ccf8df386b4ddbdf3a74db61db06a31e4cfe7ea4d01b759fd5e056b920658a0fc585a28061fee600511d7023f2b926f625e0eb7792b534f45059745123d21
@@ -1,28 +1,33 @@
1
1
  class OpenStruct
2
- #
3
- # @example
4
- # obj = OpenStruct.from_hash(person: {name: 'John'})
5
- # obj.person.name # => 'John'
6
- # obj.person.class # => OpenStruct
7
- #
8
- # @param [Hash] hash_obj
9
- # A Hash object.
10
- #
11
- # @return [OpenStruct]
12
- # An OpenStruct object initialized by visiting `hash_obj` with
13
- # recursion.
14
- #
15
- def self.from_hash(hash_obj)
16
- visited_object = {}
17
- hash_obj.each do |key, value|
18
- visited_object[key] = if Hash === value
19
- from_hash(value)
20
- elsif Array === value
21
- value.map { |v| Hash === v ? from_hash(v) : v }
22
- else
23
- value
24
- end
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 = '0.2.1'
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)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: openstruct-from_hash.rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robert Gleeson