Class: String
- Inherits:
-
Object
- Object
- String
- Defined in:
- lib/fat_free_crm/core_ext/string.rb
Overview
Fat Free CRM Copyright © 2008-2011 by Michael Dvorkin
This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License along with this program. If not, see <www.gnu.org/licenses/>.
Instance Method Summary collapse
- #digitize ⇒ Object
- #false? ⇒ Boolean
- #n2br ⇒ Object
-
#name_permutations ⇒ Object
Generates all permutations for first and last name, based on the order of parts A query with 4 words will generate 6 permutations.
- #snakecase ⇒ Object
- #to_url ⇒ Object
- #true? ⇒ Boolean
- #wrap(prefix, suffix = prefix) ⇒ Object
Instance Method Details
#digitize ⇒ Object
33 34 35 |
# File 'lib/fat_free_crm/core_ext/string.rb', line 33 def digitize gsub(/[^\d]/, "") # "$100,000".digitize # => 100000 end |
#false? ⇒ Boolean
53 54 55 |
# File 'lib/fat_free_crm/core_ext/string.rb', line 53 def false? self == "false" end |
#n2br ⇒ Object
23 24 25 |
# File 'lib/fat_free_crm/core_ext/string.rb', line 23 def n2br strip.gsub("\n", "<br />") end |
#name_permutations ⇒ Object
Generates all permutations for first and last name, based on the order of parts A query with 4 words will generate 6 permutations
59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/fat_free_crm/core_ext/string.rb', line 59 def name_permutations parts = self.split(" ") (parts.size - 1).times.map {|i| # ["A", "B", "C", "D"] => [["A B C", "D"], ["A B", "C D"], ["A", "B C D"]] [parts[(0..i)].join(" "), parts[(i+1)..-1].join(" ")] }.inject([]) { |arr, perm| # Search both [first, last] and [last, first] # e.g. for every ["A B C", "D"], also include ["D", "A B C"] arr << perm arr << perm.reverse arr } end |
#snakecase ⇒ Object
43 44 45 |
# File 'lib/fat_free_crm/core_ext/string.rb', line 43 def snakecase self.strip.downcase.gsub(/[\s\/]+/, "_") end |
#to_url ⇒ Object
38 39 40 |
# File 'lib/fat_free_crm/core_ext/string.rb', line 38 def to_url self.match(/^https?:\/\//) ? self : "http://" << self end |
#true? ⇒ Boolean
48 49 50 |
# File 'lib/fat_free_crm/core_ext/string.rb', line 48 def true? self == "true" end |
#wrap(prefix, suffix = prefix) ⇒ Object
28 29 30 |
# File 'lib/fat_free_crm/core_ext/string.rb', line 28 def wrap(prefix, suffix = prefix) prefix + self + suffix end |