Class: String
- Inherits:
-
Object
show all
- Defined in:
- lib/sinarey_support/core_ext/object/blank.rb,
lib/sinarey_support/core_ext/string/access.rb,
lib/sinarey_support/core_ext/string/indent.rb,
lib/sinarey_support/core_ext/string/exclude.rb,
lib/sinarey_support/core_ext/string/filters.rb,
lib/sinarey_support/core_ext/string/behavior.rb,
lib/sinarey_support/core_ext/string/output_safety.rb
Instance Method Summary
collapse
Instance Method Details
#acts_like_string? ⇒ Boolean
2
3
4
|
# File 'lib/sinarey_support/core_ext/string/behavior.rb', line 2
def acts_like_string?
true
end
|
#at(position) ⇒ Object
3
4
5
|
# File 'lib/sinarey_support/core_ext/string/access.rb', line 3
def at(position)
self[position]
end
|
#blank? ⇒ Boolean
44
45
46
|
# File 'lib/sinarey_support/core_ext/object/blank.rb', line 44
def blank?
self !~ /[^[:space:]]/
end
|
#exclude?(string) ⇒ Boolean
2
3
4
|
# File 'lib/sinarey_support/core_ext/string/exclude.rb', line 2
def exclude?(string)
!include?(string)
end
|
#first(limit = 1) ⇒ Object
15
16
17
18
19
20
21
22
23
|
# File 'lib/sinarey_support/core_ext/string/access.rb', line 15
def first(limit = 1)
if limit == 0
''
elsif limit >= size
self
else
to(limit - 1)
end
end
|
#from(position) ⇒ Object
7
8
9
|
# File 'lib/sinarey_support/core_ext/string/access.rb', line 7
def from(position)
self[position..-1]
end
|
#indent(amount, indent_string = nil, indent_empty_lines = false) ⇒ Object
9
10
11
|
# File 'lib/sinarey_support/core_ext/string/indent.rb', line 9
def indent(amount, indent_string=nil, indent_empty_lines=false)
dup.tap {|_| _.indent!(amount, indent_string, indent_empty_lines)}
end
|
#indent!(amount, indent_string = nil, indent_empty_lines = false) ⇒ Object
3
4
5
6
7
|
# File 'lib/sinarey_support/core_ext/string/indent.rb', line 3
def indent!(amount, indent_string=nil, indent_empty_lines=false)
indent_string = indent_string || self[/^[ \t]/] || ' '
re = indent_empty_lines ? /^/ : /^(?!$)/
gsub!(re, indent_string * amount)
end
|
#last(limit = 1) ⇒ Object
25
26
27
28
29
30
31
32
33
|
# File 'lib/sinarey_support/core_ext/string/access.rb', line 25
def last(limit = 1)
if limit == 0
''
elsif limit >= size
self
else
from(-limit)
end
end
|
3
4
5
|
# File 'lib/sinarey_support/core_ext/string/filters.rb', line 3
def squish
dup.squish!
end
|
7
8
9
10
11
12
|
# File 'lib/sinarey_support/core_ext/string/filters.rb', line 7
def squish!
gsub!(/\A[[:space:]]+/, '')
gsub!(/[[:space:]]+\z/, '')
gsub!(/[[:space:]]+/, ' ')
self
end
|
#to(position) ⇒ Object
11
12
13
|
# File 'lib/sinarey_support/core_ext/string/access.rb', line 11
def to(position)
self[0..position]
end
|
#truncate(truncate_at, options = {}) ⇒ Object
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
# File 'lib/sinarey_support/core_ext/string/filters.rb', line 14
def truncate(truncate_at, options = {})
return dup unless length > truncate_at
options[:omission] ||= '...'
length_with_room_for_omission = truncate_at - options[:omission].length
stop = \
if options[:separator]
rindex(options[:separator], length_with_room_for_omission) || length_with_room_for_omission
else
length_with_room_for_omission
end
"#{self[0...stop]}#{options[:omission]}"
end
|