Module: StringStubber::Base
- Included in:
- StringStubber, CoreExt
- Defined in:
- lib/string_stubber/base.rb
Constant Summary collapse
- WORD =
/[\w[:punct:]]+/
- SNIP =
/\s+$/
Instance Method Summary collapse
- #scan_text(scanner, max_text) ⇒ Object
- #scan_word(scanner) ⇒ Object
- #scan_words(scanner, max_words) ⇒ Object
-
#stub_text(text, max_text) ⇒ Object
Stubs the given text string a number of whole-words, not to go beyond the given text position @param [String] text Any piece of text @param [Fixnum] max_text Text position that delimits the desired number of whole words @return [String] The text, stubbed at the max_text position.
-
#stub_words(text, max_words) ⇒ Object
Stubs a given text string, up to a given number of words @param [String] text Any piece of text @param [Fixnum] max_words The desired number of words @return [String] The text, stubbed at max_words number of words.
Instance Method Details
#scan_text(scanner, max_text) ⇒ Object
36 37 38 39 40 41 42 |
# File 'lib/string_stubber/base.rb', line 36 def scan_text(scanner, max_text) start = scanner.pos until scanner.pos >= max_text || scanner.scan_until(WORD).nil?; end (str = scanner.pre_match || scanner.string[start, max_text]).gsub!(SNIP, '') end |
#scan_word(scanner) ⇒ Object
26 27 28 |
# File 'lib/string_stubber/base.rb', line 26 def scan_word(scanner) scanner.scan_until(WORD) end |
#scan_words(scanner, max_words) ⇒ Object
30 31 32 33 34 |
# File 'lib/string_stubber/base.rb', line 30 def scan_words(scanner, max_words) max_words.times.map { scanner.scan_until(WORD) }.compact.join end |
#stub_text(text, max_text) ⇒ Object
Stubs the given text string a number of whole-words, not to go beyond the given text position
@param [String] text Any piece of text
@param [Fixnum] max_text Text position that delimits the desired number of whole words
@return [String] The text, stubbed at the max_text position
20 21 22 23 24 |
# File 'lib/string_stubber/base.rb', line 20 def stub_text(text, max_text) scanner = StringScanner.new(text.to_s) return scan_text(scanner, max_text) end |
#stub_words(text, max_words) ⇒ Object
Stubs a given text string, up to a given number of words
@param [String] text Any piece of text
@param [Fixnum] max_words The desired number of words
@return [String] The text, stubbed at max_words number of words
10 11 12 13 14 |
# File 'lib/string_stubber/base.rb', line 10 def stub_words(text, max_words) scanner = StringScanner.new(text.to_s) return scan_words(scanner, max_words) end |