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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: dab622231f4600a8ac046c3b77db226f8d828cd0
4
- data.tar.gz: e44c1d1749daa87efa0ec7bb9bfed8bdc665f471
3
+ metadata.gz: bc2ee27b7f3e0b5b95b6480e808fa7da9e4d8bf2
4
+ data.tar.gz: f768a8c9fa20916a086b35f11c82f3a3f1142cd8
5
5
  SHA512:
6
- metadata.gz: c20b73a381892e22111316fcb979565995f2797b41cf6e3a65f1259a7dffaaec4e67e558291b072011c7500d5323a0747646222345fb6a761d177d91ce172f0f
7
- data.tar.gz: 9e09847730cffe55dc325ee720a422bfafd77a7869c35733f35315991f3e7a61fdc5ac2bc7ecf60da4a6e98238bc11d92ef6db4ff196278eda2894d9f8b8493a
6
+ metadata.gz: 2b9d9463d14ce761977bb76dfd28b5020d7f6c359762edf4ec7a41b3cce63bc0eb2480e46d501b7f1d2e8697e34585c9173119f3cb76e1bef7d73f184c644364
7
+ data.tar.gz: eda72f3618defa9d9d6b704a9a5e50c295f9f0260172a4d5cbaa14a8869a7c45091d308df8af3d2aa5ab1cd42a046f2c4431c03437a494292495e943f4b2ac34
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Ndhash
2
2
 
3
- Creates n-dimension hashes
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
- NDHash.generate(level:3, entries_per_level:2, pointers_per_level:1)
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
 
@@ -1,3 +1,3 @@
1
1
  module NDHash
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
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(level:, entries_per_level:2, pointers_per_level:1)
7
- generate_rec(1, level, entries_per_level, pointers_per_level)
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{Creates n-dimension hashes.}
13
- spec.homepage = "https://rubygems.org/gems/ndhash"
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.1.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://rubygems.org/gems/ndhash
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: Creates n-dimension hashes.
83
+ summary: N-dimension hash creation and utilities. Good for testing multi-level hash
84
+ and JSON processing.
84
85
  test_files: []