Module: Stubber::CoreExt

Includes:
Base
Included in:
String
Defined in:
lib/stubber/core_ext.rb

Constant Summary

Constants included from Base

Base::WORD

Instance Method Summary collapse

Methods included from Base

#scan_text, #scan_word, #scan_words

Instance Method Details

#scannerStringScanner

Returns:

  • (StringScanner)


8
9
10
# File 'lib/stubber/core_ext.rb', line 8

def scanner
  @scanner ||= StringScanner.new(self)
end

#stub_at_least(min_chars, save_pos = false) ⇒ Object Also known as: stub_thru, stub_through

@param [Fixnum]

@param [true, false]
@return [String]


55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/stubber/core_ext.rb', line 55

def stub_at_least(min_chars, save_pos=false)
  scanner.reset unless save_pos

  if min_chars < 0
    return rev_stub_at_least(min_chars.abs, save_pos)
  end

  start = scanner.pos
  str   = scan_text(scanner, min_chars)

  if(str.size < (min_chars + start))
    str << scanner.matched.to_s
  end

  str
end

#stub_at_most(max_chars, save_pos = false) ⇒ Object Also known as: stub_text, stub_chars, stub_pos, stub_to

@param [Fixnum]

@param [true, false]
@return [String]


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/stubber/core_ext.rb', line 30

def stub_at_most(max_chars, save_pos=false)
  scanner.reset unless save_pos

  str = if max_chars < 0
          rev_stub_at_most(max_chars.abs, save_pos)
        else
          scan_text(scanner, max_chars)
        end

  begin
    scanner.unscan if str.empty? && scanner.pos > max_chars && !scanner.pre_match.nil?
  rescue StringScanner::Error => e
    # Do nothing
  end

  return str
end

#stub_words(max_words, save_pos = false) ⇒ Object Also known as: stub, words

@param [Fixnum]

@param [true, false]
@return [String]


15
16
17
18
19
20
21
22
23
# File 'lib/stubber/core_ext.rb', line 15

def stub_words(max_words, save_pos=false)
  scanner.reset unless save_pos

  if max_words < 0
    rev_stub_words(max_words.abs, save_pos)
  else
    scan_words(scanner, max_words)
  end
end