openstruct-from_hash.rb 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +1 -0
- data/.pryrc +1 -0
- data/LICENSE.txt +23 -0
- data/README.md +35 -0
- data/Rakefile +5 -0
- data/lib/openstruct-from_hash.rb +28 -0
- data/openstruct-from_hash.gemspec +11 -0
- data/test/openstruct-from_hash_test.rb +17 -0
- metadata +53 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 36dcd12474ab1874aaae0c5fa7c1ba103ab9b8986f9bff05d2fda141e669a4c9
|
4
|
+
data.tar.gz: 0ec83879880fdaf82e8e061869581e05f4349c71ac020719cb1cff4516da1736
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4dbb060625aeb9403163507ad1f37c8ae23f4ca823add8939cf73e6a9af12a58902c48b82174050a92b5da595504ce3b5e85fc0b6eec95538c9bc364b09feb5b
|
7
|
+
data.tar.gz: d62fd0ed82bc491147edc22252fefafb2b17ef0e40a5141c4aff420f45c6e72b481bdaa084dedc94adba8847ddf65a1ffae4b267990cfb3d37417d2f8a9ec56b
|
data/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
*.gem
|
data/.pryrc
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require './lib/openstruct-from_hash'
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright 2017
|
4
|
+
1xAB Software
|
5
|
+
https://gitlab.com/1xAB
|
6
|
+
|
7
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
8
|
+
of this software and associated documentation files (the "Software"), to deal
|
9
|
+
in the Software without restriction, including without limitation the rights
|
10
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
11
|
+
copies of the Software, and to permit persons to whom the Software is
|
12
|
+
furnished to do so, subject to the following conditions:
|
13
|
+
|
14
|
+
The above copyright notice and this permission notice shall be included in all
|
15
|
+
copies or substantial portions of the Software.
|
16
|
+
|
17
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
18
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
19
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
20
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
21
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
22
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
23
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
# openstruct-from_hash.rb
|
2
|
+
|
3
|
+
* <a href='#introduction'>Introduction</a>
|
4
|
+
* <a href='#examples'>Examples</a>
|
5
|
+
* <a href='#install'>Install</a>
|
6
|
+
* <a href='#license'>License</a>
|
7
|
+
|
8
|
+
## <a id='introduction'>Introduction</a>
|
9
|
+
|
10
|
+
openstruct-from_hash.rb implements `OpenStruct.from_hash()`, a recursive
|
11
|
+
constructor for the OpenStruct class that is apart of Ruby's standard library.
|
12
|
+
|
13
|
+
## <a id='examples'>Examples</a>
|
14
|
+
|
15
|
+
|
16
|
+
```ruby
|
17
|
+
require 'ostruct'
|
18
|
+
require 'openstruct-from_hash'
|
19
|
+
obj = OpenStruct.from_hash(str: 'foo',
|
20
|
+
ary: [{number: 20}],
|
21
|
+
_hash: {float: 10.5}
|
22
|
+
)
|
23
|
+
obj.str # => 'foo'
|
24
|
+
obj.ary[0].number # => 20
|
25
|
+
obj._hash.float # => 10.5
|
26
|
+
```
|
27
|
+
|
28
|
+
## <a id='install'>Install</a>
|
29
|
+
|
30
|
+
gem install openstruct-from_hash.rb
|
31
|
+
|
32
|
+
|
33
|
+
## <a id='license'>License</a>
|
34
|
+
|
35
|
+
This project uses the MIT license, see [./LICENSE.txt](LICENSE.txt) for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,28 @@
|
|
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
|
25
|
+
end
|
26
|
+
OpenStruct.new(visited_object)
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
Gem::Specification.new do |spec|
|
2
|
+
spec.name = 'openstruct-from_hash.rb'
|
3
|
+
spec.version = '0.2.0'
|
4
|
+
spec.authors = ['Robert Gleeson']
|
5
|
+
spec.email = 'trebor.g@protonmail.com'
|
6
|
+
spec.files = `git ls-files`.each_line.map(&:chomp)
|
7
|
+
spec.summary = 'openstruct-from_hash.rb implements OpenStruct.from_hash(), a recursive constructor for the OpenStruct class that is apart of Ruby\'s standard library.'
|
8
|
+
spec.description = spec.summary
|
9
|
+
spec.homepage = 'https://github.com/r-obert/openstruct-from_hash.rb'
|
10
|
+
spec.licenses = ['MIT']
|
11
|
+
end
|
@@ -0,0 +1,17 @@
|
|
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(str: 'foo',
|
10
|
+
ary: [{ number: 20 }, BasicObject],
|
11
|
+
_hash: { float: 10.5 })
|
12
|
+
assert_equal 'foo', obj.str
|
13
|
+
assert_equal 20, obj.ary[0].number
|
14
|
+
assert BasicObject === obj.ary[1]
|
15
|
+
assert_equal 10.5, obj._hash.float
|
16
|
+
end
|
17
|
+
end
|
metadata
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: openstruct-from_hash.rb
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Robert Gleeson
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-12-01 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: openstruct-from_hash.rb implements OpenStruct.from_hash(), a recursive
|
14
|
+
constructor for the OpenStruct class that is apart of Ruby's standard library.
|
15
|
+
email: trebor.g@protonmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- ".gitignore"
|
21
|
+
- ".pryrc"
|
22
|
+
- LICENSE.txt
|
23
|
+
- README.md
|
24
|
+
- Rakefile
|
25
|
+
- lib/openstruct-from_hash.rb
|
26
|
+
- openstruct-from_hash.gemspec
|
27
|
+
- test/openstruct-from_hash_test.rb
|
28
|
+
homepage: https://github.com/r-obert/openstruct-from_hash.rb
|
29
|
+
licenses:
|
30
|
+
- MIT
|
31
|
+
metadata: {}
|
32
|
+
post_install_message:
|
33
|
+
rdoc_options: []
|
34
|
+
require_paths:
|
35
|
+
- lib
|
36
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
requirements: []
|
47
|
+
rubyforge_project:
|
48
|
+
rubygems_version: 2.7.7
|
49
|
+
signing_key:
|
50
|
+
specification_version: 4
|
51
|
+
summary: openstruct-from_hash.rb implements OpenStruct.from_hash(), a recursive constructor
|
52
|
+
for the OpenStruct class that is apart of Ruby's standard library.
|
53
|
+
test_files: []
|