hash-keys 1.0.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/Rakefile +113 -0
- data/hash-keys.gemspec +29 -0
- data/lib/hash-keys.rb +133 -0
- data/lib/hash-keys/version.rb +3 -0
- metadata +51 -0
data/Rakefile
ADDED
@@ -0,0 +1,113 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
require 'date'
|
4
|
+
|
5
|
+
#############################################################################
|
6
|
+
#
|
7
|
+
# Helper functions
|
8
|
+
#
|
9
|
+
#############################################################################
|
10
|
+
|
11
|
+
def name
|
12
|
+
@name ||= Dir['*.gemspec'].first.split('.').first
|
13
|
+
end
|
14
|
+
|
15
|
+
def version
|
16
|
+
line = File.read("lib/#{name}/version.rb")[/^\s*VERSION\s*=\s*.*/]
|
17
|
+
line.match(/.*VERSION\s*=\s*['"](.*)['"]/)[1]
|
18
|
+
end
|
19
|
+
|
20
|
+
def date
|
21
|
+
Date.today.to_s
|
22
|
+
end
|
23
|
+
|
24
|
+
def rubyforge_project
|
25
|
+
name
|
26
|
+
end
|
27
|
+
|
28
|
+
def gemspec_file
|
29
|
+
"#{name}.gemspec"
|
30
|
+
end
|
31
|
+
|
32
|
+
def gem_file
|
33
|
+
"#{name}-#{version}.gem"
|
34
|
+
end
|
35
|
+
|
36
|
+
def replace_header(head, header_name)
|
37
|
+
head.sub!(/(\.#{header_name}\s*= ').*'/) { "#{$1}#{send(header_name)}'"}
|
38
|
+
end
|
39
|
+
|
40
|
+
#############################################################################
|
41
|
+
#
|
42
|
+
# Custom tasks
|
43
|
+
#
|
44
|
+
#############################################################################
|
45
|
+
|
46
|
+
|
47
|
+
#############################################################################
|
48
|
+
#
|
49
|
+
# Packaging tasks
|
50
|
+
#
|
51
|
+
#############################################################################
|
52
|
+
|
53
|
+
desc "Create tag v#{version} and build and push #{gem_file} to Rubygems"
|
54
|
+
task :release => :build do
|
55
|
+
unless `git branch` =~ /^\* master$/
|
56
|
+
puts "You must be on the master branch to release!"
|
57
|
+
exit!
|
58
|
+
end
|
59
|
+
sh "git commit --allow-empty -a -m 'Release #{version}'"
|
60
|
+
sh "git tag v#{version}"
|
61
|
+
sh "git push origin master"
|
62
|
+
sh "git push origin v#{version}"
|
63
|
+
sh "gem push pkg/#{name}-#{version}.gem"
|
64
|
+
end
|
65
|
+
|
66
|
+
desc "Build #{gem_file} into the pkg directory"
|
67
|
+
task :build => :gemspec do
|
68
|
+
sh "mkdir -p pkg"
|
69
|
+
sh "gem build #{gemspec_file}"
|
70
|
+
sh "mv #{gem_file} pkg"
|
71
|
+
end
|
72
|
+
|
73
|
+
desc "Generate #{gemspec_file}"
|
74
|
+
task :gemspec => :validate do
|
75
|
+
# read spec file and split out manifest section
|
76
|
+
spec = File.read(gemspec_file)
|
77
|
+
head, manifest, tail = spec.split(" # = MANIFEST =\n")
|
78
|
+
|
79
|
+
# replace name version and date
|
80
|
+
replace_header(head, :name)
|
81
|
+
replace_header(head, :version)
|
82
|
+
replace_header(head, :date)
|
83
|
+
#comment this out if your rubyforge_project has a different name
|
84
|
+
#replace_header(head, :rubyforge_project)
|
85
|
+
|
86
|
+
# determine file list from git ls-files
|
87
|
+
files = `git ls-files`.
|
88
|
+
split("\n").
|
89
|
+
sort.
|
90
|
+
reject { |file| file =~ /^\./ }.
|
91
|
+
reject { |file| file =~ /^(rdoc|pkg)/ }.
|
92
|
+
map { |file| " #{file}" }.
|
93
|
+
join("\n")
|
94
|
+
|
95
|
+
# piece file back together and write
|
96
|
+
manifest = " s.files = %w[\n#{files}\n ]\n"
|
97
|
+
spec = [head, manifest, tail].join(" # = MANIFEST =\n")
|
98
|
+
File.open(gemspec_file, 'w') { |io| io.write(spec) }
|
99
|
+
puts "Updated #{gemspec_file}"
|
100
|
+
end
|
101
|
+
|
102
|
+
desc "Validate #{gemspec_file}"
|
103
|
+
task :validate do
|
104
|
+
libfiles = Dir['lib/*'] - ["lib/#{name}.rb", "lib/#{name}"]
|
105
|
+
unless libfiles.empty?
|
106
|
+
puts "Directory `lib` should only contain a `#{name}.rb` file and `#{name}` dir."
|
107
|
+
exit!
|
108
|
+
end
|
109
|
+
unless Dir['VERSION*'].empty?
|
110
|
+
puts "A `VERSION` file at root level violates Gem best practices."
|
111
|
+
exit!
|
112
|
+
end
|
113
|
+
end
|
data/hash-keys.gemspec
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
#$:.push File.expand_path("../lib", __FILE__)
|
2
|
+
#require "hash-keys/version"
|
3
|
+
|
4
|
+
Gem::Specification.new do |s|
|
5
|
+
s.name = 'hash-keys'
|
6
|
+
s.version = '1.0.0'
|
7
|
+
s.date = '2011-09-14'
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ['Lee Henson', 'Guy Boertje']
|
10
|
+
s.email = ['lee.m.henson@gmail.com', 'guyboertje@gmail.com']
|
11
|
+
s.homepage = ""
|
12
|
+
s.summary = %q{}
|
13
|
+
s.description = %q{}
|
14
|
+
## Leave this section as-is. It will be automatically generated from the
|
15
|
+
## contents of your Git repository via the gemspec task. DO NOT REMOVE
|
16
|
+
## THE MANIFEST COMMENTS, they are used as delimiters by the task.
|
17
|
+
# = MANIFEST =
|
18
|
+
s.files = %w[
|
19
|
+
Rakefile
|
20
|
+
hash-keys.gemspec
|
21
|
+
lib/hash-keys.rb
|
22
|
+
lib/hash-keys/version.rb
|
23
|
+
]
|
24
|
+
# = MANIFEST =
|
25
|
+
|
26
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
27
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
28
|
+
s.require_paths = ["lib"]
|
29
|
+
end
|
data/lib/hash-keys.rb
ADDED
@@ -0,0 +1,133 @@
|
|
1
|
+
module HashKeys
|
2
|
+
def has_keys?(*keys)
|
3
|
+
!keys.any? { |key| !self.has_key? key }
|
4
|
+
end
|
5
|
+
end
|
6
|
+
|
7
|
+
module HashKeysRmerge
|
8
|
+
# Recursive merge functions by Simone Carletti
|
9
|
+
# MIT License
|
10
|
+
# See: https://gist.github.com/6391
|
11
|
+
#
|
12
|
+
# Recursive version of Hash#merge!
|
13
|
+
#
|
14
|
+
# Adds the contents of +other_hash+ to +hsh+,
|
15
|
+
# merging entries in +hsh+ with duplicate keys with those from +other_hash+.
|
16
|
+
#
|
17
|
+
# Compared with Hash#merge!, this method supports nested hashes.
|
18
|
+
# When both +hsh+ and +other_hash+ contains an entry with the same key,
|
19
|
+
# it merges and returns the values from both arrays.
|
20
|
+
#
|
21
|
+
# h1 = {"a" => 100, "b" => 200, "c" => {"c1" => 12, "c2" => 14}}
|
22
|
+
# h2 = {"b" => 254, "c" => {"c1" => 16, "c3" => 94}}
|
23
|
+
# h1.rmerge!(h2) #=> {"a" => 100, "b" => 254, "c" => {"c1" => 16, "c2" => 14, "c3" => 94}}
|
24
|
+
#
|
25
|
+
# Simply using Hash#merge! would return
|
26
|
+
#
|
27
|
+
# h1.merge!(h2) #=> {"a" => 100, "b" = >254, "c" => {"c1" => 16, "c3" => 94}}
|
28
|
+
#
|
29
|
+
def rmerge!(other_hash)
|
30
|
+
merge!(other_hash) do |key, oldval, newval|
|
31
|
+
oldval.class == self.class ? oldval.rmerge!(newval) : newval
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
#
|
36
|
+
# Recursive version of Hash#merge
|
37
|
+
#
|
38
|
+
# Compared with Hash#merge!, this method supports nested hashes.
|
39
|
+
# When both +hsh+ and +other_hash+ contains an entry with the same key,
|
40
|
+
# it merges and returns the values from both arrays.
|
41
|
+
#
|
42
|
+
# Compared with Hash#merge, this method provides a different approch
|
43
|
+
# for merging nasted hashes.
|
44
|
+
# If the value of a given key is an Hash and both +other_hash+ abd +hsh
|
45
|
+
# includes the same key, the value is merged instead replaced with
|
46
|
+
# +other_hash+ value.
|
47
|
+
#
|
48
|
+
# h1 = {"a" => 100, "b" => 200, "c" => {"c1" => 12, "c2" => 14}}
|
49
|
+
# h2 = {"b" => 254, "c" => {"c1" => 16, "c3" => 94}}
|
50
|
+
# h1.rmerge(h2) #=> {"a" => 100, "b" => 254, "c" => {"c1" => 16, "c2" => 14, "c3" => 94}}
|
51
|
+
#
|
52
|
+
# Simply using Hash#merge would return
|
53
|
+
#
|
54
|
+
# h1.merge(h2) #=> {"a" => 100, "b" = >254, "c" => {"c1" => 16, "c3" => 94}}
|
55
|
+
#
|
56
|
+
def rmerge(other_hash)
|
57
|
+
r = {}
|
58
|
+
merge(other_hash) do |key, oldval, newval|
|
59
|
+
r[key] = oldval.class == self.class ? oldval.rmerge(newval) : newval
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
module HashKeysString
|
65
|
+
# Return a new hash with all keys converted to strings.
|
66
|
+
def stringify__keys
|
67
|
+
dup.stringify__keys!
|
68
|
+
end
|
69
|
+
alias_method :stringify_keys, :stringify__keys
|
70
|
+
|
71
|
+
# Destructively convert all keys to strings.
|
72
|
+
def stringify__keys!
|
73
|
+
keys.each do |key|
|
74
|
+
self[key.to_s] = delete(key)
|
75
|
+
end
|
76
|
+
self
|
77
|
+
end
|
78
|
+
alias_method :stringify_keys!, :stringify__keys!
|
79
|
+
|
80
|
+
def stringify__symbol__values!
|
81
|
+
keys.each do |key|
|
82
|
+
self[key] = self[key].to_s if self[key].is_a? Symbol
|
83
|
+
end
|
84
|
+
self
|
85
|
+
end
|
86
|
+
alias_method :stringify_symbol_values!, :stringify__symbol__values!
|
87
|
+
|
88
|
+
def recursive__stringify__keys!
|
89
|
+
stringify_keys!
|
90
|
+
values.select{|v| v.is_a? Hash}.each{|h| h.recursive__stringify__keys!}
|
91
|
+
self
|
92
|
+
end
|
93
|
+
alias_method :recursive_stringify_keys!, :recursive__stringify__keys!
|
94
|
+
|
95
|
+
def recursive__stringify__symbol__values!
|
96
|
+
stringify__symbol__values!
|
97
|
+
values.select{|v| v.is_a? Hash}.each{|h| h.recursive__stringify__symbol__values!}
|
98
|
+
self
|
99
|
+
end
|
100
|
+
alias_method :recursive_stringify_symbol_values!, :recursive__stringify__symbol__values!
|
101
|
+
end
|
102
|
+
|
103
|
+
module HashKeysSymbol
|
104
|
+
# Return a new hash with all keys converted to symbols, as long as
|
105
|
+
# they respond to +to_sym+.
|
106
|
+
def symbolize__keys
|
107
|
+
dup.symbolize__keys!
|
108
|
+
end
|
109
|
+
alias_method :symbolize_keys, :symbolize__keys
|
110
|
+
alias_method :to_options, :symbolize__keys
|
111
|
+
# Destructively convert all keys to symbols, as long as they respond
|
112
|
+
# to +to_sym+.
|
113
|
+
def symbolize__keys!
|
114
|
+
keys.each do |key|
|
115
|
+
self[(key.to_sym rescue key) || key] = delete(key)
|
116
|
+
end
|
117
|
+
self
|
118
|
+
end
|
119
|
+
alias_method :symbolize_keys!, :symbolize__keys!
|
120
|
+
alias_method :to_options!, :symbolize__keys!
|
121
|
+
|
122
|
+
def recursive__symbolize__keys!
|
123
|
+
symbolize__keys!
|
124
|
+
values.select{|v| v.is_a? Hash}.each{|h| h.recursive__symbolize__keys!}
|
125
|
+
self
|
126
|
+
end
|
127
|
+
alias_method :recursive_symbolize_keys!, :recursive__symbolize__keys!
|
128
|
+
end
|
129
|
+
|
130
|
+
Hash.send(:include, HashKeys) unless Hash.respond_to? :has_keys?
|
131
|
+
Hash.send(:include, HashKeysRmerge) unless Hash.respond_to? :rmerge!
|
132
|
+
Hash.send(:include, HashKeysString) unless Hash.respond_to? :stringify__keys!
|
133
|
+
Hash.send(:include, HashKeysSymbol) unless Hash.respond_to? :symbolize__keys!
|
metadata
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hash-keys
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Lee Henson
|
9
|
+
- Guy Boertje
|
10
|
+
autorequire:
|
11
|
+
bindir: bin
|
12
|
+
cert_chain: []
|
13
|
+
date: 2011-09-14 00:00:00.000000000 Z
|
14
|
+
dependencies: []
|
15
|
+
description: ''
|
16
|
+
email:
|
17
|
+
- lee.m.henson@gmail.com
|
18
|
+
- guyboertje@gmail.com
|
19
|
+
executables: []
|
20
|
+
extensions: []
|
21
|
+
extra_rdoc_files: []
|
22
|
+
files:
|
23
|
+
- Rakefile
|
24
|
+
- hash-keys.gemspec
|
25
|
+
- lib/hash-keys.rb
|
26
|
+
- lib/hash-keys/version.rb
|
27
|
+
homepage: ''
|
28
|
+
licenses: []
|
29
|
+
post_install_message:
|
30
|
+
rdoc_options: []
|
31
|
+
require_paths:
|
32
|
+
- lib
|
33
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
34
|
+
none: false
|
35
|
+
requirements:
|
36
|
+
- - ! '>='
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: '0'
|
39
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ! '>='
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: '0'
|
45
|
+
requirements: []
|
46
|
+
rubyforge_project:
|
47
|
+
rubygems_version: 1.8.6
|
48
|
+
signing_key:
|
49
|
+
specification_version: 3
|
50
|
+
summary: ''
|
51
|
+
test_files: []
|