flash_extensions 0.0.1 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -9,7 +9,7 @@ Gem::Specification.new do |spec|
9
9
  spec.authors = ["Juan Gomez"]
10
10
  spec.email = ["j.gomez@drexed.com"]
11
11
  spec.summary = %q{Commonly used object helpers}
12
- spec.description = %q{Class extensions of of commonly used object helpers.}
12
+ spec.description = %q{Class extensions of commonly used object helpers.}
13
13
  spec.homepage = "https://github.com/drexed/flash_extensions"
14
14
  spec.license = "MIT"
15
15
 
@@ -21,5 +21,5 @@ Gem::Specification.new do |spec|
21
21
  spec.add_development_dependency "bundler", "~> 1.5"
22
22
  spec.add_development_dependency "coveralls"
23
23
  spec.add_development_dependency "rake"
24
- spec.add_development_dependency "rspec", "~> 3.0.0.beta"
25
- end
24
+ spec.add_development_dependency "rspec", "~> 3.0.0"
25
+ end
@@ -1,6 +1,8 @@
1
1
  require 'flash_extensions/version'
2
2
  require 'flash_extensions/extensions/array_extension'
3
+ require 'flash_extensions/extensions/enumerable_extension'
3
4
  require 'flash_extensions/extensions/hash_extension'
5
+ require 'flash_extensions/extensions/numeric_extension'
4
6
  require 'flash_extensions/extensions/object_extension'
5
7
  require 'flash_extensions/extensions/string_extension'
6
8
  require 'flash_extensions/extensions/time_extension'
@@ -1,15 +1,15 @@
1
1
  class Array
2
2
 
3
3
  def remove_blanks
4
- self.reject{ |e| e.blank? }
4
+ reject { |e| e.blank? }
5
5
  end
6
6
 
7
- def remove_first_element
8
- self[1..self.size]
7
+ def remove_first
8
+ self[1..-1]
9
9
  end
10
10
 
11
- def remove_last_element
12
- self[0..self.size-2]
11
+ def remove_last
12
+ self[0...-1]
13
13
  end
14
14
 
15
15
  end
@@ -0,0 +1,91 @@
1
+ module Enumerable
2
+
3
+ unless method_defined?(:sum)
4
+ def sum(identity=0)
5
+ inject { |s, e| s + e } || identity
6
+ end
7
+ end
8
+
9
+ def average(identity=0)
10
+ collection_size = to_a.size
11
+ collection_size > 0 ? inject(&:+) / collection_size.to_f : identity
12
+ end
13
+
14
+ def drop_last(n)
15
+ array = to_a
16
+
17
+ return array if n > array.size
18
+ array[0...(array.size - n)]
19
+ end
20
+
21
+ def drop_last_while
22
+ return to_enum(:drop_last_while) unless block_given?
23
+
24
+ result = []
25
+ dropping = true
26
+ reverse_each do |obj|
27
+ result.unshift(obj) unless dropping &&= yield(obj)
28
+ end
29
+ result
30
+ end
31
+
32
+ def exactly?(n)
33
+ found_count = 0
34
+
35
+ if block_given?
36
+ each do |*o|
37
+ if yield(*o)
38
+ found_count += 1
39
+ end
40
+ end
41
+ else
42
+ each do |o|
43
+ if o
44
+ found_count += 1
45
+ end
46
+ end
47
+ end
48
+
49
+ (found_count > n) ? false : n == found_count
50
+ end
51
+
52
+ def frequencies
53
+ each_with_object(Hash.new(0)) { |e, a| a[e] += 1 }
54
+ end
55
+
56
+ def several?
57
+ found_count = 0
58
+
59
+ if block_given?
60
+ each do |*o|
61
+ if yield(*o)
62
+ found_count += 1
63
+ end
64
+ end
65
+ else
66
+ each do |o|
67
+ if o
68
+ found_count += 1
69
+ end
70
+ end
71
+ end
72
+
73
+ (found_count > 1) ? true : false
74
+ end
75
+
76
+ def take_last(n)
77
+ array = to_a
78
+
79
+ return array if n > array.size
80
+ array[(array.size - n)..-1]
81
+ end
82
+
83
+ def take_last_while
84
+ return to_enum(:take_last_while) unless block_given?
85
+
86
+ result = []
87
+ reverse_each { |e| yield(e) ? result.unshift(e) : break }
88
+ result
89
+ end
90
+
91
+ end
@@ -1,6 +1,6 @@
1
1
  class Hash
