openstruct-from_hash.rb 0.3.2 → 0.3.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/CHANGELOG.md +4 -0
- data/Gemfile +5 -0
- data/README.md +20 -2
- data/Rakefile +2 -1
- data/lib/openstruct-from_hash.rb +1 -1
- data/spec/openstruct-from_hash_spec.rb +24 -0
- metadata +4 -3
- data/test/openstruct-from_hash_test.rb +0 -20
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: acab5b4bc8644f21c1240b529886644a54b70c6922a13e09b4c73d21901791e9
|
4
|
+
data.tar.gz: e15db4781da6d1bb2cbbd05ff872e1b703fd6c072604b6bda7a4504f08997fba
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8ad00f9a336affbea429f99ad62d2a2752ea0cc8c4d2640965cc14836d17efd133460a8ae79e28f2011d4ba161bc401c55ff9cdca0bdaadae1146b168d4c5647
|
7
|
+
data.tar.gz: 550e0229a30ecdb801ab70b68326224a8051520ef241c8453ba5bc4ebb80bf0c7b0be1dc82f60092bcf127935f066a5baebc96ec1e4a21b2e385e727a6099018
|
data/.gitignore
CHANGED
data/CHANGELOG.md
CHANGED
data/Gemfile
ADDED
data/README.md
CHANGED
@@ -13,18 +13,36 @@ constructor for the OpenStruct class that is apart of Ruby's standard library.
|
|
13
13
|
## <a id='examples'>Examples</a>
|
14
14
|
|
15
15
|
|
16
|
+
__1.__
|
17
|
+
|
18
|
+
Recursively initialize an OpenStruct:
|
19
|
+
|
16
20
|
```ruby
|
17
21
|
require 'ostruct'
|
18
22
|
require 'openstruct-from_hash'
|
19
23
|
obj = OpenStruct.from_hash(
|
20
24
|
str: 'foo',
|
21
25
|
ary: [{ number: 20 }],
|
22
|
-
person: { name: 'John',
|
26
|
+
person: { name: 'John', friends: [{name: 'Amy'}] }
|
23
27
|
)
|
24
28
|
obj.str # => 'foo'
|
25
29
|
obj.ary[0].number # => 20
|
26
30
|
obj.person.name # => 'John'
|
27
|
-
obj.person.
|
31
|
+
obj.person.friends[0].name # => 'Amy'
|
32
|
+
```
|
33
|
+
|
34
|
+
__2.__
|
35
|
+
|
36
|
+
Recursively initialize an OpenStruct from a JSON object:
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
require 'json'
|
40
|
+
require 'ostruct'
|
41
|
+
require 'openstruct-from_hash'
|
42
|
+
json = '{ "ary": [{ "number": 20 }], "person": { "name": "John", "friends": [{ "name": "Amy" }] } }'
|
43
|
+
obj = OpenStruct.from_hash JSON.parse(json)
|
44
|
+
obj.ary[0].number # => 20
|
45
|
+
obj.person.name # => 'John'
|
28
46
|
obj.person.friends[0].name # => 'Amy'
|
29
47
|
```
|
30
48
|
|
data/Rakefile
CHANGED
data/lib/openstruct-from_hash.rb
CHANGED
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'bundler/setup'
|
2
|
+
require 'rspec'
|
3
|
+
require 'ostruct'
|
4
|
+
require 'openstruct-from_hash'
|
5
|
+
|
6
|
+
RSpec.describe OpenStruct::FromHash do
|
7
|
+
let(:ostruct) do
|
8
|
+
OpenStruct.from_hash(
|
9
|
+
str: 'foo',
|
10
|
+
ary: [{ number: 20 }, BasicObject.new],
|
11
|
+
person: { name: 'John', friends: [{ name: 'Amy' }] }
|
12
|
+
)
|
13
|
+
end
|
14
|
+
|
15
|
+
describe '#from_hash' do
|
16
|
+
it 'recursively initializes an OpenStruct' do
|
17
|
+
expect(ostruct.str).to eq('foo')
|
18
|
+
expect(ostruct.ary[0].number).to eq(20)
|
19
|
+
expect(BasicObject === ostruct.ary[1]).to be(true)
|
20
|
+
expect(ostruct.person.name).to eq('John')
|
21
|
+
expect(ostruct.person.friends[0].name).to eq('Amy')
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: openstruct-from_hash.rb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Robert Gleeson
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-12-
|
11
|
+
date: 2018-12-02 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: openstruct-from_hash.rb implements OpenStruct.from_hash(), a recursive
|
14
14
|
constructor for the OpenStruct class that is apart of Ruby's standard library.
|
@@ -20,12 +20,13 @@ files:
|
|
20
20
|
- ".gitignore"
|
21
21
|
- ".pryrc"
|
22
22
|
- CHANGELOG.md
|
23
|
+
- Gemfile
|
23
24
|
- LICENSE.txt
|
24
25
|
- README.md
|
25
26
|
- Rakefile
|
26
27
|
- lib/openstruct-from_hash.rb
|
27
28
|
- openstruct-from_hash.gemspec
|
28
|
-
-
|
29
|
+
- spec/openstruct-from_hash_spec.rb
|
29
30
|
homepage: https://github.com/r-obert/openstruct-from_hash.rb
|
30
31
|
licenses:
|
31
32
|
- MIT
|
@@ -1,20 +0,0 @@
|
|
1
|
-
require 'bundler/setup'
|
2
|
-
require 'ostruct'
|
3
|
-
require 'openstruct-from_hash'
|
4
|
-
require 'minitest'
|
5
|
-
require 'minitest/autorun'
|
6
|
-
|
7
|
-
class OpenStructTest < Minitest::Test
|
8
|
-
def test_from_hash_1
|
9
|
-
obj = OpenStruct.from_hash(
|
10
|
-
str: 'foo',
|
11
|
-
ary: [{ number: 20 }, BasicObject.new],
|
12
|
-
person: { name: 'John', friends: [{ name: 'Amy' }] }
|
13
|
-
)
|
14
|
-
assert_equal 'foo', obj.str
|
15
|
-
assert_equal 20, obj.ary[0].number
|
16
|
-
assert BasicObject === obj.ary[1]
|
17
|
-
assert_equal 'John', obj.person.name
|
18
|
-
assert_equal 'Amy', obj.person.friends[0].name
|
19
|
-
end
|
20
|
-
end
|