dot_hash 0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +29 -0
- data/Rakefile +9 -0
- data/dot_hash.gemspec +20 -0
- data/lib/dot_hash.rb +5 -0
- data/lib/dot_hash/hash_to_properties.rb +10 -0
- data/lib/dot_hash/properties.rb +37 -0
- data/lib/dot_hash/version.rb +3 -0
- data/test/dot_hash_test.rb +42 -0
- data/test/hash_to_properties_test.rb +25 -0
- data/test/test_helper.rb +2 -0
- metadata +83 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Marcelo Eden
|
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,29 @@
|
|
1
|
+
# DotHash
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'dot_hash'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install dot_hash
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/dot_hash.gemspec
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path('../lib', __FILE__)
|
3
|
+
require 'dot_hash/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |gem|
|
6
|
+
gem.authors = ["Marcelo Eden"]
|
7
|
+
gem.email = ["edendroid@gmail.com"]
|
8
|
+
gem.description = %q{Access hash values using the dot notation}
|
9
|
+
gem.summary = %q{Converts hash.to_properties so you can access values using properties.some_key}
|
10
|
+
gem.homepage = "https://github.com/3den/dot_hash"
|
11
|
+
|
12
|
+
gem.files = `git ls-files`.split($\)
|
13
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
14
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
15
|
+
gem.name = "dot_hash"
|
16
|
+
gem.require_paths = ["lib"]
|
17
|
+
gem.version = DotHash::VERSION
|
18
|
+
|
19
|
+
gem.add_development_dependency "rake"
|
20
|
+
end
|
data/lib/dot_hash.rb
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
module DotHash
|
2
|
+
class Properties
|
3
|
+
attr_reader :hash
|
4
|
+
|
5
|
+
def initialize(hash)
|
6
|
+
@hash = hash
|
7
|
+
end
|
8
|
+
|
9
|
+
def method_missing(key)
|
10
|
+
fetch key or super
|
11
|
+
end
|
12
|
+
|
13
|
+
private
|
14
|
+
|
15
|
+
def fetch(key)
|
16
|
+
simbolize_key key
|
17
|
+
has_key? key and get_value key
|
18
|
+
end
|
19
|
+
|
20
|
+
def has_key?(key)
|
21
|
+
hash.has_key? key.to_sym
|
22
|
+
end
|
23
|
+
|
24
|
+
def get_value(key)
|
25
|
+
key = key.to_sym
|
26
|
+
value = hash[key]
|
27
|
+
return value unless value.is_a? Hash
|
28
|
+
|
29
|
+
hash[key] = self.class.new value
|
30
|
+
end
|
31
|
+
|
32
|
+
def simbolize_key(key)
|
33
|
+
return unless hash.has_key?(key.to_s)
|
34
|
+
hash[key.to_sym] = hash.delete key.to_s
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require File.expand_path("../test_helper", __FILE__)
|
2
|
+
|
3
|
+
module DotHash
|
4
|
+
describe Properties do
|
5
|
+
attr_reader :settings
|
6
|
+
|
7
|
+
describe "with a simple hash" do
|
8
|
+
before do
|
9
|
+
@settings = Properties.new speed: "15", "power" => 100
|
10
|
+
end
|
11
|
+
|
12
|
+
it "gets a hash property using the dot notation" do
|
13
|
+
settings.speed.must_equal "15"
|
14
|
+
end
|
15
|
+
|
16
|
+
it "raises an error if the method is not on the hash" do
|
17
|
+
-> {settings.name}.must_raise NoMethodError
|
18
|
+
end
|
19
|
+
|
20
|
+
it "gets a property from a stringed key" do
|
21
|
+
settings.power.must_equal 100
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
describe "with a nested hash" do
|
26
|
+
before do
|
27
|
+
@settings = Properties.new user: {
|
28
|
+
"info" => {name: "dude"}
|
29
|
+
}
|
30
|
+
end
|
31
|
+
|
32
|
+
it "returns a DotHash instence for property with a nested hash" do
|
33
|
+
settings.user.must_be_instance_of DotHash::Properties
|
34
|
+
end
|
35
|
+
|
36
|
+
it "gets chained properties" do
|
37
|
+
settings.user.info.name.must_equal "dude"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require File.expand_path("../test_helper", __FILE__)
|
2
|
+
|
3
|
+
describe Hash do
|
4
|
+
attr_reader :properties
|
5
|
+
|
6
|
+
describe "#to_propertires" do
|
7
|
+
before do
|
8
|
+
@properties = {
|
9
|
+
"price" => 10, info: {name: "eagle"}
|
10
|
+
}.to_properties
|
11
|
+
end
|
12
|
+
|
13
|
+
it "returns a DotHash object" do
|
14
|
+
properties.must_be_instance_of DotHash::Properties
|
15
|
+
end
|
16
|
+
|
17
|
+
it "gets nested properties" do
|
18
|
+
properties.info.name.must_equal "eagle"
|
19
|
+
end
|
20
|
+
|
21
|
+
it "gets simple properties" do
|
22
|
+
properties.price.must_equal 10
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: dot_hash
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: '0.1'
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Marcelo Eden
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-01-11 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rake
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
description: Access hash values using the dot notation
|
31
|
+
email:
|
32
|
+
- edendroid@gmail.com
|
33
|
+
executables: []
|
34
|
+
extensions: []
|
35
|
+
extra_rdoc_files: []
|
36
|
+
files:
|
37
|
+
- .gitignore
|
38
|
+
- Gemfile
|
39
|
+
- LICENSE
|
40
|
+
- README.md
|
41
|
+
- Rakefile
|
42
|
+
- dot_hash.gemspec
|
43
|
+
- lib/dot_hash.rb
|
44
|
+
- lib/dot_hash/hash_to_properties.rb
|
45
|
+
- lib/dot_hash/properties.rb
|
46
|
+
- lib/dot_hash/version.rb
|
47
|
+
- test/dot_hash_test.rb
|
48
|
+
- test/hash_to_properties_test.rb
|
49
|
+
- test/test_helper.rb
|
50
|
+
homepage: https://github.com/3den/dot_hash
|
51
|
+
licenses: []
|
52
|
+
post_install_message:
|
53
|
+
rdoc_options: []
|
54
|
+
require_paths:
|
55
|
+
- lib
|
56
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
segments:
|
63
|
+
- 0
|
64
|
+
hash: -4490636092357410470
|
65
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
66
|
+
none: false
|
67
|
+
requirements:
|
68
|
+
- - ! '>='
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: '0'
|
71
|
+
segments:
|
72
|
+
- 0
|
73
|
+
hash: -4490636092357410470
|
74
|
+
requirements: []
|
75
|
+
rubyforge_project:
|
76
|
+
rubygems_version: 1.8.24
|
77
|
+
signing_key:
|
78
|
+
specification_version: 3
|
79
|
+
summary: Converts hash.to_properties so you can access values using properties.some_key
|
80
|
+
test_files:
|
81
|
+
- test/dot_hash_test.rb
|
82
|
+
- test/hash_to_properties_test.rb
|
83
|
+
- test/test_helper.rb
|