Module | DataMapper::Support::String |
In: |
lib/data_mapper/support/string.rb
lib/data_mapper/support/string.rb |
Matches any whitespace (including newline) and replaces with a single space EXAMPLE:
<<QUERY.compress_lines SELECT name FROM users QUERY => "SELECT name FROM users"
# File lib/data_mapper/support/string.rb, line 40 40: def compress_lines(spaced = true) 41: split($/).map { |line| line.strip }.join(spaced ? ' ' : '') 42: end
Matches any whitespace (including newline) and replaces with a single space EXAMPLE:
<<QUERY.compress_lines SELECT name FROM users QUERY => "SELECT name FROM users"
# File lib/data_mapper/support/string.rb, line 40 40: def compress_lines(spaced = true) 41: split($/).map { |line| line.strip }.join(spaced ? ' ' : '') 42: end
# File lib/data_mapper/support/string.rb, line 25 25: def ensure_ends_with(part) 26: [-1,1] == part ? self : (self + part) 27: end
# File lib/data_mapper/support/string.rb, line 25 25: def ensure_ends_with(part) 26: [-1,1] == part ? self : (self + part) 27: end
# File lib/data_mapper/support/string.rb, line 21 21: def ensure_starts_with(part) 22: [0,1] == part ? self : (part + self) 23: end
# File lib/data_mapper/support/string.rb, line 21 21: def ensure_starts_with(part) 22: [0,1] == part ? self : (part + self) 23: end
# File lib/data_mapper/support/string.rb, line 29 29: def ensure_wrapped_with(a, b = nil) 30: ensure_starts_with(a).ensure_ends_with(b || a) 31: end
# File lib/data_mapper/support/string.rb, line 29 29: def ensure_wrapped_with(a, b = nil) 30: ensure_starts_with(a).ensure_ends_with(b || a) 31: end
Useful for heredocs - removes whitespace margin.
# File lib/data_mapper/support/string.rb, line 45 45: def margin(indicator = nil) 46: lines = self.dup.split($/) 47: 48: min_margin = 0 49: lines.each do |line| 50: if line =~ /^(\s+)/ && (min_margin == 0 || $1.size < min_margin) 51: min_margin = $1.size 52: end 53: end 54: lines.map { |line| line.sub(/^\s{#{min_margin}}/, '') }.join($/) 55: end
Useful for heredocs - removes whitespace margin.
# File lib/data_mapper/support/string.rb, line 45 45: def margin(indicator = nil) 46: lines = self.dup.split($/) 47: 48: min_margin = 0 49: lines.each do |line| 50: if line =~ /^(\s+)/ && (min_margin == 0 || $1.size < min_margin) 51: min_margin = $1.size 52: end 53: end 54: lines.map { |line| line.sub(/^\s{#{min_margin}}/, '') }.join($/) 55: end
Formats String for easy translation. Replaces an arbitrary number of values using numeric identifier replacement.
"%s %s %s" % %w(one two three) #=> "one two three" "%3$s %2$s %1$s" % %w(one two three) #=> "three two one"
# File lib/data_mapper/support/string.rb, line 62 62: def t(*values) 63: self.class::translate(self) % values 64: end
Formats String for easy translation. Replaces an arbitrary number of values using numeric identifier replacement.
"%s %s %s" % %w(one two three) #=> "one two three" "%3$s %2$s %1$s" % %w(one two three) #=> "three two one"
# File lib/data_mapper/support/string.rb, line 62 62: def t(*values) 63: self.class::translate(self) % values 64: end