da_huangs_ruby_extensions 0.0.1
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/Gemfile +2 -0
- data/LICENSE +20 -0
- data/README.rdoc +17 -0
- data/Rakefile +55 -0
- data/VERSION +1 -0
- data/lib/array.rb +45 -0
- data/lib/da_huangs_ruby_extensions.rb +5 -0
- data/lib/hash.rb +25 -0
- data/lib/nil_class.rb +9 -0
- data/lib/string.rb +39 -0
- data/test/helper.rb +10 -0
- data/test/test_da_huangs_ruby_extensions.rb +7 -0
- metadata +93 -0
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Tyler Gannon
|
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,17 @@
|
|
1
|
+
= da_huangs_ruby_extensions
|
2
|
+
|
3
|
+
Description goes here.
|
4
|
+
|
5
|
+
== Note on Patches/Pull Requests
|
6
|
+
|
7
|
+
* Fork the project.
|
8
|
+
* Make your feature addition or bug fix.
|
9
|
+
* Add tests for it. This is important so I don't break it in a
|
10
|
+
future version unintentionally.
|
11
|
+
* Commit, do not mess with rakefile, version, or history.
|
12
|
+
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
13
|
+
* Send me a pull request. Bonus points for topic branches.
|
14
|
+
|
15
|
+
== Copyright
|
16
|
+
|
17
|
+
Copyright (c) 2010 Tyler Gannon. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "da_huangs_ruby_extensions"
|
8
|
+
gem.summary = "A small number of extra methods for ruby base classes"
|
9
|
+
gem.description = "I find myself wanting to use these methods all the time."
|
10
|
+
gem.email = "tgannon@gmail.com"
|
11
|
+
gem.homepage = "http://github.com/tylergannon/da_huangs_ruby_extensions"
|
12
|
+
gem.authors = ["Tyler Gannon"]
|
13
|
+
gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
|
14
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
15
|
+
gem.files = FileList['lib/**/*.rb', 'test/**/*', 'Gemfile', 'LICENSE', 'Rakefile', 'README.rdoc', 'VERSION'].to_a
|
16
|
+
end
|
17
|
+
Jeweler::GemcutterTasks.new
|
18
|
+
rescue LoadError
|
19
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
20
|
+
end
|
21
|
+
|
22
|
+
require 'rake/testtask'
|
23
|
+
Rake::TestTask.new(:test) do |test|
|
24
|
+
test.libs << 'lib' << 'test'
|
25
|
+
test.pattern = 'test/**/test_*.rb'
|
26
|
+
test.verbose = true
|
27
|
+
end
|
28
|
+
|
29
|
+
begin
|
30
|
+
require 'rcov/rcovtask'
|
31
|
+
Rcov::RcovTask.new do |test|
|
32
|
+
test.libs << 'test'
|
33
|
+
test.pattern = 'test/**/test_*.rb'
|
34
|
+
test.verbose = true
|
35
|
+
end
|
36
|
+
rescue LoadError
|
37
|
+
task :rcov do
|
38
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
task :test => :check_dependencies
|
43
|
+
|
44
|
+
task :default => :test
|
45
|
+
|
46
|
+
require 'rake/rdoctask'
|
47
|
+
Rake::RDocTask.new do |rdoc|
|
48
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
49
|
+
|
50
|
+
rdoc.rdoc_dir = 'rdoc'
|
51
|
+
rdoc.title = "da_huangs_ruby_extensions #{version}"
|
52
|
+
rdoc.rdoc_files.include('README*')
|
53
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
54
|
+
end
|
55
|
+
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.1
|
data/lib/array.rb
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
class Array
|
2
|
+
def to_h
|
3
|
+
map { |e| yield e }.inject({}) { |carry, e| carry.merge! e }
|
4
|
+
end
|
5
|
+
|
6
|
+
def strip_each
|
7
|
+
self.map{|t| t.strip}
|
8
|
+
end
|
9
|
+
def strip_each!
|
10
|
+
self.each{|t| t.strip!}
|
11
|
+
end
|
12
|
+
|
13
|
+
def to_sentence
|
14
|
+
i = size
|
15
|
+
if size<=2
|
16
|
+
self.join(" and ")
|
17
|
+
else
|
18
|
+
[self[0..size-2].join(", "), self[size-1]].join(" and ")
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def map_with_index
|
23
|
+
result = []
|
24
|
+
self.each_with_index do |elt, idx|
|
25
|
+
result << yield(elt, idx)
|
26
|
+
end
|
27
|
+
result
|
28
|
+
end
|
29
|
+
def plus_if(obj)
|
30
|
+
self << obj unless include?(obj) || obj.nil?
|
31
|
+
end
|
32
|
+
|
33
|
+
def strip_all
|
34
|
+
map!{|t| t.strip}
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
module ArrayPlus
|
39
|
+
def +(y)
|
40
|
+
y.each do |o|
|
41
|
+
self << o unless include?(o)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
data/lib/hash.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
class Hash
|
2
|
+
def copy_without_destroy
|
3
|
+
a = {}
|
4
|
+
self.each {|key,val| a[key]=val unless key.to_s == "_destroy"}
|
5
|
+
a
|
6
|
+
end
|
7
|
+
|
8
|
+
def << (hash)
|
9
|
+
hash.each{|a,b| self[a]=b}
|
10
|
+
end
|
11
|
+
|
12
|
+
def map(&block)
|
13
|
+
val = {}
|
14
|
+
self.each {|a,b| val << block.call(a,b)}
|
15
|
+
val
|
16
|
+
end
|
17
|
+
|
18
|
+
def map_ary(&block)
|
19
|
+
val = []
|
20
|
+
self.each{|a,b| val << block.call(a,b)}
|
21
|
+
val
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
|
data/lib/nil_class.rb
ADDED
data/lib/string.rb
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
class String
|
2
|
+
def normalize
|
3
|
+
ActiveSupport::Multibyte::Chars.new(self).mb_chars.normalize(:kd).gsub(/[^\x00-\x7F]/n,'').to_s
|
4
|
+
end
|
5
|
+
|
6
|
+
def is_numeric?
|
7
|
+
/^\d+$/.match(self) ? true : false
|
8
|
+
end
|
9
|
+
|
10
|
+
def to_search_string
|
11
|
+
a = gsub(/[-_+]/, " ").gsub(/%20/, " ").downcase
|
12
|
+
a.index("%") ? a : "%#{a}"
|
13
|
+
end
|
14
|
+
|
15
|
+
def to_url_string
|
16
|
+
titleize.gsub(" ", "_")
|
17
|
+
end
|
18
|
+
|
19
|
+
# Convert a string to a format suitable for a URL without ever using escaped characters.
|
20
|
+
# It calls strip, removeaccents, downcase (optional) then removes the spaces (optional)
|
21
|
+
# and finally removes any characters matching the default regexp (/[^-_A-Za-z0-9]/).
|
22
|
+
#
|
23
|
+
# Options
|
24
|
+
#
|
25
|
+
# * :downcase => call downcase on the string (defaults to true)
|
26
|
+
# * :convert_spaces => Convert space to underscore (defaults to false)
|
27
|
+
# * :regexp => The regexp matching characters that will be converting to an empty string (defaults to /[^-_A-Za-z0-9]/)
|
28
|
+
def urlize(options = {})
|
29
|
+
options[:downcase] ||= true
|
30
|
+
options[:convert_spaces] ||= false
|
31
|
+
options[:regexp] ||= /[^-_A-Za-z0-9]/
|
32
|
+
|
33
|
+
str = self.strip.normalize
|
34
|
+
str.downcase! if options[:downcase]
|
35
|
+
str.gsub!(/\ /,'_') if options[:convert_spaces]
|
36
|
+
str.gsub(options[:regexp], '')
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
data/test/helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: da_huangs_ruby_extensions
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Tyler Gannon
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-07-15 00:00:00 -07:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: thoughtbot-shoulda
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 3
|
30
|
+
segments:
|
31
|
+
- 0
|
32
|
+
version: "0"
|
33
|
+
type: :development
|
34
|
+
version_requirements: *id001
|
35
|
+
description: I find myself wanting to use these methods all the time.
|
36
|
+
email: tgannon@gmail.com
|
37
|
+
executables: []
|
38
|
+
|
39
|
+
extensions: []
|
40
|
+
|
41
|
+
extra_rdoc_files:
|
42
|
+
- LICENSE
|
43
|
+
- README.rdoc
|
44
|
+
files:
|
45
|
+
- Gemfile
|
46
|
+
- LICENSE
|
47
|
+
- README.rdoc
|
48
|
+
- Rakefile
|
49
|
+
- VERSION
|
50
|
+
- lib/array.rb
|
51
|
+
- lib/da_huangs_ruby_extensions.rb
|
52
|
+
- lib/hash.rb
|
53
|
+
- lib/nil_class.rb
|
54
|
+
- lib/string.rb
|
55
|
+
- test/helper.rb
|
56
|
+
- test/test_da_huangs_ruby_extensions.rb
|
57
|
+
has_rdoc: true
|
58
|
+
homepage: http://github.com/tylergannon/da_huangs_ruby_extensions
|
59
|
+
licenses: []
|
60
|
+
|
61
|
+
post_install_message:
|
62
|
+
rdoc_options:
|
63
|
+
- --charset=UTF-8
|
64
|
+
require_paths:
|
65
|
+
- lib
|
66
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
67
|
+
none: false
|
68
|
+
requirements:
|
69
|
+
- - ">="
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
hash: 3
|
72
|
+
segments:
|
73
|
+
- 0
|
74
|
+
version: "0"
|
75
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
76
|
+
none: false
|
77
|
+
requirements:
|
78
|
+
- - ">="
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
hash: 3
|
81
|
+
segments:
|
82
|
+
- 0
|
83
|
+
version: "0"
|
84
|
+
requirements: []
|
85
|
+
|
86
|
+
rubyforge_project:
|
87
|
+
rubygems_version: 1.3.7
|
88
|
+
signing_key:
|
89
|
+
specification_version: 3
|
90
|
+
summary: A small number of extra methods for ruby base classes
|
91
|
+
test_files:
|
92
|
+
- test/helper.rb
|
93
|
+
- test/test_da_huangs_ruby_extensions.rb
|