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
- #capitalize_name ⇒ Object
- #capitalize_name! ⇒ 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
self
represents, if any. -
#end_with(str) ⇒ Object
Return a duplicate of
self
, withstr
appended to it if it doesn’t already end withstr
. -
#ends_with?(str) ⇒ Boolean
Returns true iff
str
appears exactly at the end ofself
. - #possessive ⇒ Object
-
#start_with(str) ⇒ Object
Return a duplicate of
self
, withstr
prepended to it if it doesn’t already start withstr
. -
#starts_with?(str) ⇒ Boolean
Returns true iff
str
appears 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
#capitalize_name ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/hammock/monkey_patches/string.rb', line 57 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
71 72 73 |
# File 'lib/hammock/monkey_patches/string.rb', line 71 def capitalize_name! self.replace self.capitalize_name end |
#colorize(description = '', start_at = nil) ⇒ Object
130 131 132 133 134 135 136 |
# File 'lib/hammock/monkey_patches/string.rb', line 130 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
138 139 140 |
# File 'lib/hammock/monkey_patches/string.rb', line 138 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
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/hammock/monkey_patches/string.rb', line 94 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
50 51 52 |
# File 'lib/hammock/monkey_patches/string.rb', line 50 def possessive "#{self}'#{'s' unless ends_with?('s')}" 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.
126 127 128 |
# File 'lib/hammock/monkey_patches/string.rb', line 126 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.
76 77 78 79 80 81 82 |
# File 'lib/hammock/monkey_patches/string.rb', line 76 def valid_ip? if production? describe_as_ip == :public else describe_as_ip.in? :public, :private, :loopback end end |