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
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]
49 50 51 52 53 54 55 |
# File 'lib/string_stubber/base.rb', line 49 def scan_text(scanner, max_text) start = scanner.pos until scanner.pos >= max_text || scan_word(scanner).nil?; end (scanner.pre_match || scanner.string[start, max_text]).to_s.gsub(SNIP, '') end |
#scan_word(scanner) ⇒ Object
Scans ahead one word position, and returns it @param [StringScanner] scanner @return [String]
29 30 31 |
# File 'lib/string_stubber/base.rb', line 29 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]
37 38 39 40 41 42 43 |
# File 'lib/string_stubber/base.rb', line 37 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
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 |