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

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’



66
67
68
# File 'lib/hammock/monkey_patches/string.rb', line 66

def / other
  File.join self, other
end

#capitalize_nameObject



81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/hammock/monkey_patches/string.rb', line 81

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



95
96
97
# File 'lib/hammock/monkey_patches/string.rb', line 95

def capitalize_name!
  self.replace self.capitalize_name
end

#clean_emailObject



161
162
163
# File 'lib/hammock/monkey_patches/string.rb', line 161

def clean_email
  dup.clean_email!
end

#clean_email!Object



154
155
156
157
158
159
160
# File 'lib/hammock/monkey_patches/string.rb', line 154

def clean_email!
  strip!
  downcase!
  gsub! 'mailto:', ''
  strip!
  self
end

#colorize(description = '', start_at = nil) ⇒ Object



165
166
167
168
169
170
171
# File 'lib/hammock/monkey_patches/string.rb', line 165

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



173
174
175
# File 'lib/hammock/monkey_patches/string.rb', line 173

def colorize! description = '', start_at = nil
  replace colorize(description, start_at)
end

#describe_as_ipObject

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


118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/hammock/monkey_patches/string.rb', line 118

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.

Returns:

  • (Boolean)


36
37
38
# File 'lib/hammock/monkey_patches/string.rb', line 36

def ends_with? str
  self[-str.length, str.length] == str
end

#normalizeObject



50
51
52
# File 'lib/hammock/monkey_patches/string.rb', line 50

def normalize
  mb_chars.normalize(:kd).gsub(/[^\x00-\x7f]/n, '').to_s
end

#normalize_for_displayObject



54
55
56
57
58
59
# File 'lib/hammock/monkey_patches/string.rb', line 54

def normalize_for_display
  mb_chars.normalize(:kd).
  gsub('æ', 'ae').
  gsub('ð', 'd').
  gsub(/[^\x00-\x7f]/n, '').to_s
end

#possessiveObject



70
71
72
# File 'lib/hammock/monkey_patches/string.rb', line 70

def possessive
  "#{self}'#{'s' unless ends_with?('s')}"
end

#quote_for_shellObject



74
75
76
# File 'lib/hammock/monkey_patches/string.rb', line 74

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.

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


150
151
152
# File 'lib/hammock/monkey_patches/string.rb', line 150

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.

Returns:

  • (Boolean)


100
101
102
103
104
105
106
# File 'lib/hammock/monkey_patches/string.rb', line 100

def valid_ip?
  if production?
    describe_as_ip == :public
  else
    describe_as_ip.in? :public, :private, :loopback
  end
end