hash_to_struct 0.1.0
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 +7 -0
- data/.gitignore +13 -0
- data/.rspec +3 -0
- data/CHANGELOG.md +3 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +83 -0
- data/Rakefile +6 -0
- data/hash_to_struct.gemspec +31 -0
- data/lib/hash_to_struct/open_struct_builder.rb +15 -0
- data/lib/hash_to_struct/struct.rb +54 -0
- data/lib/hash_to_struct/struct_builder.rb +14 -0
- data/lib/hash_to_struct/version.rb +3 -0
- data/lib/hash_to_struct.rb +20 -0
- metadata +104 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 8606f1e55e6fbabe1ccb0d02f70688025b87aed05490ba221896605c92d31402
|
4
|
+
data.tar.gz: 5d51c847d87d7c96dfd8938301c68effa74e51bc450094447c35b8beddab8096
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 61ffa8d5725f6552e0b35174d69d9e566693c736c1e695b682643613ca3a0a0b8c4976f2d54e54c043c42a9e0993855f2155aff7a077e6f05ac99f051e72d2dc
|
7
|
+
data.tar.gz: 9c8bc999bc6e71ca6b76ccdf0f0093aaf75301a89507b1da41f10e4216dadae9e32eb6c23113848888af25741457ff4eac2d50fb0abaa242a192017054c89342
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2022 Andrew Bohush
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
# HashToStruct
|
2
|
+
|
3
|
+
It enables recursive conversion of ruby Hash to Struct-like object and back.
|
4
|
+
|
5
|
+
Stop thinking about symbols vs strings when accessing values in your hash. Simply call a method.
|
6
|
+
|
7
|
+
It is built on top of standard `Struct` and `OpenStruct` classes with all their features preserved while adding a few on top for conveniences like handling nested hashes/arrays, unified constructor interface, immutability.
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
gem 'hash_to_struct'
|
15
|
+
```
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
$ bundle install
|
20
|
+
|
21
|
+
Or install it yourself as:
|
22
|
+
|
23
|
+
$ gem install hash_to_struct
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
`Struct` based conversion:
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
struct = HashToStruct.struct({q: 1})
|
31
|
+
|
32
|
+
struct.q # => 1
|
33
|
+
struct[:x] # => NameError: no member 'x' in struct
|
34
|
+
```
|
35
|
+
|
36
|
+
`OpenStruct` based conversion:
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
ostruct = HashToStruct.ostruct({q: 1})
|
40
|
+
|
41
|
+
ostruct.q # => 1
|
42
|
+
ostruct[:x] = 5
|
43
|
+
ostruct.x # => 5
|
44
|
+
```
|
45
|
+
|
46
|
+
Convert back to `Hash`:
|
47
|
+
|
48
|
+
```ruby
|
49
|
+
ostruct.to_h # => { :q => 1, :x => 5 }
|
50
|
+
```
|
51
|
+
|
52
|
+
Recursive conversion:
|
53
|
+
|
54
|
+
```ruby
|
55
|
+
struct = HashToStruct.struct({q: 1, w: { e: 2 }})
|
56
|
+
|
57
|
+
struct.q.w.e # => 2
|
58
|
+
```
|
59
|
+
**Note!** It will only convert objects of type Hash, not Hash derivatives or some acting like Hash.
|
60
|
+
|
61
|
+
Recursive conversions inside `Array`:
|
62
|
+
|
63
|
+
```ruby
|
64
|
+
struct = HashToStruct.struct({q: 1, w: [{ e: 2 }]}, including_arrays: true)
|
65
|
+
|
66
|
+
struct.q.w.first.e # => 2
|
67
|
+
```
|
68
|
+
**Note!** It will only iterate over objects of type Array, not Array derivatives or other enumerables.
|
69
|
+
|
70
|
+
Make immutable:
|
71
|
+
```ruby
|
72
|
+
struct = HashToStruct.struct({q: 1}, immutable: true)
|
73
|
+
|
74
|
+
struct.q = 2 # => FrozenError: can't modify frozen #<struct q=1>
|
75
|
+
```
|
76
|
+
|
77
|
+
## Contributing
|
78
|
+
|
79
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/a-bohush/hash_to_struct.
|
80
|
+
|
81
|
+
## License
|
82
|
+
|
83
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
require_relative 'lib/hash_to_struct/version'
|
2
|
+
|
3
|
+
Gem::Specification.new do |spec|
|
4
|
+
spec.name = "hash_to_struct"
|
5
|
+
spec.version = HashToStruct::VERSION
|
6
|
+
spec.authors = ["Andrew Bohush"]
|
7
|
+
spec.email = ["a.bohush01@gmail.com"]
|
8
|
+
|
9
|
+
spec.summary = "It enables recursive conversion of ruby Hash to Struct-like object and back."
|
10
|
+
spec.description = "It is built on top of standard `Struct` and `OpenStruct` classes with all their features preserved while adding a few on top for conveniences like handling nested hashes/arrays, unified constructor interface, immutability."
|
11
|
+
spec.homepage = "https://github.com/a-bohush/hash_to_struct.git"
|
12
|
+
spec.license = "MIT"
|
13
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
|
14
|
+
|
15
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
16
|
+
spec.metadata["source_code_uri"] = spec.homepage
|
17
|
+
spec.metadata["changelog_uri"] = "https://github.com/a-bohush/hash_to_struct/blob/main/CHANGELOG.md"
|
18
|
+
spec.extra_rdoc_files = ['README.md']
|
19
|
+
|
20
|
+
# Specify which files should be added to the gem when it is released.
|
21
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
22
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
23
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
24
|
+
end
|
25
|
+
|
26
|
+
# spec.require_paths = ["lib"]
|
27
|
+
|
28
|
+
spec.add_development_dependency "rake", "~> 12.0"
|
29
|
+
spec.add_development_dependency "pry-byebug"
|
30
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
31
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'delegate'
|
2
|
+
|
3
|
+
module HashToStruct
|
4
|
+
class Struct < SimpleDelegator
|
5
|
+
def initialize(
|
6
|
+
hash,
|
7
|
+
builder_class: HashToStruct::StructBuilder,
|
8
|
+
immutable: false,
|
9
|
+
including_arrays: false
|
10
|
+
)
|
11
|
+
@options = {
|
12
|
+
builder_class: builder_class,
|
13
|
+
immutable: immutable,
|
14
|
+
including_arrays: including_arrays
|
15
|
+
}
|
16
|
+
__setobj__(to_struct(hash))
|
17
|
+
freeze if immutable
|
18
|
+
end
|
19
|
+
|
20
|
+
def to_h
|
21
|
+
to_hash
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def to_struct(hash)
|
27
|
+
h = hash.each_key.each_with_object({}) do |key, h|
|
28
|
+
create_new = -> (hv) { self.class.new(hv, **@options) }
|
29
|
+
h[key] = case
|
30
|
+
when hash[key].class == Hash
|
31
|
+
create_new.call(hash[key])
|
32
|
+
when @options[:including_arrays] && hash[key].class == Array
|
33
|
+
hash[key].map(&create_new)
|
34
|
+
else
|
35
|
+
hash[key]
|
36
|
+
end
|
37
|
+
end
|
38
|
+
@options[:builder_class].build(h)
|
39
|
+
end
|
40
|
+
|
41
|
+
def to_hash
|
42
|
+
__getobj__.to_h.tap do |h|
|
43
|
+
h.keys.each do |key|
|
44
|
+
h[key] = case
|
45
|
+
when h[key].is_a?(self.class) then h[key].to_h
|
46
|
+
when h[key].class == Array then h[key].map(&:to_h)
|
47
|
+
else
|
48
|
+
h[key]
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require "hash_to_struct/version"
|
2
|
+
require "hash_to_struct/struct_builder"
|
3
|
+
require "hash_to_struct/struct"
|
4
|
+
|
5
|
+
module HashToStruct
|
6
|
+
class << self
|
7
|
+
def build(h, **opts)
|
8
|
+
HashToStruct::Struct.new(h, **opts)
|
9
|
+
end
|
10
|
+
|
11
|
+
def struct(hash, **opts)
|
12
|
+
build(hash, **opts.merge({builder_class: HashToStruct::StructBuilder}))
|
13
|
+
end
|
14
|
+
|
15
|
+
def ostruct(hash, **opts)
|
16
|
+
require 'hash_to_struct/open_struct_builder'
|
17
|
+
build(hash, **opts.merge({builder_class: HashToStruct::OpenStructBuilder}))
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
metadata
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hash_to_struct
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Andrew Bohush
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2022-01-17 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rake
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '12.0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '12.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: pry-byebug
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
description: It is built on top of standard `Struct` and `OpenStruct` classes with
|
56
|
+
all their features preserved while adding a few on top for conveniences like handling
|
57
|
+
nested hashes/arrays, unified constructor interface, immutability.
|
58
|
+
email:
|
59
|
+
- a.bohush01@gmail.com
|
60
|
+
executables: []
|
61
|
+
extensions: []
|
62
|
+
extra_rdoc_files:
|
63
|
+
- README.md
|
64
|
+
files:
|
65
|
+
- ".gitignore"
|
66
|
+
- ".rspec"
|
67
|
+
- CHANGELOG.md
|
68
|
+
- Gemfile
|
69
|
+
- LICENSE.txt
|
70
|
+
- README.md
|
71
|
+
- Rakefile
|
72
|
+
- hash_to_struct.gemspec
|
73
|
+
- lib/hash_to_struct.rb
|
74
|
+
- lib/hash_to_struct/open_struct_builder.rb
|
75
|
+
- lib/hash_to_struct/struct.rb
|
76
|
+
- lib/hash_to_struct/struct_builder.rb
|
77
|
+
- lib/hash_to_struct/version.rb
|
78
|
+
homepage: https://github.com/a-bohush/hash_to_struct.git
|
79
|
+
licenses:
|
80
|
+
- MIT
|
81
|
+
metadata:
|
82
|
+
homepage_uri: https://github.com/a-bohush/hash_to_struct.git
|
83
|
+
source_code_uri: https://github.com/a-bohush/hash_to_struct.git
|
84
|
+
changelog_uri: https://github.com/a-bohush/hash_to_struct/blob/main/CHANGELOG.md
|
85
|
+
post_install_message:
|
86
|
+
rdoc_options: []
|
87
|
+
require_paths:
|
88
|
+
- lib
|
89
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
90
|
+
requirements:
|
91
|
+
- - ">="
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: 2.3.0
|
94
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
95
|
+
requirements:
|
96
|
+
- - ">="
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: '0'
|
99
|
+
requirements: []
|
100
|
+
rubygems_version: 3.1.6
|
101
|
+
signing_key:
|
102
|
+
specification_version: 4
|
103
|
+
summary: It enables recursive conversion of ruby Hash to Struct-like object and back.
|
104
|
+
test_files: []
|