Class: String
- Defined in:
- lib/happy_support/core_ext/object/blank.rb,
lib/happy_support/core_ext/string/access.rb,
lib/happy_support/core_ext/string/indent.rb,
lib/happy_support/core_ext/string/exclude.rb,
lib/happy_support/core_ext/string/filters.rb,
lib/happy_support/core_ext/string/behavior.rb
Instance Method Summary collapse
- #acts_like_string? ⇒ Boolean
- #at(position) ⇒ Object
- #blank? ⇒ Boolean
- #exclude?(string) ⇒ Boolean
- #first(limit = 1) ⇒ Object
- #from(position) ⇒ Object
- #indent(amount, indent_string = nil, indent_empty_lines = false) ⇒ Object
- #indent!(amount, indent_string = nil, indent_empty_lines = false) ⇒ Object
- #last(limit = 1) ⇒ Object
- #squish ⇒ Object
- #squish! ⇒ Object
- #to(position) ⇒ Object
- #truncate(truncate_at, options = {}) ⇒ Object
Instance Method Details
#acts_like_string? ⇒ Boolean
2 3 4 |
# File 'lib/happy_support/core_ext/string/behavior.rb', line 2 def acts_like_string? true end |
#at(position) ⇒ Object
3 4 5 |
# File 'lib/happy_support/core_ext/string/access.rb', line 3 def at(position) self[position] end |
#blank? ⇒ Boolean
44 45 46 |
# File 'lib/happy_support/core_ext/object/blank.rb', line 44 def blank? self !~ /[^[:space:]]/ end |
#exclude?(string) ⇒ Boolean
2 3 4 |
# File 'lib/happy_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/happy_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/happy_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/happy_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/happy_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/happy_support/core_ext/string/access.rb', line 25 def last(limit = 1) if limit == 0 '' elsif limit >= size self else from(-limit) end end |
#squish ⇒ Object
3 4 5 |
# File 'lib/happy_support/core_ext/string/filters.rb', line 3 def squish dup.squish! end |
#squish! ⇒ Object
7 8 9 10 11 12 |
# File 'lib/happy_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/happy_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/happy_support/core_ext/string/filters.rb', line 14 def truncate(truncate_at, = {}) return dup unless length > truncate_at [:omission] ||= '...' length_with_room_for_omission = truncate_at - [:omission].length stop = \ if [:separator] rindex([:separator], length_with_room_for_omission) || length_with_room_for_omission else length_with_room_for_omission end "#{self[0...stop]}#{options[:omission]}" end |