Class: String
- Includes:
- WithKnowledgeOfColor
- Defined in:
- lib/qualitysmith_extensions/console/command.rb,
lib/qualitysmith_extensions/string/md5.rb,
lib/qualitysmith_extensions/string/digits_only.rb,
lib/qualitysmith_extensions/symbol/constantize.rb,
lib/qualitysmith_extensions/string/shell_escape.rb,
lib/qualitysmith_extensions/colored/toggleability.rb,
lib/qualitysmith_extensions/string/each_char_with_index.rb,
lib/qualitysmith_extensions/string/to_underscored_label.rb,
lib/qualitysmith_extensions/test/difference_highlighting.rb,
lib/qualitysmith_extensions/string/with_knowledge_of_color.rb
Overview
–
- Author
-
Tyler Rick
- Copyright
-
Copyright © 2007 QualitySmith, Inc.
- License
-
Ruby License
- Submit to Facets?
-
Probably not.
Developer notes:
-
Can we use a more general method instead (like humanize or methodize)? Does this really have a use distinct from all the other inflection methods out there?
++
Constant Summary collapse
- @@colorize_enabled =
true
Constants included from WithKnowledgeOfColor
WithKnowledgeOfColor::Color_regexp
Instance Method Summary collapse
- #colorize_with_toggleability(string, options = {}) ⇒ Object
-
#constantize ⇒ Object
Tries to find a declared constant with the name specified in self.
-
#digits_only ⇒ Object
Strips out everything except digits.
- #each_char_with_index ⇒ Object
-
#highlight_absence ⇒ Object
This is a (sub)string that doesn’t exist in self, only in the other string.
-
#highlight_commonality ⇒ Object
This is a (sub)string that is common to both expected and actual.
-
#highlight_difference ⇒ Object
This is a (sub)string that is different between expected and actual.
-
#highlight_unique ⇒ Object
This is a (sub)string that exists only in self, not in the other string.
- #make_control_characters_visible ⇒ Object
- #make_spaces_visible(color) ⇒ Object
-
#md5 ⇒ Object
Because it’s so much more natural to have this as a method of the string rather than having to call
Digest::MD5.hexdigest(string)
. - #option_demethodize ⇒ Object
- #option_methodize ⇒ Object
- #shell_escape ⇒ Object
-
#to_underscored_label ⇒ Object
Strips out most non-alphanumeric characters and leaves you with a lowercased, underscored string that can safely be used as a class_name.
Methods included from WithKnowledgeOfColor
#length_without_color, #ljust_without_color, #nonprinting_characters_used_for_color, #strip_color
Instance Method Details
#colorize_with_toggleability(string, options = {}) ⇒ Object
19 20 21 22 23 24 25 |
# File 'lib/qualitysmith_extensions/colored/toggleability.rb', line 19 def colorize_with_toggleability(string, = {}) if @@colorize_enabled colorize_without_toggleability(string, ) else string end end |
#constantize ⇒ Object
Tries to find a declared constant with the name specified in self.
'Foo'.constantize => Foo
Unlike ActiveSupport, we don’t do this check (because Kernel.module “can handle module hierarchy”):
vendor/rails/activesupport/lib/active_support/inflector.rb
unless /\A(?:::)?([A-Z]\w*(?:::[A-Z]\w*)*)\z/ =~ camel_cased_word
raise NameError, "#{camel_cased_word.inspect} is not a valid constant name!"
end
37 38 39 |
# File 'lib/qualitysmith_extensions/symbol/constantize.rb', line 37 def constantize Kernel.constant(self) end |
#digits_only ⇒ Object
Strips out everything except digits.
10 11 12 |
# File 'lib/qualitysmith_extensions/string/digits_only.rb', line 10 def digits_only self.gsub(/[^0-9]/, "") end |
#each_char_with_index ⇒ Object
13 14 15 16 17 18 19 |
# File 'lib/qualitysmith_extensions/string/each_char_with_index.rb', line 13 def each_char_with_index i = 0 split(//).each do |c| yield i, c i += 1 end end |
#highlight_absence ⇒ Object
This is a (sub)string that doesn’t exist in self, only in the other string. It’s just a placeholder character (a space) that represents a missing character.
53 54 55 |
# File 'lib/qualitysmith_extensions/test/difference_highlighting.rb', line 53 def highlight_absence self.white.on_cyan.bold end |
#highlight_commonality ⇒ Object
This is a (sub)string that is common to both expected and actual
30 31 32 33 34 35 36 |
# File 'lib/qualitysmith_extensions/test/difference_highlighting.rb', line 30 def highlight_commonality self. make_control_characters_visible. make_spaces_visible(:green). green.bold #send_unless(self == ' ', :bold) # spaces are not bold; '_'s (and everything else) are end |
#highlight_difference ⇒ Object
This is a (sub)string that is different between expected and actual
38 39 40 41 42 43 44 |
# File 'lib/qualitysmith_extensions/test/difference_highlighting.rb', line 38 def highlight_difference self. make_control_characters_visible. make_spaces_visible(:red). red.bold #send_unless(self == ' ', :bold) # spaces are not bold; '_'s (and everything else) are end |
#highlight_unique ⇒ Object
This is a (sub)string that exists only in self, not in the other string
46 47 48 49 50 51 |
# File 'lib/qualitysmith_extensions/test/difference_highlighting.rb', line 46 def highlight_unique self. make_control_characters_visible. make_spaces_visible(:magenta). magenta end |
#make_control_characters_visible ⇒ Object
60 61 62 63 64 65 |
# File 'lib/qualitysmith_extensions/test/difference_highlighting.rb', line 60 def make_control_characters_visible self.gsub(/\n/, '\n'+"\n"). # Show '\n' in addition to actually doing the line break gsub(/\r/, '\r'). # Just escape it... gsub(/\t/, '\t') #:todo: Add other control characters? end |
#make_spaces_visible(color) ⇒ Object
56 57 58 59 |
# File 'lib/qualitysmith_extensions/test/difference_highlighting.rb', line 56 def make_spaces_visible(color) #:todo: Make this optional? Might be useful if you are comparing things with lots of spaces and underscores and you want to be able to tell the difference between them...? self.gsub(' ', ' '.send(:"on_#{color}")) end |
#md5 ⇒ Object
Because it’s so much more natural to have this as a method of the string rather than having to call Digest::MD5.hexdigest(string)
.
12 13 14 |
# File 'lib/qualitysmith_extensions/string/md5.rb', line 12 def md5 return Digest::MD5.hexdigest(self) end |
#option_demethodize ⇒ Object
39 40 41 |
# File 'lib/qualitysmith_extensions/console/command.rb', line 39 def option_demethodize self.sub('__','--').gsub('_','-') end |
#option_methodize ⇒ Object
42 43 44 |
# File 'lib/qualitysmith_extensions/console/command.rb', line 42 def option_methodize self.gsub('-','_') end |
#shell_escape ⇒ Object
14 15 16 |
# File 'lib/qualitysmith_extensions/string/shell_escape.rb', line 14 def shell_escape Escape.shell_command([self]) end |
#to_underscored_label ⇒ Object
Strips out most non-alphanumeric characters and leaves you with a lowercased, underscored string that can safely be used as a class_name
12 13 14 15 16 17 18 19 |
# File 'lib/qualitysmith_extensions/string/to_underscored_label.rb', line 12 def to_underscored_label self. downcase. gsub(/-+/, "_").gsub(/ +/, "_"). # spaces and -'s-> underscores gsub(/[^a-z0-9_]/, ""). # keep only alphanumeric and _ characters gsub(/_+$/, ""). # We don't want any _ characters at the end gsub(/^_+/, "") # ... or the beginning end |