hash-utils 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,11 @@
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 "bundler", "~> 1.0.0"
10
+ gem "jeweler", "~> 1.5.2"
11
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,16 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ git (1.2.5)
5
+ jeweler (1.5.2)
6
+ bundler (~> 1.0.0)
7
+ git (>= 1.2.5)
8
+ rake
9
+ rake (0.8.7)
10
+
11
+ PLATFORMS
12
+ ruby
13
+
14
+ DEPENDENCIES
15
+ bundler (~> 1.0.0)
16
+ jeweler (~> 1.5.2)
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Martin Kozák
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.md ADDED
@@ -0,0 +1,51 @@
1
+ Hash Utils
2
+ ==========
3
+
4
+ **Hash Utils** adds some utility methods well known from [Array][1]
5
+ class to [Hash][2] class. Extends it with following methods:
6
+
7
+ * [`#compact`][3] – rejects elements with `nil` values,
8
+ * [`#compact!`][4]
9
+ * `#map_pairs` – works as [Array#map][5], but for *whole pair*,
10
+ * `#map_pairs!`
11
+ * `#map_keys` – works as [Array#map][5], but for *keys only*,
12
+ * `#map_keys!`
13
+ * `#keys_to_sym` – converts all keys to [Symbols][6],
14
+ * `#keys_to_sym`
15
+
16
+ All methods with `!` emulates work *in place*, but in fact they will
17
+ replace old hash with new one. An example of use:
18
+
19
+ foo = {"a" => 1, "b" => 2}
20
+ foo.map_keys! { |k| k.to_sym }
21
+
22
+ # result will be
23
+ # {:a => 1, :b => 2}
24
+
25
+ This is the same as `#keys_to_sym!` for example.
26
+
27
+ Contributing
28
+ ------------
29
+
30
+ 1. Fork it.
31
+ 2. Create a branch (`git checkout -b 20101220-my-change`).
32
+ 3. Commit your changes (`git commit -am "Added something"`).
33
+ 4. Push to the branch (`git push origin 20101220-my-change`).
34
+ 5. Create an [Issue][7] with a link to your branch.
35
+ 6. Enjoy a refreshing Diet Coke and wait.
36
+
37
+
38
+ Copyright
39
+ ---------
40
+
41
+ Copyright (c) 2011 [Martin Kozák][8]. See `LICENSE.txt` for
42
+ further details.
43
+
44
+ [1]: http://www.ruby-doc.org/core/classes/Array.html
45
+ [2]: http://www.ruby-doc.org/core/classes/Hash.html
46
+ [3]: http://www.ruby-doc.org/core/classes/Array.html#M000278
47
+ [4]: http://www.ruby-doc.org/core/classes/Array.html#M000279
48
+ [5]: http://www.ruby-doc.org/core/classes/Array.html#M000249
49
+ [6]: http://www.ruby-doc.org/core/classes/Symbol.html
50
+ [7]: http://github.com/martinkozak/hash-utils/issues
51
+ [8]: http://www.martinkozak.net/
data/Rakefile ADDED
@@ -0,0 +1,37 @@
1
+ # encoding: utf-8
2
+ require 'rubygems'
3
+ require 'bundler'
4
+ begin
5
+ Bundler.setup(:default, :development)
6
+ rescue Bundler::BundlerError => e
7
+ $stderr.puts e.message
8
+ $stderr.puts "Run `bundle install` to install missing gems"
9
+ exit e.status_code
10
+ end
11
+ require 'rake'
12
+
13
+ require 'jeweler'
14
+ Jeweler::Tasks.new do |gem|
15
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
16
+ gem.name = "hash-utils"
17
+ gem.homepage = "http://github.com/martinkozak/hash-utils"
18
+ gem.license = "MIT"
19
+ gem.summary = 'Adds some useful well known methods similar to appropriate Array methods to Hash.'
20
+ gem.email = "martinkozak@martinkozak.net"
21
+ gem.authors = ["Martin Kozák"]
22
+ # Include your dependencies below. Runtime dependencies are required when using your gem,
23
+ # and development dependencies are only needed for development (ie running rake tasks, tests, etc)
24
+ # gem.add_runtime_dependency 'jabber4r', '> 0.1'
25
+ # gem.add_development_dependency 'rspec', '> 1.2.3'
26
+ end
27
+ Jeweler::RubygemsDotOrgTasks.new
28
+
29
+ require 'rake/rdoctask'
30
+ Rake::RDocTask.new do |rdoc|
31
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
32
+
33
+ rdoc.rdoc_dir = 'rdoc'
34
+ rdoc.title = "hash-utils #{version}"
35
+ rdoc.rdoc_files.include('README*')
36
+ rdoc.rdoc_files.include('lib/**/*.rb')
37
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,51 @@
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{hash-utils}
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 = ["Martin Kozák"]
12
+ s.date = %q{2011-01-02}
13
+ s.email = %q{martinkozak@martinkozak.net}
14
+ s.extra_rdoc_files = [
15
+ "LICENSE.txt",
16
+ "README.md"
17
+ ]
18
+ s.files = [
19
+ ".document",
20
+ "Gemfile",
21
+ "Gemfile.lock",
22
+ "LICENSE.txt",
23
+ "README.md",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "hash-utils.gemspec",
27
+ "lib/hash-utils.rb"
28
+ ]
29
+ s.homepage = %q{http://github.com/martinkozak/hash-utils}
30
+ s.licenses = ["MIT"]
31
+ s.require_paths = ["lib"]
32
+ s.rubygems_version = %q{1.3.7}
33
+ s.summary = %q{Adds some useful well known methods similar to appropriate Array methods to Hash.}
34
+
35
+ if s.respond_to? :specification_version then
36
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
37
+ s.specification_version = 3
38
+
39
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
40
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
41
+ s.add_development_dependency(%q<jeweler>, ["~> 1.5.2"])
42
+ else
43
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
44
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
45
+ end
46
+ else
47
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
48
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
49
+ end
50
+ end
51
+
data/lib/hash-utils.rb ADDED
@@ -0,0 +1,94 @@
1
+ # encoding: utf-8
2
+ # (c) 2011 Martin Kozák (martinkozak@martinkozak.net)
3
+
4
+ class Hash
5
+
6
+ ##
7
+ # Returns a copy of <tt>self</tt> with all <tt>nil</tt>
8
+ # elements removed.
9
+ #
10
+
11
+ def compact
12
+ self.reject { |k, v| v.nil? }
13
+ end
14
+
15
+ ##
16
+ # Removes <tt>nil</tt> elements from the hash. Returns <tt>nil</tt>
17
+ # if no changes were made, otherwise returns <tt>self</self>.
18
+ #
19
+
20
+ def compact!
21
+ self.reject! { |k, v| v.nil? }
22
+ end
23
+
24
+ ##
25
+ # Returns a new hash with the results of running block once for
26
+ # every pair in <tt>self</tt>.
27
+ #
28
+
29
+ def map_pairs(&block)
30
+ new = Hash::new(&self.default_proc)
31
+ new.default = self.default
32
+
33
+ self.each_pair do |k, v|
34
+ new_k, new_v = block.call(k, v)
35
+ new[new_k] = new_v
36
+ end
37
+
38
+ return new
39
+ end
40
+
41
+ alias :collect_pairs :map_pairs
42
+
43
+ ##
44
+ # Emulates #map_pairs on place. In fact, replaces old hash by
45
+ # new one.
46
+ #
47
+
48
+ def map_pairs!(&block)
49
+ self.replace(self.map_pairs(&block))
50
+ end
51
+
52
+ alias :"collect_pairs!" :"map_pairs!"
53
+
54
+ ##
55
+ # Returns a new hash with the results of running block once for
56
+ # every key in <tt>self</tt>.
57
+ #
58
+
59
+ def map_keys(&block)
60
+ self.map_pairs do |k, v|
61
+ [block.call(k), v]
62
+ end
63
+ end
64
+
65
+ alias :collect_keys :map_keys
66
+
67
+ ##
68
+ # Emulates #map_keys on place. In fact, replaces old hash by
69
+ # new one.
70
+ #
71
+
72
+ def map_keys!(&block)
73
+ self.replace(self.map_keys(&block))
74
+ end
75
+
76
+ alias :"collect_keys!" :"map_keys!"
77
+
78
+ ##
79
+ # Converts all keys to symbols.
80
+ #
81
+
82
+ def keys_to_sym
83
+ self.map_keys { |k| k.to_sym }
84
+ end
85
+
86
+ ##
87
+ # Emulates #keys_to_sym on place. In fact, replaces old hash by
88
+ # new one.
89
+ #
90
+
91
+ def keys_to_sym!
92
+ self.replace(self.keys_to_sym)
93
+ end
94
+ end
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hash-utils
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - "Martin Koz\xC3\xA1k"
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-01-02 00:00:00 +01:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: bundler
22
+ requirement: &id001 !ruby/object:Gem::Requirement
23
+ none: false
24
+ requirements:
25
+ - - ~>
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 1
29
+ - 0
30
+ - 0
31
+ version: 1.0.0
32
+ type: :development
33
+ prerelease: false
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: jeweler
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ~>
41
+ - !ruby/object:Gem::Version
42
+ segments:
43
+ - 1
44
+ - 5
45
+ - 2
46
+ version: 1.5.2
47
+ type: :development
48
+ prerelease: false
49
+ version_requirements: *id002
50
+ description:
51
+ email: martinkozak@martinkozak.net
52
+ executables: []
53
+
54
+ extensions: []
55
+
56
+ extra_rdoc_files:
57
+ - LICENSE.txt
58
+ - README.md
59
+ files:
60
+ - .document
61
+ - Gemfile
62
+ - Gemfile.lock
63
+ - LICENSE.txt
64
+ - README.md
65
+ - Rakefile
66
+ - VERSION
67
+ - hash-utils.gemspec
68
+ - lib/hash-utils.rb
69
+ has_rdoc: true
70
+ homepage: http://github.com/martinkozak/hash-utils
71
+ licenses:
72
+ - MIT
73
+ post_install_message:
74
+ rdoc_options: []
75
+
76
+ require_paths:
77
+ - lib
78
+ required_ruby_version: !ruby/object:Gem::Requirement
79
+ none: false
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ hash: 3564167343409950295
84
+ segments:
85
+ - 0
86
+ version: "0"
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ none: false
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ segments:
93
+ - 0
94
+ version: "0"
95
+ requirements: []
96
+
97
+ rubyforge_project:
98
+ rubygems_version: 1.3.7
99
+ signing_key:
100
+ specification_version: 3
101
+ summary: Adds some useful well known methods similar to appropriate Array methods to Hash.
102
+ test_files: []
103
+