Class: String
- Inherits:
-
Object
- Object
- String
- Defined in:
- lib/dragonfly_extensions/monkey_patches.rb
Class Method Summary collapse
Instance Method Summary collapse
- #asset_escape ⇒ Object
- #capitalize_each_word ⇒ Object
- #chunk_by_length(max_length = 40) ⇒ Object
- #chunk_by_words(chunk_size = 8) ⇒ Object
- #compress ⇒ Object
-
#initial_caps ⇒ Object
unicode_str.initial_caps => new_str returns a copy of a string with initial capitals “Jules-Édouard”.initial_caps => “J.É.”.
- #truncate_words(length = 10, end_string = ' …') ⇒ Object
Class Method Details
.random_alphanumeric(length = 6) ⇒ Object
151 152 153 154 155 156 |
# File 'lib/dragonfly_extensions/monkey_patches.rb', line 151 def String.random_alphanumeric(length=6) chars = ['A'..'Z','0'..'9'].map{|r|r.to_a}.flatten ignore = ['0','1','L','A','E','I','O','U'] valid = (chars - ignore) Array.new(length).map{valid[rand(valid.size)]}.join.downcase end |
Instance Method Details
#asset_escape ⇒ Object
158 159 160 |
# File 'lib/dragonfly_extensions/monkey_patches.rb', line 158 def asset_escape CGI::escape(self.downcase.downcase.gsub(/\s+/,'_').gsub(/[^[:alnum:]_]/, '')) end |
#capitalize_each_word ⇒ Object
114 115 116 |
# File 'lib/dragonfly_extensions/monkey_patches.rb', line 114 def capitalize_each_word self.downcase.gsub(/^[a-z]|\s+[a-z]/) { |a| a.upcase } end |
#chunk_by_length(max_length = 40) ⇒ Object
136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/dragonfly_extensions/monkey_patches.rb', line 136 def chunk_by_length(max_length = 40) result = [] words = self.split line = '' for word in words if (line.length > max_length) or (line.length + word.length > max_length) result << line line = '' end line << ' ' + word end result << line result end |
#chunk_by_words(chunk_size = 8) ⇒ Object
127 128 129 130 131 132 133 134 |
# File 'lib/dragonfly_extensions/monkey_patches.rb', line 127 def chunk_by_words(chunk_size = 8) result = [] words = self.split for chunk in words.in_groups_of(chunk_size) result << chunk.join(' ') end result end |
#compress ⇒ Object
118 119 120 |
# File 'lib/dragonfly_extensions/monkey_patches.rb', line 118 def compress self.gsub(' ','') end |
#initial_caps ⇒ Object
unicode_str.initial_caps => new_str returns a copy of a string with initial capitals “Jules-Édouard”.initial_caps => “J.É.”
110 111 112 |
# File 'lib/dragonfly_extensions/monkey_patches.rb', line 110 def initial_caps self.tr('-', ' ').split(' ').map { |word| word.chars.first.upcase.to_s + "." }.join end |
#truncate_words(length = 10, end_string = ' …') ⇒ Object
122 123 124 125 |
# File 'lib/dragonfly_extensions/monkey_patches.rb', line 122 def truncate_words(length = 10, end_string = ' …') words = self.split() words[0..(length-1)].join(' ') + (words.length > length ? end_string : '') end |