Module: Hammock::StringPatches::InstanceMethods
- Defined in:
- lib/hammock/monkey_patches/string.rb
Defined Under Namespace
Classes: Colorizer
Constant Summary collapse
- NamePrefixes =
TODO any more to add?
%w[de den la von].freeze
Instance Method Summary collapse
-
#/(other) ⇒ Object
Return
selfjoined to other using File#join. - #capitalize_name ⇒ Object
- #capitalize_name! ⇒ Object
- #clean_email ⇒ Object
- #clean_email! ⇒ Object
- #colorize(description = '', start_at = nil) ⇒ Object
- #colorize!(description = '', start_at = nil) ⇒ Object
-
#describe_as_ip ⇒ Object
Returns a symbol describing the class of IP address
selfrepresents, if any. -
#end_with(str) ⇒ Object
Return a duplicate of
self, withstrappended to it if it doesn’t already end withstr. -
#ends_with?(str) ⇒ Boolean
Returns true iff
strappears exactly at the end ofself. - #possessive ⇒ Object
- #quote_for_shell ⇒ Object
-
#start_with(str) ⇒ Object
Return a duplicate of
self, withstrprepended to it if it doesn’t already start withstr. -
#starts_with?(str) ⇒ Boolean
Returns true iff
strappears exactly at the start ofself. -
#valid_email? ⇒ Boolean
Returns true if the string represents a valid email address.
-
#valid_ip? ⇒ Boolean
Returns whether this IP should be considered a valid one for a client to be using.
Instance Method Details
#/(other) ⇒ Object
Return self joined to other using File#join. This allows paths to be constructed with a nice syntax; for example, the following two expressions are equivalent.
File.join RAILS_ROOT, ‘views’, controller_name, ‘index.html.haml’ RAILS_ROOT / ‘views’ / controller_name / ‘index.html.haml’
55 56 57 |
# File 'lib/hammock/monkey_patches/string.rb', line 55 def / other File.join self, other end |
#capitalize_name ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/hammock/monkey_patches/string.rb', line 70 def capitalize_name split(' ').map {|term| term.split('-').map {|term| if NamePrefixes.include?(term) term.downcase elsif (term != term.downcase) term else # only capitalize words that are entirely lower case term.capitalize end }.join('-') }.join(' ') end |
#capitalize_name! ⇒ Object
84 85 86 |
# File 'lib/hammock/monkey_patches/string.rb', line 84 def capitalize_name! self.replace self.capitalize_name end |
#clean_email ⇒ Object
150 151 152 |
# File 'lib/hammock/monkey_patches/string.rb', line 150 def clean_email dup.clean_email! end |
#clean_email! ⇒ Object
143 144 145 146 147 148 149 |
# File 'lib/hammock/monkey_patches/string.rb', line 143 def clean_email! strip! downcase! gsub! 'mailto:', '' strip! self end |
#colorize(description = '', start_at = nil) ⇒ Object
154 155 156 157 158 159 160 |
# File 'lib/hammock/monkey_patches/string.rb', line 154 def colorize description = '', start_at = nil if start_at.nil? || (cut_point = index(start_at)).nil? Colorizer.colorize self, description else self[0...cut_point] + Colorizer.colorize(self[cut_point..-1], description) end end |
#colorize!(description = '', start_at = nil) ⇒ Object
162 163 164 |
# File 'lib/hammock/monkey_patches/string.rb', line 162 def colorize! description = '', start_at = nil replace colorize(description, start_at) end |
#describe_as_ip ⇒ Object
Returns a symbol describing the class of IP address self represents, if any.
Examples:
"Hello world!".valid_ip? #=> false
"192.168.".valid_ip? #=> false
"127.0.0.1".valid_ip? #=> :loopback
"172.24.137.6".valid_ip? #=> :private
"169.254.1.142".valid_ip? #=> :self_assigned
"72.9.108.122".valid_ip? #=> :public
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/hammock/monkey_patches/string.rb', line 107 def describe_as_ip parts = strip.split('.') bytes = parts.zip(parts.map(&:to_i)).map {|(str,val)| val if ((1..255) === val) || (val == 0 && str == '0') }.squash if bytes.length != 4 false elsif bytes.starts_with? 0 # Source hosts on "this" network :reserved elsif bytes.starts_with? 127 # Loopback network; RFC1700 :loopback elsif bytes.starts_with? 10 # Class-A private; RFC1918 :private elsif bytes.starts_with?(172) && ((16..31) === bytes[1]) # Class-B private; RFC1918 :private elsif bytes.starts_with? 169, 254 # Link-local range; RFC3330/3927 bytes[2].in?(0, 255) ? :reserved : :self_assigned elsif bytes.starts_with? 192, 0, 2 # TEST-NET - used as example.com IP :reserved elsif bytes.starts_with? 192, 88, 99 # 6-to-4 relay anycast; RFC3068 :reserved elsif bytes.starts_with? 192, 168 # Class-C private; RFC1918 :private elsif bytes.starts_with? 198, 18 # Benchmarking; RFC2544 :reserved else :public end end |
#end_with(str) ⇒ Object
Return a duplicate of self, with str appended to it if it doesn’t already end with str.
46 47 48 |
# File 'lib/hammock/monkey_patches/string.rb', line 46 def end_with str ends_with?(str) ? self : self + str end |
#ends_with?(str) ⇒ Boolean
Returns true iff str appears exactly at the end of self.
36 37 38 |
# File 'lib/hammock/monkey_patches/string.rb', line 36 def ends_with? str self[-str.length, str.length] == str end |
#possessive ⇒ Object
59 60 61 |
# File 'lib/hammock/monkey_patches/string.rb', line 59 def possessive "#{self}'#{'s' unless ends_with?('s')}" end |
#quote_for_shell ⇒ Object
63 64 65 |
# File 'lib/hammock/monkey_patches/string.rb', line 63 def quote_for_shell %Q{"#{gsub('"', '\"')}"} end |
#start_with(str) ⇒ Object
Return a duplicate of self, with str prepended to it if it doesn’t already start with str.
41 42 43 |
# File 'lib/hammock/monkey_patches/string.rb', line 41 def start_with str starts_with?(str) ? self : str + self end |
#starts_with?(str) ⇒ Boolean
Returns true iff str appears exactly at the start of self.
31 32 33 |
# File 'lib/hammock/monkey_patches/string.rb', line 31 def starts_with? str self[0, str.length] == str end |
#valid_email? ⇒ Boolean
Returns true if the string represents a valid email address.
139 140 141 |
# File 'lib/hammock/monkey_patches/string.rb', line 139 def valid_email? /^([a-z0-9\-\+\=\&\'\_\.]{2,})\@([a-z0-9\-]+\.)*([a-z0-9\-]{2,}\.)([a-z0-9\-]{2,})$/ =~ self end |
#valid_ip? ⇒ Boolean
Returns whether this IP should be considered a valid one for a client to be using.
89 90 91 92 93 94 95 |
# File 'lib/hammock/monkey_patches/string.rb', line 89 def valid_ip? if production? describe_as_ip == :public else describe_as_ip.in? :public, :private, :loopback end end |