Class: String

Inherits:
Object show all
Defined in:
lib/iron/extensions/string.rb

Instance Method Summary collapse

Instance Method Details

#append(str) ⇒ Object



14
15
16
# File 'lib/iron/extensions/string.rb', line 14

def append(str)
  self.insert(-1, str.to_s)
end

#blank?Boolean

Provide helper for nil/“” blankness checker

Returns:

  • (Boolean)


6
7
8
# File 'lib/iron/extensions/string.rb', line 6

def blank?
  self.strip.empty?
end

#constantizeObject



140
141
142
143
144
145
146
147
148
149
# File 'lib/iron/extensions/string.rb', line 140

def constantize
  names = self.split('::')
  names.shift if names.empty? || names.first.empty?

  constant = Object
  names.each do |name|
    constant = constant.const_defined?(name) ? constant.const_get(name) : constant.const_missing(name)
  end
  constant
end

#ends_with?(suffix) ⇒ Boolean

Does the string end with the specified suffix?

Returns:

  • (Boolean)


132
133
134
135
# File 'lib/iron/extensions/string.rb', line 132

def ends_with?(suffix)
  suffix = suffix.to_s
  self[-suffix.length, suffix.length] == suffix
end

#extract(regex) ⇒ Object

Returns the various captured elements from this string with the regex applied.

Usage:  a,b = 'abc123 is cool'.extract(/([a-z]*)([0-9]*)/)
  => a = 'abc', b = '123'

With no capture in regex, returns full match If no match, returns nil



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/iron/extensions/string.rb', line 47

def extract(regex)
  data = self.match(regex)
  return nil unless data
  if data.size > 2
    return *(data.to_a[1..-1])
  elsif data.size == 2
    return data[1]
  else
    return data[0]
  end
end

#integer?Boolean

When true, is a valid integer

Returns:

  • (Boolean)


153
154
155
# File 'lib/iron/extensions/string.rb', line 153

def integer?
  self.to_i.to_s == self
end

#natural_order(nocase = true) ⇒ Object

Returns an array that can be compared (eg via Array#sort) with another string’s natural order to implement natural order sorting (“Bob123” => [‘BOB’, 123])



101
102
103
104
105
106
# File 'lib/iron/extensions/string.rb', line 101

def natural_order(nocase=true)
  i = true
  str = self
  str = str.upcase if nocase
  str.gsub(/\s+/, '').split(/(\d+)/).map {|x| (i = !i) ? x.to_i : x}
end

#prepend(str) ⇒ Object



10
11
12
# File 'lib/iron/extensions/string.rb', line 10

def prepend(str)
  self.insert(0, str.to_s)
end

#replace_vars(obj) ⇒ Object

Simple parse-and-replace code, use eg: “My name is :name:”.replace_vars(@person), will call #name on @person and insert into place of :name:. You can also pass in a hash to source the variable values, eg: “I like :food:!”.replace_vars(=> ‘eggs’)



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/iron/extensions/string.rb', line 62

def replace_vars(obj)
  self.gsub(/:([a-z0-9_]+):/) do |match|
    verb = Regexp.last_match[1].intern
    if obj.is_a?(Hash)
      obj[verb].to_s
    elsif obj.respond_to?(verb)
      obj.send(verb).to_s
    else
      ":#{verb}:"
    end
  end
end

#smart_pluralizeObject



113
114
115
116
117
118
119
120
# File 'lib/iron/extensions/string.rb', line 113

def smart_pluralize
  amt, word = self.extract(/[^0-9\-]*(-?[0-9,]+)?.*(?:[^a-z]|^)([a-z]+)[^a-z]*$/i)
  unless word.blank?
    word = word.pluralize if amt.blank? || amt.gsub(',','').to_i != 1
    return self.gsub(/([^a-z]|^)([a-z]+)([^a-z]*)$/i, '\1'+word+'\3')
  end
  return self
end

#smart_truncate(len = 30, ending = '...') ⇒ Object

Truncate a string to no more than len characters, honoring word boundaries (whitespace and - character)



90
91
92
93
94
95
96
97
# File 'lib/iron/extensions/string.rb', line 90

def smart_truncate(len = 30, ending = '...')
  len = Math.max(len, 5)
  return self if self.length <= len
  s = self[0...(len-2)].reverse
  bits = s.split(/[\s\-,]/,2)
  s = bits.length == 2 ? bits[1] : bits[0]
  s.reverse + ending
end

#starts_with?(prefix) ⇒ Boolean

Does the string start with the specified prefix?

Returns:

  • (Boolean)


126
127
128
129
# File 'lib/iron/extensions/string.rb', line 126

def starts_with?(prefix)
  prefix = prefix.to_s
  self[0, prefix.length] == prefix
end

#to_dashcaseObject Also known as: to_permalink

To a permalink-style string rep, removing all non-word characters and dasherizing all spaces



76
77
78
79
80
81
82
83
84
85
# File 'lib/iron/extensions/string.rb', line 76

def to_dashcase
  s = self.dup
  s.gsub!(/\'/,'')    # remove ' from rob's, so we don't get rob-s-blog
  s.gsub!(/\W+/, ' ') # all non-word chars to spaces
  s.gsub!('_',' ')    # we don't like underscores
  s.strip!            # ooh la la
  s.downcase!         #
  s.gsub!(/\ +/, '-') # spaces to dashes, preferred separator char everywhere
  s
end

#to_dateObject

Date.parse sucks hard (assumes EU formatting, for one, does the wrong thing with 2 digit years for two… etc.) so we have this here as our personal, bona-fide date parsing helper.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/iron/extensions/string.rb', line 20

def to_date
  us_format = /^([0-9]{1,2})[-\/\.]([0-9]{1,2})[-\/\.]([0-9]+)$/        # US standard MM/DD/YYYY, allowing for 2 digit year
  db_format = /^([0-9]{4})[-\/\.]([0-9]{2})[-\/\.]([0-9]{2})$/  # More sane, but less common YYYY-MM-DD

  if self.match(us_format)
    m, d, y = self.extract(us_format)
  elsif self.match(db_format)
    y, m, d = self.extract(db_format)
  else
    return nil
  end
  
  y = y.to_i
  if y < 100
    # Ok, we'll let you do this 2 digit thing, but only because we like you.
    # Assume that 10 years in the future is as far as we're interested in.
    cutoff = Date.today.year - 2000 + 10
    y += (y < cutoff) ? 2000 : 1900
  end
  Date.new(y,m.to_i,d.to_i) rescue nil
end