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.
@@ -1,30 +1,32 @@
1
- === 1.1.1 2010-03-30
2
-
3
- * Forgot to update the manifest. Whoops.
4
-
5
- === 1.1.0 2010-03-30
6
-
7
- * Merged some of my other core extensions:
8
- * Array#deep_dup
9
- * Array#%
10
- * Hash#deep_dup
11
- * Object#dup?
12
- * String#parenthesize
13
- * String#depunctuate
14
- * String#indent
15
- * String#hex_to_bin
16
- * DateTime#to_i / DateTime#to_f
17
- * https://rails.lighthouseapp.com/projects/8994/tickets/3511-to_yaml-fails-for-datetime-objects
18
- * Object#attr_boolean
19
- * ActiveSupport::OrderedHash
20
- * patched to support #to_yaml, unless patch is already applied
21
- * Float#to_frac
22
-
23
- * 2 minor enhancements:
24
- * Hash and array methods now call self.class.new instead of {} or [], so that their descendants work as expected.
25
- * Hash#camelize_keys saw some improvement by removing redundant calls to #to_s
26
-
27
- === 1.0.0 2010-03-18
28
-
29
- * 1 major enhancement:
30
- * Initial release
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
@@ -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/object.rb
14
- lib/sc-core-ext/regexp.rb
15
- lib/sc-core-ext/string.rb
16
- script/console
17
- script/destroy
18
- script/generate
19
- spec/rcov.opts
20
- spec/sc-core-ext/active_support/ordered_hash_spec.rb
21
- spec/sc-core-ext/array_spec.rb
22
- spec/sc-core-ext/date_time_spec.rb
23
- spec/sc-core-ext/float_spec.rb
24
- spec/sc-core-ext/hash_spec.rb
25
- spec/sc-core-ext/object_spec.rb
26
- spec/sc-core-ext/regexp_spec.rb
27
- spec/sc-core-ext/string_spec.rb
28
- spec/spec.opts
29
- spec/spec_helper.rb
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
@@ -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
+
@@ -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
 
@@ -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
- module ScCoreExt
14
- VERSION = '1.1.1'
15
- end
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
@@ -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'
@@ -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 : '&frac14;'
6
- when 0.5 : '&frac12;'
7
- when 0.75 : '&frac34;'
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 : '&frac14;'
6
+ when 0.5 : '&frac12;'
7
+ when 0.75 : '&frac34;'
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,66 +1,84 @@
1
- class Hash
2
- # Performs a "deep copy" of this hash; that is, returns a Hash that is a duplicate of this Hash, and whose
3
- # keys and values have each, in turn, had #deep_dup or #dup called on them. This should produce a Hash 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
- inject(self.class.new) do |new_hash, (key, value)|
9
- key = key.respond_to?(:deep_dup) ? key.deep_dup : key.dup?
10
- value = value.respond_to?(:deep_dup) ? value.deep_dup : value.dup?
11
- new_hash[key] = value
12
- new_hash
13
- end
14
- end
15
-
16
- def without(*keys)
17
- keys.flatten!
18
- inject(self.class.new) do |hash, (key, value)|
19
- hash[key] = value unless keys.include?(key)
20
- hash
21
- end
22
- end
23
-
24
- def without_values(*values)
25
- values.flatten!
26
- inject(self.class.new) do |hash, (key, value)|
27
- hash[key] = value unless values.include?(value)
28
- hash
29
- end
30
- end
31
-
32
- # Returns a hash that is a copy of this one, except that all nil values have been removed, making them
33
- # essentially "optional" keys.
34
- def optionalize
35
- without_values(nil)
36
- end
37
-
38
- alias without_nil_values optionalize
39
-
40
- def camelize_keys
41
- stringified = stringify_keys
42
- stringified.rename(stringified.inject(self.class.new) do |renamed, (key, value)|
43
- renamed[key] = key.camelize
44
- renamed
45
- end)
46
- end
47
-
48
- # Takes a hash whose keys must match keys in this hash. Those keys will be renamed to match the
49
- # corresponding value in the specified hash.
50
- #
51
- # Keys not found are ignored.
52
- #
53
- # Returns self.
54
- #
55
- # Example:
56
- # { :a => 1 }.rename(:a => :b)
57
- # => {:b => 1}
58
- #
59
- def rename(to)
60
- merge!(inject(self.class.new) do |hash, (old_key, value)|
61
- hash[to[old_key] || old_key] = value
62
- delete(old_key)
63
- hash
64
- end)
65
- end
66
- end
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