2
2
 
3
- unless method_defined?("stringify_keys")
3
+ unless method_defined?(:stringify_keys)
4
4
  def stringify_keys
5
5
  inject({}) do |options, (key, value)|
6
6
  options[key.to_s] = value
@@ -9,13 +9,13 @@ class Hash
9
9
  end
10
10
  end
11
11
 
12
- unless method_defined?("stringify_keys!")
12
+ unless method_defined?(:stringify_keys!)
13
13
  def stringify_keys!
14
- self.replace(self.stringify_keys)
14
+ replace(stringify_keys)
15
15
  end
16
16
  end
17
17
 
18
- unless method_defined?("symbolize_keys")
18
+ unless method_defined?(:symbolize_keys)
19
19
  def symbolize_keys
20
20
  inject({}) do |options, (key, value)|
21
21
  options[(key.to_sym rescue key) || key] = value
@@ -24,18 +24,34 @@ class Hash
24
24
  end
25
25
  end
26
26
 
27
- unless method_defined?("symbolize_keys!")
27
+ unless method_defined?(:symbolize_keys!)
28
28
  def symbolize_keys!
29
- self.replace(self.symbolize_keys)
29
+ replace(symbolize_keys)
30
30
  end
31
31
  end
32
32
 
33
+ def except(*args)
34
+ hash = dup
35
+ args.each {|k| hash.delete(k) }
36
+ hash
37
+ end
38
+
39
+ def only(*args)
40
+ hash = {}
41
+ args.each {|k| hash[k] = self[k] if self.has_key?(k) }
42
+ hash
43
+ end
44
+
33
45
  def rename_keys(*args)
34
46
  keys = Hash[*args.flatten]
35
- keys.each { |k, v| self[v] = self.delete(k) if self[k] }
47
+ keys.each { |k, v| self[v] = delete(k) if self[k] }
36
48
  self
37
49
  end
38
50
 
51
+ def rename_keys!(*args)
52
+ replace(rename_keys(*args))
53
+ end
54
+
39
55
  def symbolize_and_underscore_keys
40
56
  inject({}) do |options, (key, value)|
41
57
  options[(key.to_s.underscore.to_sym rescue key) || key] = value
@@ -44,7 +60,7 @@ class Hash
44
60
  end
45
61
 
46
62
  def symbolize_and_underscore_keys!
47
- self.replace(self.symbolize_and_underscore_keys)
63
+ replace(symbolize_and_underscore_keys)
48
64
  end
49
65
 
50
66
  end
@@ -0,0 +1,17 @@
1
+ class Numeric
2
+
3
+ unless method_defined?(:multiple_of?)
4
+ def multiple_of?(number)
5
+ number != 0 ? modulo(number).zero? : zero?
6
+ end
7
+ end
8
+
9
+ def negative?
10
+ self < 0
11
+ end
12
+
13
+ def positive?
14
+ self > 0
15
+ end
16
+
17
+ end
@@ -1,29 +1,29 @@
1
1
  class Object
2
2
 
3
- unless method_defined?("blank?")
3
+ unless method_defined?(:blank?)
4
4
  def blank?
5
5
  respond_to?(:empty?) ? !!empty? : !self
6
6
  end
7
7
  end
8
8
 
9
- unless method_defined?("present?")
9
+ unless method_defined?(:present?)
10
10
  def present?
11
11
  !blank?
12
12
  end
13
13
  end
14
14
 
