da_huangs_ruby_extensions 0.0.6 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.6
1
+ 0.0.7
data/lib/array_ext.rb CHANGED
@@ -1,37 +1,37 @@
1
- class Array
2
- def to_h
3
- map { |e| yield e }.inject({}) { |carry, e| carry.merge! e }
4
- end
1
+ module DaHuang
2
+ module ArrayExt
3
+ def to_hash
4
+ map { |e| yield e }.inject({}) { |carry, e| carry.merge! e }
5
+ end
5
6
 
6
- def strip_each
7
- self.map{|t| t.strip}
8
- end
9
- def strip_each!
10
- self.each{|t| t.strip!}
11
- end
7
+ def strip_each
8
+ self.map{|t| t.strip}
9
+ end
10
+ def strip_each!
11
+ self.each{|t| t.strip!}
12
+ end
12
13
 
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 ")
14
+ def to_sentence
15
+ i = size
16
+ if size<=2
17
+ self.join(" and ")
18
+ else
19
+ [self[0..size-2].join(", "), self[size-1]].join(" and ")
20
+ end
19
21
  end
20
- end
21
22
 
22
- def map_with_index
23
- result = []
24
- self.each_with_index do |elt, idx|
25
- result << yield(elt, idx)
23
+ def map_with_index
24
+ result = []
25
+ self.each_with_index do |elt, idx|
26
+ result << yield(elt, idx)
27
+ end
28
+ result
26
29
  end
27
- result
28
- end
29
- def plus_if(obj)
30
- self << obj unless include?(obj) || obj.nil?
31
- end
32
30
 
33
- def strip_all
34
- map!{|t| t.strip}
31
+ def strip_all
32
+ map!{|t| t.strip}
33
+ end
35
34
  end
35
+
36
36
  end
37
37
 
@@ -1,5 +1,13 @@
1
+
1
2
  require 'array_ext'
3
+ Array.send :include, DaHuang::ArrayExt
4
+
2
5
  require 'hash_ext'
6
+ Hash.send :include, DaHuang::HashExt
7
+
3
8
  require 'nil_class_ext'
9
+ NilClass.send :include, DaHuang::NilClassExt
10
+
4
11
  require 'string_ext'
12
+ String.send :include, DaHuang::StringExt
5
13
 
data/lib/hash_ext.rb CHANGED
@@ -1,24 +1,18 @@
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
1
+ module DaHuang
2
+ module HashExt
3
+ def add(hash)
4
+ hash.each{|a,b| self[a]=b}
5
+ end
11
6
 
12
- def map(&block)
13
- val = {}
14
- self.each {|a,b| val << block.call(a,b)}
15
- val
16
- end
7
+ def map_ary(&block)
8
+ val = []
9
+ self.each{|a,b| val.add block.call(a,b)}
10
+ val
11
+ end
17
12
 
18
- def map_ary(&block)
19
- val = []
20
- self.each{|a,b| val << block.call(a,b)}
21
- val
13
+ def reject_keys(ary)
14
+ self.reject{|a,b| ary.include?(a)}
15
+ end
22
16
  end
23
17
  end
24
18
 
data/lib/nil_class_ext.rb CHANGED
@@ -1,9 +1,11 @@
1
- class NilClass
2
- def empty?
3
- true
4
- end
5
- def blank?
6
- true
1
+ module DaHuang
2
+ module NilClassExt
3
+ def empty?
4
+ true
5
+ end
6
+ def blank?
7
+ true
8
+ end
7
9
  end
8
10
  end
9
11
 
data/lib/string_ext.rb CHANGED
@@ -1,39 +1,42 @@
1
- class String
2
- def normalize
3
- ActiveSupport::Multibyte::Chars.new(self).mb_chars.normalize(:kd).gsub(/[^\x00-\x7F]/n,'').to_s
4
- end
1
+ module DaHuang
2
+ module StringExt
3
+ def normalize
4
+ ActiveSupport::Multibyte::Chars.new(self).mb_chars.normalize(:kd).gsub(/[^\x00-\x7F]/n,'').to_s
5
+ end
5
6
 
6
- def is_numeric?
7
- /^\d+$/.match(self) ? true : false
8
- end
7
+ def is_numeric?
8
+ /^\d+$/.match(self) ? true : false
9
+ end
9
10
 
10
- def to_search_string
11
- a = gsub(/[-_+]/, " ").gsub(/%20/, " ").downcase
12
- a.index("%") ? a : "%#{a}"
13
- end
11
+ def to_search_string
12
+ a = gsub(/[-_+]/, " ").gsub(/%20/, " ").downcase
13
+ a.index("%") ? a : "%#{a}"
14
+ end
14
15
 
15
- def to_url_string
16
- titleize.gsub(" ", "_")
17
- end
16
+ def to_url_string
17
+ titleize.gsub(" ", "_")
18
+ end
18
19
 
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]/
20
+ # Convert a string to a format suitable for a URL without ever using escaped characters.
21
+ # It calls strip, removeaccents, downcase (optional) then removes the spaces (optional)
22
+ # and finally removes any characters matching the default regexp (/[^-_A-Za-z0-9]/).
23
+ #
24
+ # Options
25
+ #
26
+ # * :downcase => call downcase on the string (defaults to true)
27
+ # * :convert_spaces => Convert space to underscore (defaults to false)
28
+ # * :regexp => The regexp matching characters that will be converting to an empty string (defaults to /[^-_A-Za-z0-9]/)
29
+ def urlize(options = {})
30
+ options[:downcase] ||= true
31
+ options[:convert_spaces] ||= false
32
+ options[:regexp] ||= /[^-_A-Za-z0-9]/
32
33
 
33
- str = self.strip.normalize
34
- str.downcase! if options[:downcase]
35
- str.gsub!(/\ /,'_') if options[:convert_spaces]
36
- str.gsub(options[:regexp], '')
34
+ str = self.strip.normalize
35
+ str.downcase! if options[:downcase]
36
+ str.gsub!(/\ /,'_') if options[:convert_spaces]
37
+ str.gsub(options[:regexp], '')
38
+ end
37
39
  end
40
+
38
41
  end
39
42
 
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: da_huangs_ruby_extensions
3
3
  version: !ruby/object:Gem::Version
4
- hash: 19
4
+ hash: 17
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 6
10
- version: 0.0.6
9
+ - 7
10
+ version: 0.0.7
11
11
  platform: ruby
12
12
  authors:
13
13
  - Tyler Gannon