hflat 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/Gemfile +18 -0
- data/LICENSE.txt +20 -0
- data/README.rdoc +21 -0
- data/Rakefile +51 -0
- data/VERSION +1 -0
- data/hflat.gemspec +58 -0
- data/lib/hflat.rb +16 -0
- data/spec/hflat_spec.rb +62 -0
- data/spec/spec_helper.rb +17 -0
- metadata +112 -0
data/.document
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
source "http://rubygems.org"
|
2
|
+
# Add dependencies required to use your gem here.
|
3
|
+
# Example:
|
4
|
+
# gem "activesupport", ">= 2.3.5"
|
5
|
+
|
6
|
+
# Add dependencies to develop your gem here.
|
7
|
+
# Include everything needed to run rake, tests, features, etc.
|
8
|
+
group :development do
|
9
|
+
gem "shoulda", ">= 0"
|
10
|
+
gem "bundler", "~> 1.0.0"
|
11
|
+
gem "jeweler", "~> 1.6.2"
|
12
|
+
gem "rcov", ">= 0"
|
13
|
+
end
|
14
|
+
|
15
|
+
group :test do
|
16
|
+
gem 'rspec', :require => 'spec'
|
17
|
+
gem 'rack-test'
|
18
|
+
end
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 Carlos Vilhena
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
= hflat
|
2
|
+
|
3
|
+
This is a simple gem that extends the Hash class with a method to flatten a hash table.
|
4
|
+
|
5
|
+
Some examples can be found in the examples directory.
|
6
|
+
|
7
|
+
|
8
|
+
== Contributing to hflat
|
9
|
+
|
10
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
|
11
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
|
12
|
+
* Fork the project
|
13
|
+
* Start a feature/bugfix branch
|
14
|
+
* Commit and push until you are happy with your contribution
|
15
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
16
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
17
|
+
|
18
|
+
== Copyright
|
19
|
+
|
20
|
+
Copyright (c) 2011 Carlos. See LICENSE.txt for
|
21
|
+
further details.
|
data/Rakefile
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler'
|
5
|
+
require 'rspec/core/rake_task'
|
6
|
+
|
7
|
+
task :default => :test
|
8
|
+
task :test => :spec
|
9
|
+
|
10
|
+
begin
|
11
|
+
Bundler.setup(:default, :development)
|
12
|
+
rescue Bundler::BundlerError => e
|
13
|
+
$stderr.puts e.message
|
14
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
15
|
+
exit e.status_code
|
16
|
+
end
|
17
|
+
|
18
|
+
if !defined?(RSpec)
|
19
|
+
puts "spec targets require RSpec"
|
20
|
+
else
|
21
|
+
desc "Run all examples"
|
22
|
+
RSpec::Core::RakeTask.new(:spec) do |t|
|
23
|
+
t.pattern = 'spec/**/*.rb'
|
24
|
+
t.rspec_opts = ['-cfs']
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
require 'rake'
|
29
|
+
require 'jeweler'
|
30
|
+
Jeweler::Tasks.new do |gem|
|
31
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
32
|
+
gem.name = "hflat"
|
33
|
+
gem.homepage = "http://github.com/carvil/hflat"
|
34
|
+
gem.license = "MIT"
|
35
|
+
gem.summary = %Q{Flatten a hash table}
|
36
|
+
gem.description = %Q{Flattens a hash table in order to only have a hash table with key/values where each value is not a hash.}
|
37
|
+
gem.email = "carlosvilhena@gmail.com"
|
38
|
+
gem.authors = ["Carlos Vilhena"]
|
39
|
+
# dependencies defined in Gemfile
|
40
|
+
end
|
41
|
+
Jeweler::RubygemsDotOrgTasks.new
|
42
|
+
|
43
|
+
require 'rake/rdoctask'
|
44
|
+
Rake::RDocTask.new do |rdoc|
|
45
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
46
|
+
|
47
|
+
rdoc.rdoc_dir = 'rdoc'
|
48
|
+
rdoc.title = "hflat #{version}"
|
49
|
+
rdoc.rdoc_files.include('README*')
|
50
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
51
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
data/hflat.gemspec
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{hflat}
|
8
|
+
s.version = "0.1.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Carlos Vilhena"]
|
12
|
+
s.date = %q{2011-06-21}
|
13
|
+
s.description = %q{Flattens a hash table in order to only have a hash table with key/values where each value is not a hash.}
|
14
|
+
s.email = %q{carlosvilhena@gmail.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE.txt",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
"Gemfile",
|
22
|
+
"LICENSE.txt",
|
23
|
+
"README.rdoc",
|
24
|
+
"Rakefile",
|
25
|
+
"VERSION",
|
26
|
+
"hflat.gemspec",
|
27
|
+
"lib/hflat.rb",
|
28
|
+
"spec/hflat_spec.rb",
|
29
|
+
"spec/spec_helper.rb"
|
30
|
+
]
|
31
|
+
s.homepage = %q{http://github.com/carvil/hflat}
|
32
|
+
s.licenses = ["MIT"]
|
33
|
+
s.require_paths = ["lib"]
|
34
|
+
s.rubygems_version = %q{1.6.2}
|
35
|
+
s.summary = %q{Flatten a hash table}
|
36
|
+
|
37
|
+
if s.respond_to? :specification_version then
|
38
|
+
s.specification_version = 3
|
39
|
+
|
40
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
41
|
+
s.add_development_dependency(%q<shoulda>, [">= 0"])
|
42
|
+
s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
|
43
|
+
s.add_development_dependency(%q<jeweler>, ["~> 1.6.2"])
|
44
|
+
s.add_development_dependency(%q<rcov>, [">= 0"])
|
45
|
+
else
|
46
|
+
s.add_dependency(%q<shoulda>, [">= 0"])
|
47
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
48
|
+
s.add_dependency(%q<jeweler>, ["~> 1.6.2"])
|
49
|
+
s.add_dependency(%q<rcov>, [">= 0"])
|
50
|
+
end
|
51
|
+
else
|
52
|
+
s.add_dependency(%q<shoulda>, [">= 0"])
|
53
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
54
|
+
s.add_dependency(%q<jeweler>, ["~> 1.6.2"])
|
55
|
+
s.add_dependency(%q<rcov>, [">= 0"])
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
data/lib/hflat.rb
ADDED
data/spec/hflat_spec.rb
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
require "#{File.dirname(__FILE__)}/spec_helper"
|
2
|
+
|
3
|
+
describe "Core tests" do
|
4
|
+
|
5
|
+
before(:all) do
|
6
|
+
@hash_1 = {:key1 => {:key2 => "value2", :key3 => "value3"} }
|
7
|
+
@hash_2 = {:key1 => {:key2 => "value2", :key1 => "value3"} }
|
8
|
+
@flat_hash = { 1 => 1, 2 => 2, 3 => 3}
|
9
|
+
@mix_hash = { :key1 => 1, :key2 => {:key4 => 4, :key5 => { :key6 => 6 } } , :key3 => 3 }
|
10
|
+
@hash_3 = { :key1 => 1, :key2 => {:key4 => 4, :key5 => { } } , :key3 => nil }
|
11
|
+
end
|
12
|
+
|
13
|
+
describe "Testing Hash 1 - 1 level of nested hashes" do
|
14
|
+
it "should return only key/values 2 and 3" do
|
15
|
+
@hash_1.flatten.should == {:key2 => "value2", :key3 => "value3"}
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should contain two key/value pairs" do
|
19
|
+
@hash_1.flatten.size.should == 2
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
describe "Testing hash 2 - having key repetition in nested hashes" do
|
24
|
+
it "should return only key/values 2 and 3" do
|
25
|
+
@hash_2.flatten.should == {:key2 => "value2", :key1 => "value3"}
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should contain two key/value pairs" do
|
29
|
+
@hash_2.flatten.size.should == 2
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
describe "Testing trivial cases" do
|
34
|
+
it "should return an empty hash" do
|
35
|
+
{}.flatten.should == {}
|
36
|
+
end
|
37
|
+
it "should return itself" do
|
38
|
+
@flat_hash.flatten.should == @flat_hash
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
describe "Testing a mixed hash" do
|
43
|
+
it "should only return key/value pares 1, 3, 4 and 6" do
|
44
|
+
@mix_hash.flatten.should == { :key1 => 1, :key4 => 4, :key6 => 6, :key3 => 3 }
|
45
|
+
end
|
46
|
+
|
47
|
+
it "should contain exactly 4 elements" do
|
48
|
+
@mix_hash.flatten.size.should == 4
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
describe "Testing Hash 3" do
|
53
|
+
it "should contain exactly 3 elements" do
|
54
|
+
@hash_3.flatten.size.should == 3
|
55
|
+
end
|
56
|
+
|
57
|
+
it "should contain key/value pairs 1, 3 and 4" do
|
58
|
+
@hash_3.flatten.should == { :key1 => 1, :key4 => 4, :key3 => nil}
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
require 'rspec'
|
4
|
+
require 'rack/test'
|
5
|
+
|
6
|
+
begin
|
7
|
+
Bundler.setup(:default, :development)
|
8
|
+
rescue Bundler::BundlerError => e
|
9
|
+
$stderr.puts e.message
|
10
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
11
|
+
exit e.status_code
|
12
|
+
end
|
13
|
+
|
14
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
15
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
16
|
+
|
17
|
+
require 'hflat'
|
metadata
ADDED
@@ -0,0 +1,112 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hflat
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.1.0
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Carlos Vilhena
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2011-06-21 00:00:00 +01:00
|
14
|
+
default_executable:
|
15
|
+
dependencies:
|
16
|
+
- !ruby/object:Gem::Dependency
|
17
|
+
name: shoulda
|
18
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
19
|
+
none: false
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
type: :development
|
25
|
+
prerelease: false
|
26
|
+
version_requirements: *id001
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
30
|
+
none: false
|
31
|
+
requirements:
|
32
|
+
- - ~>
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: 1.0.0
|
35
|
+
type: :development
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: *id002
|
38
|
+
- !ruby/object:Gem::Dependency
|
39
|
+
name: jeweler
|
40
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 1.6.2
|
46
|
+
type: :development
|
47
|
+
prerelease: false
|
48
|
+
version_requirements: *id003
|
49
|
+
- !ruby/object:Gem::Dependency
|
50
|
+
name: rcov
|
51
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: "0"
|
57
|
+
type: :development
|
58
|
+
prerelease: false
|
59
|
+
version_requirements: *id004
|
60
|
+
description: Flattens a hash table in order to only have a hash table with key/values where each value is not a hash.
|
61
|
+
email: carlosvilhena@gmail.com
|
62
|
+
executables: []
|
63
|
+
|
64
|
+
extensions: []
|
65
|
+
|
66
|
+
extra_rdoc_files:
|
67
|
+
- LICENSE.txt
|
68
|
+
- README.rdoc
|
69
|
+
files:
|
70
|
+
- .document
|
71
|
+
- Gemfile
|
72
|
+
- LICENSE.txt
|
73
|
+
- README.rdoc
|
74
|
+
- Rakefile
|
75
|
+
- VERSION
|
76
|
+
- hflat.gemspec
|
77
|
+
- lib/hflat.rb
|
78
|
+
- spec/hflat_spec.rb
|
79
|
+
- spec/spec_helper.rb
|
80
|
+
has_rdoc: true
|
81
|
+
homepage: http://github.com/carvil/hflat
|
82
|
+
licenses:
|
83
|
+
- MIT
|
84
|
+
post_install_message:
|
85
|
+
rdoc_options: []
|
86
|
+
|
87
|
+
require_paths:
|
88
|
+
- lib
|
89
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
90
|
+
none: false
|
91
|
+
requirements:
|
92
|
+
- - ">="
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
hash: 1659928154997338558
|
95
|
+
segments:
|
96
|
+
- 0
|
97
|
+
version: "0"
|
98
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
99
|
+
none: false
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: "0"
|
104
|
+
requirements: []
|
105
|
+
|
106
|
+
rubyforge_project:
|
107
|
+
rubygems_version: 1.6.2
|
108
|
+
signing_key:
|
109
|
+
specification_version: 3
|
110
|
+
summary: Flatten a hash table
|
111
|
+
test_files: []
|
112
|
+
|