ndhash 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +14 -2
- data/lib/ndhash/version.rb +1 -1
- data/lib/ndhash.rb +19 -2
- data/ndhash.gemspec +2 -2
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bc2ee27b7f3e0b5b95b6480e808fa7da9e4d8bf2
|
4
|
+
data.tar.gz: f768a8c9fa20916a086b35f11c82f3a3f1142cd8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2b9d9463d14ce761977bb76dfd28b5020d7f6c359762edf4ec7a41b3cce63bc0eb2480e46d501b7f1d2e8697e34585c9173119f3cb76e1bef7d73f184c644364
|
7
|
+
data.tar.gz: eda72f3618defa9d9d6b704a9a5e50c295f9f0260172a4d5cbaa14a8869a7c45091d308df8af3d2aa5ab1cd42a046f2c4431c03437a494292495e943f4b2ac34
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# Ndhash
|
2
2
|
|
3
|
-
|
3
|
+
N-dimension hash creation and utilities. Good for testing multi-level hash and JSON processing.
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
@@ -20,9 +20,21 @@ Or install it yourself as:
|
|
20
20
|
|
21
21
|
## Usage
|
22
22
|
|
23
|
+
```ruby
|
23
24
|
require 'ndhash'
|
24
25
|
|
25
|
-
|
26
|
+
# Generate hash with custom number of levels, number of key-value pairs and hashes per level
|
27
|
+
NDHash.generate(levels:2, values_per_level:2, hashes_per_level:2)
|
28
|
+
# =>
|
29
|
+
```
|
30
|
+
```json
|
31
|
+
{"level_1_key_1":"level_1_value_1","level_1_key_2":"level_1_value_2","level_1_pointer_1":{"level_2_key_1":"level_2_value_1","level_2_key_2":"level_2_value_2"},"level_1_pointer_2":{"level_2_key_1":"level_2_value_1","level_2_key_2":"level_2_value_2"}}
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
# Count number of nested levels per hash
|
35
|
+
NDHash.count_levels({key:{}}})
|
36
|
+
# => 2
|
37
|
+
```
|
26
38
|
|
27
39
|
## Development
|
28
40
|
|
data/lib/ndhash/version.rb
CHANGED
data/lib/ndhash.rb
CHANGED
@@ -3,8 +3,10 @@ require "ndhash/version"
|
|
3
3
|
module NDHash
|
4
4
|
module_function
|
5
5
|
|
6
|
-
def generate(
|
7
|
-
|
6
|
+
def generate(levels:, values_per_level:2, hashes_per_level:1)
|
7
|
+
raise ArgumentError, 'Negative or zero value given' if levels < 1 or values_per_level < 0 or hashes_per_level < 0
|
8
|
+
raise ArgumentError, 'Less than 1 number of pointers defined for multi-level hash' if levels > 1 and hashes_per_level < 1
|
9
|
+
generate_rec(1, levels, values_per_level, hashes_per_level)
|
8
10
|
end
|
9
11
|
|
10
12
|
def generate_rec(current_level, max_level, entries_per_level, pointers_per_level)
|
@@ -39,4 +41,19 @@ module NDHash
|
|
39
41
|
def get_pointer_key(level, entry_num:)
|
40
42
|
"level_#{level}_pointer_#{entry_num}"
|
41
43
|
end
|
44
|
+
|
45
|
+
def count_levels(hash)
|
46
|
+
count_levels_rec(1, hash)
|
47
|
+
end
|
48
|
+
|
49
|
+
def count_levels_rec(levels, hash)
|
50
|
+
hash.each do |k, v|
|
51
|
+
if Hash === v
|
52
|
+
levels = count_levels_rec(levels+1, v)
|
53
|
+
else
|
54
|
+
return levels
|
55
|
+
end
|
56
|
+
end
|
57
|
+
levels
|
58
|
+
end
|
42
59
|
end
|
data/ndhash.gemspec
CHANGED
@@ -9,8 +9,8 @@ Gem::Specification.new do |spec|
|
|
9
9
|
spec.authors = ["Csaba Apagyi"]
|
10
10
|
spec.email = ["csapagyi@users.noreply.github.com"]
|
11
11
|
|
12
|
-
spec.summary = %q{
|
13
|
-
spec.homepage = "https://
|
12
|
+
spec.summary = %q{N-dimension hash creation and utilities. Good for testing multi-level hash and JSON processing.}
|
13
|
+
spec.homepage = "https://github.com/csapagyi/ndhash"
|
14
14
|
spec.license = "MIT"
|
15
15
|
|
16
16
|
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ndhash
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Csaba Apagyi
|
@@ -57,7 +57,7 @@ files:
|
|
57
57
|
- lib/ndhash/version.rb
|
58
58
|
- ndhash.gemspec
|
59
59
|
- ndhash/README.md
|
60
|
-
homepage: https://
|
60
|
+
homepage: https://github.com/csapagyi/ndhash
|
61
61
|
licenses:
|
62
62
|
- MIT
|
63
63
|
metadata: {}
|
@@ -80,5 +80,6 @@ rubyforge_project:
|
|
80
80
|
rubygems_version: 2.5.1
|
81
81
|
signing_key:
|
82
82
|
specification_version: 4
|
83
|
-
summary:
|
83
|
+
summary: N-dimension hash creation and utilities. Good for testing multi-level hash
|
84
|
+
and JSON processing.
|
84
85
|
test_files: []
|