diver 1.0.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 +22 -0
- data/Gemfile +3 -0
- data/LICENSE.txt +22 -0
- data/README.md +52 -0
- data/Rakefile +9 -0
- data/diver.gemspec +23 -0
- data/lib/diver.rb +53 -0
- data/lib/diver/version.rb +3 -0
- data/test/test_diver.rb +64 -0
- metadata +83 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: cbc7a85b063ab7244a575b628c430dd01ec9e409
|
4
|
+
data.tar.gz: 54b9e823ad8244c2eccf37a10d0645d84ac05336
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f781771c6a5239d93c8cb3259485815fcb636da12cdd3b2339cc41a9da68826510e85e33e32260ac3120b572cfa62b5a852368f57526400fd05d08a143981d94
|
7
|
+
data.tar.gz: 3d4f3d3f49b330748f1dbc57270735a53c29d257d9c6032a5cadba64687bc8cf16964c15a3f5f7ae29ab12b15975e67c5987f30f8b28c321e28fca1c0eabe7dc
|
data/.gitignore
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
.bundle
|
4
|
+
.config
|
5
|
+
.yardoc
|
6
|
+
Gemfile.lock
|
7
|
+
InstalledFiles
|
8
|
+
_yardoc
|
9
|
+
coverage
|
10
|
+
doc/
|
11
|
+
lib/bundler/man
|
12
|
+
pkg
|
13
|
+
rdoc
|
14
|
+
spec/reports
|
15
|
+
test/tmp
|
16
|
+
test/version_tmp
|
17
|
+
tmp
|
18
|
+
*.bundle
|
19
|
+
*.so
|
20
|
+
*.o
|
21
|
+
*.a
|
22
|
+
mkmf.log
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Radu-Bogdan Croitoru
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
# Diver
|
2
|
+
|
3
|
+
Simple, ad-hoc access to elements of deeply nested structures.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'diver'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install diver
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
Use dive method to pull out a value from a nested data structure.
|
22
|
+
|
23
|
+
Example:
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
> x = {"a" => {"b" => {"c" => "d"}}}
|
27
|
+
|
28
|
+
> x.dive("a", "b") # sugar for x["a"]["b"]
|
29
|
+
|
30
|
+
{"c" => "d"}
|
31
|
+
|
32
|
+
```
|
33
|
+
|
34
|
+
Use diveSet method to create a nested data structure.
|
35
|
+
|
36
|
+
```ruby
|
37
|
+
> x = {}
|
38
|
+
> x.diveSet("a", "b", "c", "d")
|
39
|
+
> p x
|
40
|
+
{"a" => {"b" => {"c" => "d"}}}
|
41
|
+
```
|
42
|
+
|
43
|
+
## Contributing
|
44
|
+
|
45
|
+
1. Fork it ( https://github.com/radubogdan/ruby-diver/fork )
|
46
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
47
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
48
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
49
|
+
5. Create a new Pull Request
|
50
|
+
|
51
|
+
## Regards
|
52
|
+
Thanks to [Tye McQueen](http://search.cpan.org/~tyemq/) who made this first long time ago for the Perl Community.
|
data/Rakefile
ADDED
data/diver.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'diver/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "diver"
|
8
|
+
spec.version = Diver::VERSION
|
9
|
+
spec.authors = ["Radu-Bogdan Croitoru"]
|
10
|
+
spec.email = ["croitoruradubogdan@gmail.com"]
|
11
|
+
spec.summary = %q{Simple, ad-hoc access to elements of deeply nested structures}
|
12
|
+
spec.description = %q{Implement new methods in Hash class for ad-hoc access to elements of deeply nested data structures}
|
13
|
+
spec.homepage = "https://github.com/radubogdan/ruby-diver.git"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.6"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
end
|
data/lib/diver.rb
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
require "diver/version"
|
2
|
+
|
3
|
+
# Add two new methods to the Hash class that will allow ad-hoc accesses
|
4
|
+
# of deeply nested structures
|
5
|
+
#
|
6
|
+
# Author: Radu-Bogdan Croitoru (mailto:croitoruradubogdan@gmail.com)
|
7
|
+
#
|
8
|
+
# License: MIT License. See LICENSE file.
|
9
|
+
#
|
10
|
+
|
11
|
+
class Hash
|
12
|
+
|
13
|
+
# This function will pull out one value from a nested data structure.
|
14
|
+
#
|
15
|
+
# Example:
|
16
|
+
#
|
17
|
+
# x = {"a" => {"b" => "c"}}
|
18
|
+
#
|
19
|
+
# x.dive('a')
|
20
|
+
#
|
21
|
+
# Return: {"b" => "c"}
|
22
|
+
#
|
23
|
+
def dive(*args)
|
24
|
+
args_list = args.flatten
|
25
|
+
key = args_list.shift
|
26
|
+
|
27
|
+
if key
|
28
|
+
new_hash = self[key]
|
29
|
+
new_hash.class == Hash ? new_hash.dive(args_list) : new_hash
|
30
|
+
else
|
31
|
+
self
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
# This function will create a nested hash
|
36
|
+
#
|
37
|
+
# Example:
|
38
|
+
#
|
39
|
+
# x = {}
|
40
|
+
#
|
41
|
+
# x.diveSet('a','b','c')
|
42
|
+
#
|
43
|
+
# p x # {"a" => {"b" => "c"}}
|
44
|
+
#
|
45
|
+
def diveSet(*args)
|
46
|
+
args_list = args.flatten
|
47
|
+
return self if args_list.length == 0 || args_list[0] == "" || args_list.length == 1
|
48
|
+
key = args_list.shift
|
49
|
+
return self[key] = args_list[0] if args_list.length == 1
|
50
|
+
self[key] = {} unless self.has_key?(key)
|
51
|
+
self[key].diveSet(args_list)
|
52
|
+
end
|
53
|
+
end
|
data/test/test_diver.rb
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'diver'
|
3
|
+
|
4
|
+
class DiverTest < Test::Unit::TestCase
|
5
|
+
def setup
|
6
|
+
@short_test = {'a' => {'b' => {'c' => 'd'}}}
|
7
|
+
@empty = {}
|
8
|
+
end
|
9
|
+
|
10
|
+
# Simple tests for dive function
|
11
|
+
def test_respond_to_dive_function
|
12
|
+
assert_respond_to(@short_test, :dive)
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_expect_a_result_when_key_exist
|
16
|
+
assert_not_nil(@short_test.dive('a'))
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_expect_nil_as_result_when_key_doesnt_exist
|
20
|
+
assert_nil(@short_test.dive('x'))
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_should_equal_semantic
|
24
|
+
assert_equal(@short_test['a']['b']['c'], @short_test.dive('a', 'b', 'c'))
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_should_equal_hash
|
28
|
+
assert_equal({'b' => {'c' => 'd'}}, @short_test.dive('a'))
|
29
|
+
assert_equal({'c' => 'd'}, @short_test.dive('a', 'b'))
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_should_equal_value
|
33
|
+
assert_equal('d', @short_test.dive('a', 'b', 'c'))
|
34
|
+
end
|
35
|
+
|
36
|
+
# Simple tests for diveSet function
|
37
|
+
def test_respond_to_diveSet_function
|
38
|
+
assert_respond_to(@empty, :diveSet)
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_expect_something_when_string_is_given
|
42
|
+
assert_not_nil(@empty.diveSet('a'))
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_expect_empty_hash
|
46
|
+
assert_equal(@empty, @empty.diveSet(''))
|
47
|
+
assert_equal({}, @empty.diveSet('a'))
|
48
|
+
end
|
49
|
+
|
50
|
+
def test_expect_simple_hash
|
51
|
+
@empty.diveSet('a', 'b')
|
52
|
+
assert_equal({'a' => 'b'}, @empty)
|
53
|
+
end
|
54
|
+
|
55
|
+
def test_expect_nested_hash
|
56
|
+
@empty.diveSet('a','b','c')
|
57
|
+
assert_equal({'a' => {'b' => 'c'}}, @empty)
|
58
|
+
end
|
59
|
+
|
60
|
+
def test_expect_big_nested_hash
|
61
|
+
@empty.diveSet('a', 'b', 'c', 'd', 'e', 'f', 'g')
|
62
|
+
assert_equal({'a' => {'b' => {'c' => {'d' => {'e' => {'f' => 'g'}}}}}}, @empty)
|
63
|
+
end
|
64
|
+
end
|
metadata
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: diver
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Radu-Bogdan Croitoru
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-08-02 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: '1.6'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.6'
|
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
|
+
description: Implement new methods in Hash class for ad-hoc access to elements of
|
42
|
+
deeply nested data structures
|
43
|
+
email:
|
44
|
+
- croitoruradubogdan@gmail.com
|
45
|
+
executables: []
|
46
|
+
extensions: []
|
47
|
+
extra_rdoc_files: []
|
48
|
+
files:
|
49
|
+
- ".gitignore"
|
50
|
+
- Gemfile
|
51
|
+
- LICENSE.txt
|
52
|
+
- README.md
|
53
|
+
- Rakefile
|
54
|
+
- diver.gemspec
|
55
|
+
- lib/diver.rb
|
56
|
+
- lib/diver/version.rb
|
57
|
+
- test/test_diver.rb
|
58
|
+
homepage: https://github.com/radubogdan/ruby-diver.git
|
59
|
+
licenses:
|
60
|
+
- MIT
|
61
|
+
metadata: {}
|
62
|
+
post_install_message:
|
63
|
+
rdoc_options: []
|
64
|
+
require_paths:
|
65
|
+
- lib
|
66
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
67
|
+
requirements:
|
68
|
+
- - ">="
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: '0'
|
71
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
requirements: []
|
77
|
+
rubyforge_project:
|
78
|
+
rubygems_version: 2.2.0
|
79
|
+
signing_key:
|
80
|
+
specification_version: 4
|
81
|
+
summary: Simple, ad-hoc access to elements of deeply nested structures
|
82
|
+
test_files:
|
83
|
+
- test/test_diver.rb
|