15
- unless method_defined?("try")
15
+ unless method_defined?(:try)
16
16
  def try(method)
17
- send method if respond_to? method
17
+ send(method) if respond_to?(method)
18
18
  end
19
19
  end
20
20
 
21
21
  def numeric?
22
- !self.to_s.match(/\A[+-]?\d+?(\.\d+)?\Z/).nil?
22
+ !to_s.match(/\A[+-]?\d+?(\.\d+)?\Z/).nil?
23
23
  end
24
24
 
25
25
  def palindrome?
26
- self.to_s.reverse == self.to_s
26
+ to_s.reverse == to_s
27
27
  end
28
28
 
29
29
  end
@@ -1,25 +1,31 @@
1
1
  class String
2
2
 
3
- unless method_defined?("camelize")
3
+ unless method_defined?(:camelize)
4
4
  def camelize(first_letter_in_uppercase = true)
5
5
  if first_letter_in_uppercase != :lower
6
- self.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
6
+ to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
7
7
  else
8
- self.to_s[0].chr.downcase + camelize(self)[1..-1]
8
+ to_s[0].chr.downcase + camelize(self)[1..-1]
9
9
  end
10
10
  end
11
11
  end
12
12
 
13
- unless method_defined?("ends_with?")
13
+ unless method_defined?(:camelize!)
14
+ def camelize!(first_letter_in_uppercase = true)
15
+ replace(camelize(first_letter_in_uppercase))
16
+ end
17
+ end
18
+
19
+ unless method_defined?(:ends_with?)
14
20
  def ends_with?(suffix)
15
21
  suffix = suffix.to_s
16
22
  self[-suffix.length, suffix.length] == suffix
17
23
  end
18
24
  end
19
25
 
20
- unless method_defined?("humanize")
26
+ unless method_defined?(:humanize)
21
27
  def humanize(options = {})
22
- self.underscore.
28
+ underscore.
23
29
  gsub(/_id$/, "").
24
30
  tr('_', ' ').
25
31
  gsub(/([a-z\d]*)/i) { |match| match.downcase }.
@@ -27,24 +33,36 @@ class String
27
33
  end
28
34
  end
29
35
 
30
- unless method_defined?("titleize")
36
+ unless method_defined?(:humanize!)
37
+ def humanize!(options = {})
38
+ replace(humanize)
39
+ end
40
+ end
41
+
42
+ unless method_defined?(:titleize)
31
43
  def titleize
32
- self.underscore.
44
+ underscore.
33
45
  humanize.
