dot_hash 0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
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
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in dot_hash.gemspec
4
+ gemspec
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
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+ require 'rake/testtask'
4
+
5
+ Rake::TestTask.new do |t|
6
+ t.libs.push "lib"
7
+ t.test_files = FileList['test/*_test.rb']
8
+ t.verbose = true
9
+ end
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,5 @@
1
+ $:.push File.expand_path('../', __FILE__)
2
+
3
+ require 'dot_hash/properties'
4
+ require 'dot_hash/hash_to_properties'
5
+ require 'dot_hash/version'
@@ -0,0 +1,10 @@
1
+ module DotHash
2
+ module HashToProperties
3
+ def to_properties
4
+ Properties.new self
5
+ end
6
+ end
7
+ end
8
+
9
+ puts "TO PROPERTIES"
10
+ Hash.send :include, DotHash::HashToProperties
@@ -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,3 @@
1
+ module DotHash
2
+ VERSION = 0.1
3
+ 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
@@ -0,0 +1,2 @@
1
+ require "minitest/autorun"
2
+ require File.expand_path("../../lib/dot_hash", __FILE__)
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