sc-core-ext 1.1.1 → 1.2.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/History.txt +32 -30
- data/Manifest.txt +31 -29
- data/PostInstall.txt +7 -7
- data/README.rdoc +40 -40
- data/Rakefile +15 -1
- data/lib/sc-core-ext.rb +16 -15
- data/lib/sc-core-ext/active_support/ordered_hash.rb +7 -7
- data/lib/sc-core-ext/array.rb +23 -23
- data/lib/sc-core-ext/dependencies.rb +6 -6
- data/lib/sc-core-ext/float.rb +20 -20
- data/lib/sc-core-ext/hash.rb +84 -66
- data/lib/sc-core-ext/numeric.rb +10 -0
- data/lib/sc-core-ext/object.rb +19 -19
- data/lib/sc-core-ext/regexp.rb +14 -14
- data/lib/sc-core-ext/string.rb +44 -44
- data/script/console +9 -9
- data/script/destroy +14 -14
- data/script/generate +14 -14
- data/spec/rcov.opts +2 -2
- data/spec/sc-core-ext/active_support/ordered_hash_spec.rb +16 -16
- data/spec/sc-core-ext/array_spec.rb +41 -41
- data/spec/sc-core-ext/date_time_spec.rb +15 -15
- data/spec/sc-core-ext/float_spec.rb +28 -28
- data/spec/sc-core-ext/hash_spec.rb +53 -39
- data/spec/sc-core-ext/numeric_spec.rb +24 -0
- data/spec/sc-core-ext/object_spec.rb +28 -28
- data/spec/sc-core-ext/regexp_spec.rb +17 -17
- data/spec/sc-core-ext/string_spec.rb +56 -56
- data/spec/spec.opts +4 -4
- metadata +9 -7
data/History.txt
CHANGED
@@ -1,30 +1,32 @@
|
|
1
|
-
=== 1.
|
2
|
-
|
3
|
-
*
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
*
|
11
|
-
*
|
12
|
-
*
|
13
|
-
*
|
14
|
-
* String#
|
15
|
-
* String#
|
16
|
-
*
|
17
|
-
|
18
|
-
*
|
19
|
-
|
20
|
-
|
21
|
-
*
|
22
|
-
|
23
|
-
*
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
1
|
+
=== 1.2.0 2010-04-30
|
2
|
+
* Added some methods to Hash:
|
3
|
+
* #stringify_values!
|
4
|
+
* #stringify_values
|
5
|
+
* #keys?
|
6
|
+
|
7
|
+
=== 1.1.0 2010-03-30
|
8
|
+
|
9
|
+
* Merged some of my other core extensions:
|
10
|
+
* Array#deep_dup
|
11
|
+
* Array#%
|
12
|
+
* Hash#deep_dup
|
13
|
+
* Object#dup?
|
14
|
+
* String#parenthesize
|
15
|
+
* String#depunctuate
|
16
|
+
* String#indent
|
17
|
+
* String#hex_to_bin
|
18
|
+
* DateTime#to_i / DateTime#to_f
|
19
|
+
* https://rails.lighthouseapp.com/projects/8994/tickets/3511-to_yaml-fails-for-datetime-objects
|
20
|
+
* Object#attr_boolean
|
21
|
+
* ActiveSupport::OrderedHash
|
22
|
+
* patched to support #to_yaml, unless patch is already applied
|
23
|
+
* Float#to_frac
|
24
|
+
|
25
|
+
* 2 minor enhancements:
|
26
|
+
* Hash and array methods now call self.class.new instead of {} or [], so that their descendants work as expected.
|
27
|
+
* Hash#camelize_keys saw some improvement by removing redundant calls to #to_s
|
28
|
+
|
29
|
+
=== 1.0.0 2010-03-18
|
30
|
+
|
31
|
+
* 1 major enhancement:
|
32
|
+
* Initial release
|
data/Manifest.txt
CHANGED
@@ -1,29 +1,31 @@
|
|
1
|
-
History.txt
|
2
|
-
Manifest.txt
|
3
|
-
PostInstall.txt
|
4
|
-
README.rdoc
|
5
|
-
Rakefile
|
6
|
-
lib/sc-core-ext.rb
|
7
|
-
lib/sc-core-ext/active_support/ordered_hash.rb
|
8
|
-
lib/sc-core-ext/array.rb
|
9
|
-
lib/sc-core-ext/date_time.rb
|
10
|
-
lib/sc-core-ext/dependencies.rb
|
11
|
-
lib/sc-core-ext/float.rb
|
12
|
-
lib/sc-core-ext/hash.rb
|
13
|
-
lib/sc-core-ext/
|
14
|
-
lib/sc-core-ext/
|
15
|
-
lib/sc-core-ext/
|
16
|
-
|
17
|
-
script/
|
18
|
-
script/
|
19
|
-
|
20
|
-
spec/
|
21
|
-
spec/sc-core-ext/
|
22
|
-
spec/sc-core-ext/
|
23
|
-
spec/sc-core-ext/
|
24
|
-
spec/sc-core-ext/
|
25
|
-
spec/sc-core-ext/
|
26
|
-
spec/sc-core-ext/
|
27
|
-
spec/sc-core-ext/
|
28
|
-
spec/
|
29
|
-
spec/
|
1
|
+
History.txt
|
2
|
+
Manifest.txt
|
3
|
+
PostInstall.txt
|
4
|
+
README.rdoc
|
5
|
+
Rakefile
|
6
|
+
lib/sc-core-ext.rb
|
7
|
+
lib/sc-core-ext/active_support/ordered_hash.rb
|
8
|
+
lib/sc-core-ext/array.rb
|
9
|
+
lib/sc-core-ext/date_time.rb
|
10
|
+
lib/sc-core-ext/dependencies.rb
|
11
|
+
lib/sc-core-ext/float.rb
|
12
|
+
lib/sc-core-ext/hash.rb
|
13
|
+
lib/sc-core-ext/numeric.rb
|
14
|
+
lib/sc-core-ext/object.rb
|
15
|
+
lib/sc-core-ext/regexp.rb
|
16
|
+
lib/sc-core-ext/string.rb
|
17
|
+
script/console
|
18
|
+
script/destroy
|
19
|
+
script/generate
|
20
|
+
spec/rcov.opts
|
21
|
+
spec/sc-core-ext/active_support/ordered_hash_spec.rb
|
22
|
+
spec/sc-core-ext/array_spec.rb
|
23
|
+
spec/sc-core-ext/date_time_spec.rb
|
24
|
+
spec/sc-core-ext/float_spec.rb
|
25
|
+
spec/sc-core-ext/hash_spec.rb
|
26
|
+
spec/sc-core-ext/numeric_spec.rb
|
27
|
+
spec/sc-core-ext/object_spec.rb
|
28
|
+
spec/sc-core-ext/regexp_spec.rb
|
29
|
+
spec/sc-core-ext/string_spec.rb
|
30
|
+
spec/spec.opts
|
31
|
+
spec/spec_helper.rb
|
data/PostInstall.txt
CHANGED
@@ -1,7 +1,7 @@
|
|
1
|
-
|
2
|
-
For more information on sc-core-ext, see http://sc-core-ext.rubyforge.org
|
3
|
-
|
4
|
-
NOTE: Change this information in PostInstall.txt
|
5
|
-
You can also delete it if you don't want it.
|
6
|
-
|
7
|
-
|
1
|
+
|
2
|
+
For more information on sc-core-ext, see http://sc-core-ext.rubyforge.org
|
3
|
+
|
4
|
+
NOTE: Change this information in PostInstall.txt
|
5
|
+
You can also delete it if you don't want it.
|
6
|
+
|
7
|
+
|
data/README.rdoc
CHANGED
@@ -1,41 +1,41 @@
|
|
1
|
-
= sc-core-ext
|
2
|
-
|
3
|
-
* http://github.com/sinisterchipmunk/core-ext
|
4
|
-
|
5
|
-
== DESCRIPTION:
|
6
|
-
|
7
|
-
A collection of core extensions for Ruby that I've built up over time. Most of my projects will probably end up
|
8
|
-
requiring this as a dependency.
|
9
|
-
|
10
|
-
== REQUIREMENTS:
|
11
|
-
|
12
|
-
* ActiveSupport 2.3.5+
|
13
|
-
|
14
|
-
== INSTALL:
|
15
|
-
|
16
|
-
* sudo gem install sc-core-ext
|
17
|
-
|
18
|
-
== LICENSE:
|
19
|
-
|
20
|
-
(The MIT License)
|
21
|
-
|
22
|
-
Copyright (c) 2010 FIXME Colin MacKenzie IV
|
23
|
-
|
24
|
-
Permission is hereby granted, free of charge, to any person obtaining
|
25
|
-
a copy of this software and associated documentation files (the
|
26
|
-
'Software'), to deal in the Software without restriction, including
|
27
|
-
without limitation the rights to use, copy, modify, merge, publish,
|
28
|
-
distribute, sublicense, and/or sell copies of the Software, and to
|
29
|
-
permit persons to whom the Software is furnished to do so, subject to
|
30
|
-
the following conditions:
|
31
|
-
|
32
|
-
The above copyright notice and this permission notice shall be
|
33
|
-
included in all copies or substantial portions of the Software.
|
34
|
-
|
35
|
-
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
36
|
-
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
37
|
-
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
38
|
-
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
39
|
-
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
40
|
-
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
1
|
+
= sc-core-ext
|
2
|
+
|
3
|
+
* http://github.com/sinisterchipmunk/core-ext
|
4
|
+
|
5
|
+
== DESCRIPTION:
|
6
|
+
|
7
|
+
A collection of core extensions for Ruby that I've built up over time. Most of my projects will probably end up
|
8
|
+
requiring this as a dependency.
|
9
|
+
|
10
|
+
== REQUIREMENTS:
|
11
|
+
|
12
|
+
* ActiveSupport 2.3.5+
|
13
|
+
|
14
|
+
== INSTALL:
|
15
|
+
|
16
|
+
* sudo gem install sc-core-ext
|
17
|
+
|
18
|
+
== LICENSE:
|
19
|
+
|
20
|
+
(The MIT License)
|
21
|
+
|
22
|
+
Copyright (c) 2010 FIXME Colin MacKenzie IV
|
23
|
+
|
24
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
25
|
+
a copy of this software and associated documentation files (the
|
26
|
+
'Software'), to deal in the Software without restriction, including
|
27
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
28
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
29
|
+
permit persons to whom the Software is furnished to do so, subject to
|
30
|
+
the following conditions:
|
31
|
+
|
32
|
+
The above copyright notice and this permission notice shall be
|
33
|
+
included in all copies or substantial portions of the Software.
|
34
|
+
|
35
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
36
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
37
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
38
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
39
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
40
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
41
41
|
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
CHANGED
@@ -4,6 +4,10 @@ require 'hoe'
|
|
4
4
|
require 'fileutils'
|
5
5
|
require './lib/sc-core-ext'
|
6
6
|
|
7
|
+
def rcov_opts
|
8
|
+
IO.readlines("spec/rcov.opts").map {|l| l.chomp.split " "}.flatten
|
9
|
+
end
|
10
|
+
|
7
11
|
Hoe.plugin :newgem
|
8
12
|
# Hoe.plugin :website
|
9
13
|
# Hoe.plugin :cucumberfeatures
|
@@ -24,11 +28,21 @@ Rake::RDocTask.new(:docs) do |rdoc|
|
|
24
28
|
rdoc.rdoc_files.add(files)
|
25
29
|
rdoc.main = 'README.rdoc'
|
26
30
|
rdoc.title = 'sc-core-ext'
|
27
|
-
#rdoc.template = '/path/to/gems/allison-2.0/lib/allison'
|
28
31
|
rdoc.rdoc_dir = 'doc'
|
29
32
|
rdoc.options << '--line-numbers' << '--inline-source'
|
30
33
|
end
|
31
34
|
|
35
|
+
namespace :spec do
|
36
|
+
desc "Run all specs with rcov"
|
37
|
+
Spec::Rake::SpecTask.new(:rcov) do |t|
|
38
|
+
t.spec_files = FileList['spec/**/*_spec.rb']
|
39
|
+
t.spec_opts = ['--options', 'spec/spec.opts']
|
40
|
+
t.rcov = true
|
41
|
+
t.rcov_dir = 'coverage'
|
42
|
+
t.rcov_opts = rcov_opts
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
32
46
|
require 'newgem/tasks'
|
33
47
|
Dir['tasks/**/*.rake'].each { |t| load t }
|
34
48
|
|
data/lib/sc-core-ext.rb
CHANGED
@@ -1,15 +1,16 @@
|
|
1
|
-
$:.unshift(File.dirname(__FILE__)) unless $:.include?(File.dirname(__FILE__))
|
2
|
-
|
3
|
-
require 'sc-core-ext/dependencies'
|
4
|
-
require 'sc-core-ext/object'
|
5
|
-
require 'sc-core-ext/hash'
|
6
|
-
require 'sc-core-ext/string'
|
7
|
-
require 'sc-core-ext/regexp'
|
8
|
-
require 'sc-core-ext/array'
|
9
|
-
require 'sc-core-ext/date_time'
|
10
|
-
require 'sc-core-ext/active_support/ordered_hash'
|
11
|
-
require 'sc-core-ext/float'
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
1
|
+
$:.unshift(File.dirname(__FILE__)) unless $:.include?(File.dirname(__FILE__))
|
2
|
+
|
3
|
+
require 'sc-core-ext/dependencies'
|
4
|
+
require 'sc-core-ext/object'
|
5
|
+
require 'sc-core-ext/hash'
|
6
|
+
require 'sc-core-ext/string'
|
7
|
+
require 'sc-core-ext/regexp'
|
8
|
+
require 'sc-core-ext/array'
|
9
|
+
require 'sc-core-ext/date_time'
|
10
|
+
require 'sc-core-ext/active_support/ordered_hash'
|
11
|
+
require 'sc-core-ext/float'
|
12
|
+
require 'sc-core-ext/numeric'
|
13
|
+
|
14
|
+
module ScCoreExt
|
15
|
+
VERSION = '1.2.0'
|
16
|
+
end
|
@@ -1,7 +1,7 @@
|
|
1
|
-
unless ActiveSupport::OrderedHash.methods.include?("yaml_initialize")
|
2
|
-
class ActiveSupport::OrderedHash
|
3
|
-
def yaml_initialize(type, val)
|
4
|
-
replace(val)
|
5
|
-
end
|
6
|
-
end
|
7
|
-
end
|
1
|
+
unless ActiveSupport::OrderedHash.methods.include?("yaml_initialize")
|
2
|
+
class ActiveSupport::OrderedHash
|
3
|
+
def yaml_initialize(type, val)
|
4
|
+
replace(val)
|
5
|
+
end
|
6
|
+
end
|
7
|
+
end
|
data/lib/sc-core-ext/array.rb
CHANGED
@@ -1,23 +1,23 @@
|
|
1
|
-
class Array
|
2
|
-
# Performs a "deep copy" of this array; that is, returns an Array that is a duplicate of this Array, and whose
|
3
|
-
# elements have each, in turn, had #deep_dup or #dup called on them. This should produce an Array whose every
|
4
|
-
# element is a copy of the original.
|
5
|
-
#
|
6
|
-
# This operation is expensive, and should be used sparingly.
|
7
|
-
def deep_dup
|
8
|
-
self.collect do |ele|
|
9
|
-
ele.respond_to?(:deep_dup) ? ele.deep_dup : ele.dup?
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
|
-
# Modulus operator: returns an array consisting only of those elements that are shared by both this array and
|
14
|
-
# the arr object. If arr is not an array, then only elements equal to arr itself are returned.
|
15
|
-
#
|
16
|
-
# Examples:
|
17
|
-
# [1, 2, 3] % [1, 2] # => [1, 2]
|
18
|
-
# [1, 2, 3] % 2 # => [2]
|
19
|
-
#
|
20
|
-
def %(arr)
|
21
|
-
select { |i| arr.kind_of?(Array) ? arr.include?(i) : arr == i }
|
22
|
-
end
|
23
|
-
end
|
1
|
+
class Array
|
2
|
+
# Performs a "deep copy" of this array; that is, returns an Array that is a duplicate of this Array, and whose
|
3
|
+
# elements have each, in turn, had #deep_dup or #dup called on them. This should produce an Array whose every
|
4
|
+
# element is a copy of the original.
|
5
|
+
#
|
6
|
+
# This operation is expensive, and should be used sparingly.
|
7
|
+
def deep_dup
|
8
|
+
self.collect do |ele|
|
9
|
+
ele.respond_to?(:deep_dup) ? ele.deep_dup : ele.dup?
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
# Modulus operator: returns an array consisting only of those elements that are shared by both this array and
|
14
|
+
# the arr object. If arr is not an array, then only elements equal to arr itself are returned.
|
15
|
+
#
|
16
|
+
# Examples:
|
17
|
+
# [1, 2, 3] % [1, 2] # => [1, 2]
|
18
|
+
# [1, 2, 3] % 2 # => [2]
|
19
|
+
#
|
20
|
+
def %(arr)
|
21
|
+
select { |i| arr.kind_of?(Array) ? arr.include?(i) : arr == i }
|
22
|
+
end
|
23
|
+
end
|
@@ -1,6 +1,6 @@
|
|
1
|
-
unless defined?(Gem)
|
2
|
-
require 'rubygems'
|
3
|
-
gem 'activesupport', '>= 2.3.5'
|
4
|
-
end
|
5
|
-
|
6
|
-
require 'active_support/core_ext'
|
1
|
+
unless defined?(Gem)
|
2
|
+
require 'rubygems'
|
3
|
+
gem 'activesupport', '>= 2.3.5'
|
4
|
+
end
|
5
|
+
|
6
|
+
require 'active_support/core_ext'
|
data/lib/sc-core-ext/float.rb
CHANGED
@@ -1,20 +1,20 @@
|
|
1
|
-
class Float
|
2
|
-
def to_frac
|
3
|
-
fraction = case self.abs%1.0
|
4
|
-
when 0 : ''
|
5
|
-
when 0.25 : '¼'
|
6
|
-
when 0.5 : '½'
|
7
|
-
when 0.75 : '¾'
|
8
|
-
end
|
9
|
-
if fraction
|
10
|
-
body = case self.floor
|
11
|
-
when -1 : '-'
|
12
|
-
when 0 : ''
|
13
|
-
else self.to_i.to_s
|
14
|
-
end
|
15
|
-
body + fraction
|
16
|
-
else
|
17
|
-
self.to_s
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
1
|
+
class Float
|
2
|
+
def to_frac
|
3
|
+
fraction = case self.abs%1.0
|
4
|
+
when 0 : ''
|
5
|
+
when 0.25 : '¼'
|
6
|
+
when 0.5 : '½'
|
7
|
+
when 0.75 : '¾'
|
8
|
+
end
|
9
|
+
if fraction
|
10
|
+
body = case self.floor
|
11
|
+
when -1 : '-'
|
12
|
+
when 0 : ''
|
13
|
+
else self.to_i.to_s
|
14
|
+
end
|
15
|
+
body + fraction
|
16
|
+
else
|
17
|
+
self.to_s
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
data/lib/sc-core-ext/hash.rb
CHANGED
@@ -1,66 +1,84 @@
|
|
1
|
-
class Hash
|
2
|
-
#
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
inject(self.class.new) do |
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
def
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
#
|
51
|
-
#
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
1
|
+
class Hash
|
2
|
+
# As the ActiveSupport extension #stringify_keys! except that it's applied to values instead.
|
3
|
+
def stringify_values!
|
4
|
+
inject(self.class.new) do |new_hash, (key, value)|
|
5
|
+
new_hash[key] = value.kind_of?(String) ? value : value.to_s
|
6
|
+
new_hash
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
def stringify_values
|
11
|
+
dup.stringify_values!
|
12
|
+
end
|
13
|
+
|
14
|
+
# Returns true if #key? would return true for each item specified.
|
15
|
+
def keys?(*items)
|
16
|
+
items.flatten.each { |item| return false unless key?(item) }
|
17
|
+
true
|
18
|
+
end
|
19
|
+
|
20
|
+
# Performs a "deep copy" of this hash; that is, returns a Hash that is a duplicate of this Hash, and whose
|
21
|
+
# keys and values have each, in turn, had #deep_dup or #dup called on them. This should produce a Hash whose every
|
22
|
+
# element is a copy of the original.
|
23
|
+
#
|
24
|
+
# This operation is expensive, and should be used sparingly.
|
25
|
+
def deep_dup
|
26
|
+
inject(self.class.new) do |new_hash, (key, value)|
|
27
|
+
key = key.respond_to?(:deep_dup) ? key.deep_dup : key.dup?
|
28
|
+
value = value.respond_to?(:deep_dup) ? value.deep_dup : value.dup?
|
29
|
+
new_hash[key] = value
|
30
|
+
new_hash
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def without(*keys)
|
35
|
+
keys.flatten!
|
36
|
+
inject(self.class.new) do |hash, (key, value)|
|
37
|
+
hash[key] = value unless keys.include?(key)
|
38
|
+
hash
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def without_values(*values)
|
43
|
+
values.flatten!
|
44
|
+
inject(self.class.new) do |hash, (key, value)|
|
45
|
+
hash[key] = value unless values.include?(value)
|
46
|
+
hash
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
# Returns a hash that is a copy of this one, except that all nil values have been removed, making them
|
51
|
+
# essentially "optional" keys.
|
52
|
+
def optionalize
|
53
|
+
without_values(nil)
|
54
|
+
end
|
55
|
+
|
56
|
+
alias without_nil_values optionalize
|
57
|
+
|
58
|
+
def camelize_keys
|
59
|
+
stringified = stringify_keys
|
60
|
+
stringified.rename(stringified.inject(self.class.new) do |renamed, (key, value)|
|
61
|
+
renamed[key] = key.camelize
|
62
|
+
renamed
|
63
|
+
end)
|
64
|
+
end
|
65
|
+
|
66
|
+
# Takes a hash whose keys must match keys in this hash. Those keys will be renamed to match the
|
67
|
+
# corresponding value in the specified hash.
|
68
|
+
#
|
69
|
+
# Keys not found are ignored.
|
70
|
+
#
|
71
|
+
# Returns self.
|
72
|
+
#
|
73
|
+
# Example:
|
74
|
+
# { :a => 1 }.rename(:a => :b)
|
75
|
+
# => {:b => 1}
|
76
|
+
#
|
77
|
+
def rename(to)
|
78
|
+
merge!(inject(self.class.new) do |hash, (old_key, value)|
|
79
|
+
hash[to[old_key] || old_key] = value
|
80
|
+
delete(old_key)
|
81
|
+
hash
|
82
|
+
end)
|
83
|
+
end
|
84
|
+
end
|