hash_wrap 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +15 -0
- data/.rspec +3 -0
- data/.travis.yml +6 -0
- data/Gemfile +8 -0
- data/LICENSE.txt +21 -0
- data/README.md +73 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/hash_wrap.gemspec +27 -0
- data/lib/hash_wrap.rb +96 -0
- data/lib/hash_wrap/json.rb +27 -0
- data/lib/hash_wrap/version.rb +3 -0
- data/lib/hash_wrap/yaml.rb +27 -0
- metadata +61 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 6a6ac96de266fa0f1fbab1d7b481d88fd20abde3985a3b941f47dadb56d026c7
|
4
|
+
data.tar.gz: 6ee88096a74780406146c3bee3bd28852dda73e0b2e9ae37d37228e97bcbd956
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c7b6fd145b64454ad279829625c2f4de9432b4e39ed3e10e736f5d4b051cde115fc0465cf5b60d543cb6ed95484ecd87912962f7f50ad5ef88fb77d5602db73b
|
7
|
+
data.tar.gz: c4c25b5e3a4162a5e72cbd0bc11ee7524f250b459f839d4431762046e8825f3b8f80039579293367bc7b503b7ba25da644374d102d7870373ca7e50ed7ab698a
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2020 John Anderson
|
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,73 @@
|
|
1
|
+
# HashWrap
|
2
|
+
|
3
|
+
Normal usage:
|
4
|
+
|
5
|
+
```ruby
|
6
|
+
require 'hashwrap'
|
7
|
+
|
8
|
+
hw = HashWrap one: 1, due: 2, tre: 3, kvr: 4, vif: {six: 6, svn: 7, oct: {non: 9}}
|
9
|
+
|
10
|
+
> hw.one
|
11
|
+
=> 1
|
12
|
+
|
13
|
+
> hw.due
|
14
|
+
=> 2
|
15
|
+
|
16
|
+
> hw.vif.oct.non
|
17
|
+
=> 9
|
18
|
+
```
|
19
|
+
|
20
|
+
These are conveniences that will *override* the usual Kernel.HashWrap method. You have been warned.
|
21
|
+
|
22
|
+
```ruby
|
23
|
+
require 'hash_wrap/json'
|
24
|
+
|
25
|
+
hw = HashWrap %q|{"one":1,"due":2,"tre":3,"kvr":4}|
|
26
|
+
```
|
27
|
+
|
28
|
+
```ruby
|
29
|
+
require 'hash_wrap/yaml'
|
30
|
+
|
31
|
+
hw = HashWrap <<YAML
|
32
|
+
---
|
33
|
+
:one: 1
|
34
|
+
:due: 2
|
35
|
+
:tre: 3
|
36
|
+
:kvr: 4
|
37
|
+
YAML
|
38
|
+
```
|
39
|
+
|
40
|
+
## Installation
|
41
|
+
|
42
|
+
Add this line to your application's Gemfile:
|
43
|
+
|
44
|
+
```ruby
|
45
|
+
gem 'hash_wrap'
|
46
|
+
```
|
47
|
+
|
48
|
+
And then execute:
|
49
|
+
|
50
|
+
$ bundle install
|
51
|
+
|
52
|
+
Or install it yourself as:
|
53
|
+
|
54
|
+
$ gem install hash_wrap
|
55
|
+
|
56
|
+
## Usage
|
57
|
+
|
58
|
+
TODO: Write usage instructions here
|
59
|
+
|
60
|
+
## Development
|
61
|
+
|
62
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
63
|
+
|
64
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
65
|
+
|
66
|
+
## Contributing
|
67
|
+
|
68
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/hash_wrap.
|
69
|
+
|
70
|
+
|
71
|
+
## License
|
72
|
+
|
73
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "hash_wrap"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/hash_wrap.gemspec
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
require_relative 'lib/hash_wrap/version'
|
2
|
+
|
3
|
+
Gem::Specification.new do |spec|
|
4
|
+
spec.name = "hash_wrap"
|
5
|
+
spec.version = HashWrap::VERSION
|
6
|
+
spec.authors = ["John Anderson"]
|
7
|
+
spec.email = ["panic@semiosix.com"]
|
8
|
+
|
9
|
+
spec.summary = %q{A quick-n-dirty way to deep-wrap HoHA objects.}
|
10
|
+
spec.description = %q{HoHA being Hash of Hashes and Arrays. If it doesn't work for you, rather than scope-creeping this consider: Hash, OpenStruct, a class, etc.}
|
11
|
+
spec.homepage = 'https://github.com/djellemah/hash_wrap'
|
12
|
+
spec.license = 'MIT'
|
13
|
+
spec.required_ruby_version = Gem::Requirement.new ">= 2.7.0"
|
14
|
+
|
15
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
16
|
+
spec.metadata["source_code_uri"] = 'https://github.com/djellemah/hash_wrap'
|
17
|
+
spec.metadata["changelog_uri"] = 'https://github.com/djellemah/hash_wrap/ChangeLog'
|
18
|
+
|
19
|
+
# Specify which files should be added to the gem when it is released.
|
20
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
21
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
22
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
23
|
+
end
|
24
|
+
spec.bindir = "exe"
|
25
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
26
|
+
spec.require_paths = ["lib"]
|
27
|
+
end
|
data/lib/hash_wrap.rb
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
require "hash_wrap/version"
|
2
|
+
|
3
|
+
class HashWrap
|
4
|
+
def initialize( hash )
|
5
|
+
@hash = case hash
|
6
|
+
when Hash
|
7
|
+
hash.transform_keys(&:to_sym).freeze
|
8
|
+
when HashWrap
|
9
|
+
hash
|
10
|
+
else
|
11
|
+
raise "dunno how to wrap #{hash.inspect}"
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def method_missing(meth, *args, &blk)
|
16
|
+
# handle both string and symbol keys
|
17
|
+
value = @hash.fetch meth.to_s do |_key|
|
18
|
+
@hash.fetch meth do |_key|
|
19
|
+
super
|
20
|
+
end
|
21
|
+
end
|
22
|
+
__wrap value
|
23
|
+
end
|
24
|
+
|
25
|
+
private def __wrap value
|
26
|
+
case value
|
27
|
+
when Hash
|
28
|
+
self.class.new value
|
29
|
+
when Array
|
30
|
+
value.map do |item|
|
31
|
+
__wrap item
|
32
|
+
end
|
33
|
+
else
|
34
|
+
value
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
private def __hash__; @hash end
|
39
|
+
|
40
|
+
def == rhs
|
41
|
+
@hash == __hash__
|
42
|
+
end
|
43
|
+
|
44
|
+
def each &blk
|
45
|
+
return enum_for :each unless block_given?
|
46
|
+
@hash.each do |k,v|
|
47
|
+
yield [k,__wrap(v)]
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def to_h
|
52
|
+
# no, you can't have a copy of this hash to mess with
|
53
|
+
@hash.dup
|
54
|
+
end
|
55
|
+
|
56
|
+
def dig *parts
|
57
|
+
@hash.dig *parts
|
58
|
+
end
|
59
|
+
|
60
|
+
def deconstruct
|
61
|
+
raise NotImplementedError
|
62
|
+
end
|
63
|
+
|
64
|
+
def deconstruct_keys keys
|
65
|
+
if keys.nil? || keys.empty?
|
66
|
+
@hash
|
67
|
+
else
|
68
|
+
@hash.slice(*keys).map{|k,v| [k, __wrap(v)]}.to_h
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
def to_s
|
73
|
+
@hash.to_s
|
74
|
+
end
|
75
|
+
|
76
|
+
def inspect
|
77
|
+
@hash.inspect
|
78
|
+
end
|
79
|
+
|
80
|
+
# have to handle this otherwise pry gets upset
|
81
|
+
def pretty_print(pp)
|
82
|
+
pp.text inspect
|
83
|
+
end
|
84
|
+
|
85
|
+
# for awesome_print
|
86
|
+
def ai; inspect end
|
87
|
+
end
|
88
|
+
|
89
|
+
module Kernel
|
90
|
+
private def HashWrap arg
|
91
|
+
case arg
|
92
|
+
when Hash; HashWrap.new arg
|
93
|
+
else; HashWrap.new arg.to_h
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require_relative '../hash_wrap.rb'
|
2
|
+
|
3
|
+
class HashWrap
|
4
|
+
# really a convenience method
|
5
|
+
def self.of_json json
|
6
|
+
vs = JSON.parse json, symbolize_names: true
|
7
|
+
case vs
|
8
|
+
when Array
|
9
|
+
vs.map{|obj| new obj}
|
10
|
+
when Hash
|
11
|
+
new vs
|
12
|
+
else
|
13
|
+
raise "Cannot construct #{self.class} from json in #{json.inspect}"
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
# The String part of this can be overwritten by other requires in this gem, so
|
19
|
+
# use with caution.
|
20
|
+
module Kernel
|
21
|
+
private def HashWrap arg
|
22
|
+
case arg
|
23
|
+
when String; HashWrap.of_json arg
|
24
|
+
when Hash; HashWrap.new arg
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require_relative '../hash_wrap.rb'
|
2
|
+
|
3
|
+
class HashWrap
|
4
|
+
# really a convenience method
|
5
|
+
def self.of_yaml yaml
|
6
|
+
vs = YAML.load yaml
|
7
|
+
case vs
|
8
|
+
when Array
|
9
|
+
vs.map{|obj| new obj}
|
10
|
+
when Hash
|
11
|
+
new vs
|
12
|
+
else
|
13
|
+
raise "Cannot construct #{self.class} from yaml in #{yaml.inspect}"
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
# The String part of this can be overwritten by other requires in this gem, so
|
19
|
+
# use with caution.
|
20
|
+
module Kernel
|
21
|
+
private def HashWrap arg
|
22
|
+
case arg
|
23
|
+
when String; HashWrap.of_yaml arg
|
24
|
+
when Hash; HashWrap.new arg
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
metadata
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hash_wrap
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.3.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- John Anderson
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2021-01-21 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: 'HoHA being Hash of Hashes and Arrays. If it doesn''t work for you, rather
|
14
|
+
than scope-creeping this consider: Hash, OpenStruct, a class, etc.'
|
15
|
+
email:
|
16
|
+
- panic@semiosix.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- ".gitignore"
|
22
|
+
- ".rspec"
|
23
|
+
- ".travis.yml"
|
24
|
+
- Gemfile
|
25
|
+
- LICENSE.txt
|
26
|
+
- README.md
|
27
|
+
- Rakefile
|
28
|
+
- bin/console
|
29
|
+
- bin/setup
|
30
|
+
- hash_wrap.gemspec
|
31
|
+
- lib/hash_wrap.rb
|
32
|
+
- lib/hash_wrap/json.rb
|
33
|
+
- lib/hash_wrap/version.rb
|
34
|
+
- lib/hash_wrap/yaml.rb
|
35
|
+
homepage: https://github.com/djellemah/hash_wrap
|
36
|
+
licenses:
|
37
|
+
- MIT
|
38
|
+
metadata:
|
39
|
+
homepage_uri: https://github.com/djellemah/hash_wrap
|
40
|
+
source_code_uri: https://github.com/djellemah/hash_wrap
|
41
|
+
changelog_uri: https://github.com/djellemah/hash_wrap/ChangeLog
|
42
|
+
post_install_message:
|
43
|
+
rdoc_options: []
|
44
|
+
require_paths:
|
45
|
+
- lib
|
46
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
47
|
+
requirements:
|
48
|
+
- - ">="
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: 2.7.0
|
51
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
56
|
+
requirements: []
|
57
|
+
rubygems_version: 3.2.3
|
58
|
+
signing_key:
|
59
|
+
specification_version: 4
|
60
|
+
summary: A quick-n-dirty way to deep-wrap HoHA objects.
|
61
|
+
test_files: []
|