34
46
  gsub(/\b(?<!['’`])[a-z]/) { $&.capitalize }
35
47
  end
36
48
  end
37
49
 
38
- unless method_defined?("starts_with?")
50
+ unless method_defined?(:titleize!)
51
+ def titleize!
52
+ replace(titleize)
53
+ end
54
+ end
55
+
56
+ unless method_defined?(:starts_with?)
39
57
  def starts_with?(prefix)
40
58
  prefix = prefix.to_s
41
59
  self[0, prefix.length] == prefix
42
60
  end
43
61
  end
44
62
 
45
- unless method_defined?("underscore")
63
+ unless method_defined?(:underscore)
46
64
  def underscore
47
- self.gsub(/::/, '/').
65
+ gsub(/::/, '/').
48
66
  gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
49
67
  gsub(/([a-z\d])([A-Z])/,'\1_\2').
50
68
  tr("-", "_").
@@ -52,25 +70,35 @@ class String
52
70
  end
53
71
  end
54
72
 
73
+ unless method_defined?(:underscore!)
74
+ def underscore!
75
+ replace(underscore)
76
+ end
77
+ end
78
+
55
79
  def domain(options={})
56
- url = self.dup
80
+ url = dup
57
81
  url =~ (/^(?:\w+:\/\/)?([^\/?]+)(?:\/|\?|$)/) ? $1 : url
58
82
  end
59
83
 
60
84
  def downcase?
61
- self.downcase == self
85
+ downcase == self
62
86
  end
63
87
 
64
88
  def ellipsize(options={})
65
- length = options[:length] || 30
89
+ length = options[:length] || 30
66
90
  separator = options[:separator] || '...'
67
- return self if self.size <= length
68
- offset = options[:offset] || 4
91
+ return self if size <= length
92
+ offset = options[:offset] || 4
69
93
  self[0,offset] + separator + self[-offset,offset]
70
94
  end
71
95
 
72
96
  def gnix(string)
73
- self.gsub(string, "")
97
+ gsub(string, "")
98
+ end
99
+
100
+ def gnix!(string)
101
+ gsub!(string, "")
74
102
  end
75
103
 
76
104
  def mixedcase?
@@ -78,41 +106,65 @@ class String
78
106
  end
79
107
 
80
108
  def nix(string)
81
- self.sub(string, "")
109
+ sub(string, "")
110
+ end
111
+
112
+ def nix!(string)
113
+ sub!(string, "")
82
114
  end
83
115
 
84
116
  def pollute(delimiter="^--^--^")
85
- self.split('').map{ |letter| "#{letter}#{delimiter}" }.join
117
+ split('').map{ |letter| "#{letter}#{delimiter}" }.join
86
118
  end
87
119
 
88
120
  def slugify
89
- self.gsub(/[^\x00-\x7F]+/, ''). # Remove anything non-ASCII entirely (e.g. diacritics).
90
- gsub(/[^\w_ \-]+/i, ''). # Remove unwanted chars.
91
- gsub(/[ \-]+/i, '-'). # No more than one of the separator in a row.
92
- gsub(/^\-|\-$/i, ''). # Remove leading/trailing separator.
121
+ gsub(/[^\x00-\x7F]+/, ''). # Remove anything non-ASCII entirely (e.g. diacritics).
122
+ gsub(/[^\w_ \-]+/i, ''). # Remove unwanted chars.
123
+ gsub(/[ \-]+/i, '-'). # No more than one of the separator in a row.
124
+ gsub(/^\-|\-$/i, ''). # Remove leading/trailing separator.
93
125
  downcase
94
126
  end
95
127
 
128
+ def slugify!
129
+ replace(slugify)
130
+ end
131
+
132
+ def squish
133
+ strip.gsub(/\s+/, ' ')
134
+ end
135
+
136
+ def squish!
137
+ replace(squish)
138
+ end
139
+
96
140
  def strip_tags
97
- self.gsub(/<\/?[^>]*>/, "")
141
+ gsub(/<\/?[^>]*>/, "")
142
+ end
143
+
144
+ def strip_tags!
145
+ replace(strip_tags)
98
146
  end
99
147
 
100
148
  def strip_whitespace
101
- self.gnix("\t").split(" ").remove_blanks.join(" ")
149
+ gnix("\t").split(" ").remove_blanks.join(" ")
150
+ end
151
+
152
+ def strip_whitespace!
153
+ replace(strip_whitespace)
102
154
  end
103
155
 
104
156
  def truncate_preserving_words(options={})
105
157
  separator = options[:separator] || '...'
106
158
  max_words = options[:max_words] || nil
107
159
  if max_words
108
- words = self.split()
160
+ words = split
109
161
  return self if words.size < max_words
110
162
  words = words[0..(max_words-1)]
111
163
  words << separator
112
164
  words.join(" ")
113
165
  else
114
166
  max_chars = options[:max_chars] || 30
115
- return self if self.size < max_chars
167
+ return self if size < max_chars
116
168
  out = self[0..(max_chars-1)].split(" ")
117
169
  out.pop
118
170
  out << separator
@@ -121,11 +173,11 @@ class String
121
173
  end
122
174
 
123
175
  def unpollute(delimiter="^--^--^")
124
- self.gsub(delimiter, "")
176
+ gsub(delimiter, "")
125
177
  end
126
178
 
127
179
  def upcase?
128
- self.upcase == self
180
+ upcase == self
129
181
  end
130
182
 
131
183
  end