Module: Stubber::Base
Constant Summary collapse
- WORD =
/\w+[\!\.\?]*[^\s,]/
Instance Method Summary collapse
-
#scan_text(scanner, max_text) ⇒ Object
Scans a given number of words, up to a given character position (but not beyond) @param [StringScanner] @param [Fixnum] @return [String].
-
#scan_word(scanner) ⇒ Object
Scans ahead one word position, and returns it @param [StringScanner] scanner @return [String].
-
#scan_words(scanner, max_words) ⇒ Object
Scans a given number of words from a scanner @param [StringScanner] @param [Fixnum] @return [String].
-
#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
Scans a given number of words, up to a given character position (but not beyond)
@param [StringScanner]
@param [Fixnum]
@return [String]
52 53 54 55 56 57 58 |
# File 'lib/stubber/base.rb', line 52 def scan_text(scanner, max_text) start = scanner.pos until scanner.pos >= (max_text + start) || scan_word(scanner).nil?; end (scanner.pre_match || scanner.string[start, max_text]).to_s end |
#scan_word(scanner) ⇒ Object
Scans ahead one word position, and returns it
@param [StringScanner] scanner
@return [String]
32 33 34 |
# File 'lib/stubber/base.rb', line 32 def scan_word(scanner) scanner.scan_until(WORD) end |
#scan_words(scanner, max_words) ⇒ Object
Scans a given number of words from a scanner
@param [StringScanner]
@param [Fixnum]
@return [String]
40 41 42 43 44 45 46 |
# File 'lib/stubber/base.rb', line 40 def scan_words(scanner, max_words) words = max_words.times.map { scan_word(scanner) } words.compact! words.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
23 24 25 26 27 |
# File 'lib/stubber/base.rb', line 23 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
13 14 15 16 17 |
# File 'lib/stubber/base.rb', line 13 def stub_words(text, max_words) scanner = StringScanner.new(text.to_s) return scan_words(scanner, max_words) end |