hashable 0.1.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.
- data/.gitignore +17 -0
- data/.travis.yml +5 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +44 -0
- data/Rakefile +20 -0
- data/hashable.gemspec +23 -0
- data/lib/hashable.rb +3 -0
- data/lib/hashable/hash.rb +17 -0
- data/lib/hashable/hashable.rb +43 -0
- data/lib/hashable/version.rb +3 -0
- data/test/test_helper.rb +2 -0
- data/test/unit/hashable_test.rb +24 -0
- metadata +93 -0
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Mustafa Turan
|
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,44 @@
|
|
1
|
+
# Hashable
|
2
|
+
|
3
|
+
Makes objects to hash recursively.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'hashable'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install hashable
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
Include 'Hashable' in your objects and then objects will have to_dh method and as an alias method to_deep_hash
|
22
|
+
|
23
|
+
### Sample
|
24
|
+
require 'hashable'
|
25
|
+
class A
|
26
|
+
include Hashable
|
27
|
+
attr_accessor :id, :name
|
28
|
+
end
|
29
|
+
|
30
|
+
a = A.new
|
31
|
+
a.to_dh
|
32
|
+
|
33
|
+
or
|
34
|
+
|
35
|
+
a.to_deep_hash
|
36
|
+
|
37
|
+
|
38
|
+
## Contributing
|
39
|
+
|
40
|
+
1. Fork it
|
41
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
42
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
43
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
44
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
2
|
+
require 'rake'
|
3
|
+
require 'rake/testtask'
|
4
|
+
|
5
|
+
desc 'Default Task'
|
6
|
+
task :default => [ :test ]
|
7
|
+
|
8
|
+
# Run the unit tests
|
9
|
+
desc 'Run all unit tests'
|
10
|
+
Rake::TestTask.new do |t|
|
11
|
+
t.libs << 'lib'
|
12
|
+
t.test_files = FileList['test/unit/*_test.rb']
|
13
|
+
t.verbose = true
|
14
|
+
end
|
15
|
+
|
16
|
+
# Make a console for testing purposes
|
17
|
+
desc 'Generate a test console'
|
18
|
+
task :console do
|
19
|
+
verbose( false ) { sh "irb -I lib/ -r 'hashable'" }
|
20
|
+
end
|
data/hashable.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 'hashable/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'hashable'
|
8
|
+
spec.version = Hashable::VERSION
|
9
|
+
spec.authors = ['Mustafa Turan']
|
10
|
+
spec.email = ['mustafaturan.net@gmail.com']
|
11
|
+
spec.description = %q{Recursively convert your objects into nested hashes}
|
12
|
+
spec.summary = spec.description
|
13
|
+
spec.homepage = 'https://github.com/mustafaturan/hashable'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
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.3'
|
22
|
+
spec.add_development_dependency 'rake'
|
23
|
+
end
|
data/lib/hashable.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/hashable'
|
2
|
+
|
3
|
+
class Hash
|
4
|
+
include Hashable
|
5
|
+
|
6
|
+
# Return recursively hash representation of the given hash
|
7
|
+
#
|
8
|
+
# @return Base object instance variables in a Hash
|
9
|
+
def to_deep_hash
|
10
|
+
hash = {}
|
11
|
+
self.keys.each do |key|
|
12
|
+
hash[key] = deeply_to_hash(self[key])
|
13
|
+
end
|
14
|
+
hash
|
15
|
+
end
|
16
|
+
alias_method :to_dh, :to_deep_hash
|
17
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
module Hashable
|
2
|
+
# Return recursively hash representation of the object
|
3
|
+
#
|
4
|
+
# @return hash
|
5
|
+
def to_deep_hash
|
6
|
+
hash = {}
|
7
|
+
instance_variables.each do |key|
|
8
|
+
hash[key[1..-1].to_sym] = deeply_to_hash(instance_variable_get(key))
|
9
|
+
end
|
10
|
+
hash
|
11
|
+
end
|
12
|
+
alias_method :to_dh, :to_deep_hash
|
13
|
+
|
14
|
+
protected
|
15
|
+
# nodoc
|
16
|
+
def convert_hash_values(hash)
|
17
|
+
hash.each do |key, val|
|
18
|
+
hash[key] = deeply_to_hash(val)
|
19
|
+
end
|
20
|
+
hash
|
21
|
+
end
|
22
|
+
|
23
|
+
# nodoc
|
24
|
+
def convert_array_values(array)
|
25
|
+
array.each_index do |index|
|
26
|
+
array[index] = deeply_to_hash(array[index])
|
27
|
+
end
|
28
|
+
array
|
29
|
+
end
|
30
|
+
|
31
|
+
# nodoc
|
32
|
+
def deeply_to_hash(val)
|
33
|
+
if val.is_a?(Hash)
|
34
|
+
convert_hash_values(val)
|
35
|
+
elsif val.is_a?(Array)
|
36
|
+
convert_array_values(val)
|
37
|
+
elsif val.respond_to?(:to_dh)
|
38
|
+
val.to_dh
|
39
|
+
else
|
40
|
+
val
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'test_helper'))
|
2
|
+
|
3
|
+
class HashableTest < Test::Unit::TestCase
|
4
|
+
def test_to_deep_hash
|
5
|
+
hash_representation = {
|
6
|
+
:blist => [ { :id => 1}, {'b' => { :id => 2 } } ]
|
7
|
+
}
|
8
|
+
assert_equal(hash_representation, A.new().to_dh)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
class A
|
13
|
+
include Hashable
|
14
|
+
attr_accessor :blist
|
15
|
+
def initialize
|
16
|
+
@blist = [ B.new(1), { 'b' => B.new(2) } ]
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
class B
|
21
|
+
include Hashable
|
22
|
+
attr_accessor :id
|
23
|
+
def initialize(id); @id = id; end
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hashable
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Mustafa Turan
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-07-10 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '1.3'
|
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: '1.3'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rake
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
description: Recursively convert your objects into nested hashes
|
47
|
+
email:
|
48
|
+
- mustafaturan.net@gmail.com
|
49
|
+
executables: []
|
50
|
+
extensions: []
|
51
|
+
extra_rdoc_files: []
|
52
|
+
files:
|
53
|
+
- .gitignore
|
54
|
+
- .travis.yml
|
55
|
+
- Gemfile
|
56
|
+
- LICENSE.txt
|
57
|
+
- README.md
|
58
|
+
- Rakefile
|
59
|
+
- hashable.gemspec
|
60
|
+
- lib/hashable.rb
|
61
|
+
- lib/hashable/hash.rb
|
62
|
+
- lib/hashable/hashable.rb
|
63
|
+
- lib/hashable/version.rb
|
64
|
+
- test/test_helper.rb
|
65
|
+
- test/unit/hashable_test.rb
|
66
|
+
homepage: https://github.com/mustafaturan/hashable
|
67
|
+
licenses:
|
68
|
+
- MIT
|
69
|
+
post_install_message:
|
70
|
+
rdoc_options: []
|
71
|
+
require_paths:
|
72
|
+
- lib
|
73
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
74
|
+
none: false
|
75
|
+
requirements:
|
76
|
+
- - ! '>='
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: '0'
|
79
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
80
|
+
none: false
|
81
|
+
requirements:
|
82
|
+
- - ! '>='
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: '0'
|
85
|
+
requirements: []
|
86
|
+
rubyforge_project:
|
87
|
+
rubygems_version: 1.8.23
|
88
|
+
signing_key:
|
89
|
+
specification_version: 3
|
90
|
+
summary: Recursively convert your objects into nested hashes
|
91
|
+
test_files:
|
92
|
+
- test/test_helper.rb
|
93
|
+
- test/unit/hashable_test.rb
|