property_hash 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.github/workflows/ci.yml +21 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +62 -0
- data/Rakefile +5 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/property_hash.rb +95 -0
- data/property_hash.gemspec +34 -0
- metadata +108 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: a962a8ddb439844dcd8888cc1d6d197529768cc6212e996bdf66e50a2da84668
|
4
|
+
data.tar.gz: 6ae8c2e6d9cff7b2d37eaff146ceef0f2764dde54e4fda0dfbdc1184151431cd
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 47256ffb191c3ea8e8cd6fc9fe7176606e790019c8480c1eb3a904b14af4d83e5872ae72055cedc72d4d272bb2aef82ec4cb70270e2fd014c9aafa58c94daaaa
|
7
|
+
data.tar.gz: 3bcb799e0cef185fefdb01e0f89ca0f0a50f6c9ec948ce1644b7bbc0bf7007ce70a64ac89dd69c5fe521769244dbb8fcdd9849c97e3c49e43134e2e032cc3b40
|
@@ -0,0 +1,21 @@
|
|
1
|
+
name: CI
|
2
|
+
|
3
|
+
on:
|
4
|
+
- push
|
5
|
+
- pull_request
|
6
|
+
|
7
|
+
jobs:
|
8
|
+
test:
|
9
|
+
runs-on: ubuntu-latest
|
10
|
+
strategy:
|
11
|
+
matrix:
|
12
|
+
ruby: ['3.2', '3.1', '3.0', '2.7', '2.6', '2.5', '2.4' ]
|
13
|
+
|
14
|
+
steps:
|
15
|
+
- uses: actions/checkout@v3
|
16
|
+
- uses: ruby/setup-ruby@v1
|
17
|
+
with:
|
18
|
+
ruby-version: ${{ matrix.ruby }}
|
19
|
+
bundler-cache: true
|
20
|
+
|
21
|
+
- run: bundle exec rake
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2022 sshaw
|
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,62 @@
|
|
1
|
+
# PropertyHash
|
2
|
+
|
3
|
+
![PropertyHash CI Status](https://github.com/sshaw/property_hash/workflows/CI/badge.svg "PropertyHash CI Status")
|
4
|
+
|
5
|
+
Access a nested Ruby Hash using Java-style properties as keys.
|
6
|
+
|
7
|
+
## Usage
|
8
|
+
|
9
|
+
```rb
|
10
|
+
require "property_hash"
|
11
|
+
require "pp"
|
12
|
+
|
13
|
+
h = {
|
14
|
+
:a => {
|
15
|
+
:b => 1,
|
16
|
+
:c => { :d => "DEEEE" },
|
17
|
+
:x => 888,
|
18
|
+
:y => [9999]
|
19
|
+
},
|
20
|
+
:b => {
|
21
|
+
:zeee => 888
|
22
|
+
}
|
23
|
+
}
|
24
|
+
|
25
|
+
ph = PropertyHash.new(h)
|
26
|
+
|
27
|
+
p ph["a.c.d"] # DEEEE
|
28
|
+
p ph["b.zeee"] # 888
|
29
|
+
|
30
|
+
ph["a.c.x"] = "X"
|
31
|
+
|
32
|
+
pp ph.keys # ["a.b", "a.c.d", "a.x", "a.y", "b.zeee", "a.c.x"]
|
33
|
+
pp ph.values # [1, "DEEEE", 888, [9999], 888, "X"]
|
34
|
+
|
35
|
+
ph.each { |k, v| p k }
|
36
|
+
pp ph.to_h # h but with :a => { :c => { :x => "X" } }
|
37
|
+
```
|
38
|
+
|
39
|
+
## Installation
|
40
|
+
|
41
|
+
Add this line to your application's Gemfile:
|
42
|
+
|
43
|
+
```ruby
|
44
|
+
gem "property_hash"
|
45
|
+
```
|
46
|
+
|
47
|
+
And then execute:
|
48
|
+
|
49
|
+
bundle
|
50
|
+
|
51
|
+
Or install it yourself as:
|
52
|
+
|
53
|
+
gem install property_hash
|
54
|
+
|
55
|
+
## See Also
|
56
|
+
|
57
|
+
- [JavaProperties](https://github.com/jnbt/java-properties)
|
58
|
+
- [Localio](https://github.com/mrmans0n/localio)
|
59
|
+
|
60
|
+
## License
|
61
|
+
|
62
|
+
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 "property_hash"
|
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
@@ -0,0 +1,95 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class PropertyHash
|
4
|
+
include Enumerable
|
5
|
+
|
6
|
+
KEY_DELIMITER = "."
|
7
|
+
VERSION = "0.0.1"
|
8
|
+
|
9
|
+
def initialize(hash, use_symbols = true)
|
10
|
+
@hash = hash.dup
|
11
|
+
@keys = keys_as_properties(@hash)
|
12
|
+
@use_symbols = use_symbols || false
|
13
|
+
end
|
14
|
+
|
15
|
+
def each
|
16
|
+
return to_enum(:each) unless block_given?
|
17
|
+
@keys.each { |key| yield([key, self[key]]) }
|
18
|
+
end
|
19
|
+
|
20
|
+
def keys
|
21
|
+
@keys.dup
|
22
|
+
end
|
23
|
+
|
24
|
+
def values
|
25
|
+
map(&:last)
|
26
|
+
end
|
27
|
+
|
28
|
+
def to_h
|
29
|
+
@hash.dup
|
30
|
+
end
|
31
|
+
|
32
|
+
alias to_hash to_h
|
33
|
+
|
34
|
+
def []=(key, value)
|
35
|
+
keys = property_key(key)
|
36
|
+
hash_to_set = @hash
|
37
|
+
|
38
|
+
keys[0..-2].each do |key|
|
39
|
+
if hash_to_set.include?(key)
|
40
|
+
hash_to_set = hash_to_set[key]
|
41
|
+
else
|
42
|
+
hash_to_set = hash_to_set[key] = {}
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
@keys << key
|
47
|
+
|
48
|
+
hash_to_set[keys[-1]] = value
|
49
|
+
end
|
50
|
+
|
51
|
+
def [](key)
|
52
|
+
keys = property_key(key)
|
53
|
+
|
54
|
+
begin
|
55
|
+
# FIXME: Does not support mixed keys
|
56
|
+
v = @hash.dig(*keys)
|
57
|
+
return v unless v.nil? && @use_symbols
|
58
|
+
|
59
|
+
keys.map!(&:to_sym)
|
60
|
+
@hash.dig(*keys)
|
61
|
+
rescue TypeError
|
62
|
+
# From dig. Ignore, value does not exist
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
private
|
67
|
+
|
68
|
+
def keys_as_properties(hash)
|
69
|
+
props = []
|
70
|
+
|
71
|
+
hash.each do |k, v|
|
72
|
+
if v.is_a?(Hash)
|
73
|
+
props.concat(property_flatten(k.to_s, v))
|
74
|
+
else
|
75
|
+
props << k.to_s
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
props
|
80
|
+
end
|
81
|
+
|
82
|
+
def property_key(value)
|
83
|
+
keys = value.to_s.split(KEY_DELIMITER)
|
84
|
+
keys.map!(&:to_sym) if @use_symbols
|
85
|
+
keys
|
86
|
+
end
|
87
|
+
|
88
|
+
def property_flatten(root, h)
|
89
|
+
h.map do |k, v|
|
90
|
+
prop = [root, k]
|
91
|
+
prop = property_flatten(prop.join(KEY_DELIMITER), v) if v.is_a?(Hash)
|
92
|
+
prop.join(KEY_DELIMITER)
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), "lib/property_hash")
|
2
|
+
|
3
|
+
Gem::Specification.new do |spec|
|
4
|
+
spec.name = "property_hash"
|
5
|
+
spec.version = PropertyHash::VERSION
|
6
|
+
spec.authors = ["Skye Shaw"]
|
7
|
+
spec.email = ["skye.shaw@gmail.com"]
|
8
|
+
|
9
|
+
spec.summary = %q{Access a nested Ruby Hash using Java-style properties as keys.}
|
10
|
+
spec.description =<<-DESC
|
11
|
+
Access a nested Ruby Hash using Java-style properties as keys.
|
12
|
+
|
13
|
+
For example: {:a => {:b => 123}} can be accessed as "a.b" and setting "a.b.c" => 456
|
14
|
+
will create {:a => {:b => { :c => 456 }}}.
|
15
|
+
DESC
|
16
|
+
spec.homepage = "https://github.com/sshaw/property_hash"
|
17
|
+
spec.license = "MIT"
|
18
|
+
|
19
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
20
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
21
|
+
if spec.respond_to?(:metadata)
|
22
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
23
|
+
spec.metadata["source_code_uri"] = "https://github.com/sshaw/property_hash"
|
24
|
+
# spec.metadata["changelog_uri"] = ""
|
25
|
+
end
|
26
|
+
|
27
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
28
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
29
|
+
end
|
30
|
+
|
31
|
+
spec.add_development_dependency "bundler"
|
32
|
+
spec.add_development_dependency "rake"
|
33
|
+
spec.add_development_dependency "minitest", "~> 5.0", "<5.16"
|
34
|
+
end
|
metadata
ADDED
@@ -0,0 +1,108 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: property_hash
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Skye Shaw
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2022-12-27 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
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: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '5.0'
|
48
|
+
- - "<"
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: '5.16'
|
51
|
+
type: :development
|
52
|
+
prerelease: false
|
53
|
+
version_requirements: !ruby/object:Gem::Requirement
|
54
|
+
requirements:
|
55
|
+
- - "~>"
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: '5.0'
|
58
|
+
- - "<"
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '5.16'
|
61
|
+
description: |2
|
62
|
+
Access a nested Ruby Hash using Java-style properties as keys.
|
63
|
+
|
64
|
+
For example: {:a => {:b => 123}} can be accessed as "a.b" and setting "a.b.c" => 456
|
65
|
+
will create {:a => {:b => { :c => 456 }}}.
|
66
|
+
email:
|
67
|
+
- skye.shaw@gmail.com
|
68
|
+
executables: []
|
69
|
+
extensions: []
|
70
|
+
extra_rdoc_files: []
|
71
|
+
files:
|
72
|
+
- ".github/workflows/ci.yml"
|
73
|
+
- ".gitignore"
|
74
|
+
- ".rspec"
|
75
|
+
- Gemfile
|
76
|
+
- LICENSE.txt
|
77
|
+
- README.md
|
78
|
+
- Rakefile
|
79
|
+
- bin/console
|
80
|
+
- bin/setup
|
81
|
+
- lib/property_hash.rb
|
82
|
+
- property_hash.gemspec
|
83
|
+
homepage: https://github.com/sshaw/property_hash
|
84
|
+
licenses:
|
85
|
+
- MIT
|
86
|
+
metadata:
|
87
|
+
homepage_uri: https://github.com/sshaw/property_hash
|
88
|
+
source_code_uri: https://github.com/sshaw/property_hash
|
89
|
+
post_install_message:
|
90
|
+
rdoc_options: []
|
91
|
+
require_paths:
|
92
|
+
- lib
|
93
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - ">="
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '0'
|
98
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
99
|
+
requirements:
|
100
|
+
- - ">="
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: '0'
|
103
|
+
requirements: []
|
104
|
+
rubygems_version: 3.0.3
|
105
|
+
signing_key:
|
106
|
+
specification_version: 4
|
107
|
+
summary: Access a nested Ruby Hash using Java-style properties as keys.
|
108
|
+
test_files: []
|