Module: DataMapper::Support::String
- Included in:
- String
- Defined in:
- lib/data_mapper/support/string.rb
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
-
.included(base) ⇒ Object
I set the constant on the String itself to avoid inheritance chain lookups.
Instance Method Summary collapse
-
#compress_lines(spaced = true) ⇒ Object
Matches any whitespace (including newline) and replaces with a single space EXAMPLE: <<QUERY.compress_lines SELECT name FROM users QUERY => “SELECT name FROM users”.
- #ensure_ends_with(part) ⇒ Object
- #ensure_starts_with(part) ⇒ Object
- #ensure_wrapped_with(a, b = nil) ⇒ Object
- #margin(indicator = nil) ⇒ Object
-
#t(*values) ⇒ Object
Formats String for easy translation.
Class Method Details
.included(base) ⇒ Object
I set the constant on the String itself to avoid inheritance chain lookups.
6 7 8 |
# File 'lib/data_mapper/support/string.rb', line 6 def self.included(base) base.extend(ClassMethods) end |
Instance Method Details
#compress_lines(spaced = true) ⇒ Object
Matches any whitespace (including newline) and replaces with a single space EXAMPLE:
<<QUERY.compress_lines
SELECT name
FROM users
QUERY
=> "SELECT name FROM users"
40 41 42 |
# File 'lib/data_mapper/support/string.rb', line 40 def compress_lines(spaced = true) split($/).map { |line| line.strip }.join(spaced ? ' ' : '') end |
#ensure_ends_with(part) ⇒ Object
25 26 27 |
# File 'lib/data_mapper/support/string.rb', line 25 def ensure_ends_with(part) [-1,1] == part ? self : (self + part) end |
#ensure_starts_with(part) ⇒ Object
21 22 23 |
# File 'lib/data_mapper/support/string.rb', line 21 def ensure_starts_with(part) [0,1] == part ? self : (part + self) end |
#ensure_wrapped_with(a, b = nil) ⇒ Object
29 30 31 |
# File 'lib/data_mapper/support/string.rb', line 29 def ensure_wrapped_with(a, b = nil) ensure_starts_with(a).ensure_ends_with(b || a) end |
#margin(indicator = nil) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/data_mapper/support/string.rb', line 44 def margin(indicator = nil) target = dup lines = target.split($/) if indicator.nil? min_margin = nil lines.each do |line| if line =~ /(\s+)/ && (min_margin.nil? || $1.size < min_margin) min_margin = $1.size end end lines.map do |line| line.sub(/^\s{#{min_margin}}/, '') end.join($/) else lines.map do |line| line.sub(/^.*?#{"\\" + indicator}/, '') end.join($/) end end |
#t(*values) ⇒ Object
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"
71 72 73 |
# File 'lib/data_mapper/support/string.rb', line 71 def t(*values) self.class::translate(self) % values end |