Module: Kamen::Parser

Extended by:
Parser
Included in:
Parser
Defined in:
lib/kamen/parser.rb

Overview

begin/=end is temporarily not supported

Constant Summary collapse

TOKENIZER =
"MockData"

Instance Method Summary collapse

Instance Method Details

#find_source_file(params) ⇒ Object

build source file fullpath



14
15
16
17
18
19
# File 'lib/kamen/parser.rb', line 14

def find_source_file params
  source_dir = File.expand_path(File.join(Rails.root, 'app','controllers'))
  basename = "#{params[:controller]}_controller.rb"

  File.join(source_dir, basename)
end

#handle_source_file(params) ⇒ Object

Read source and parse. Kamen will mark that the source is already handled unless you modify the source. So if no mock given, we will pass request to your rails application the next request directly. This will avoid to do some logic repeatly.

return:

true | false , if there is some data written in cache.


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/kamen/parser.rb', line 28

def handle_source_file params
  filepath = find_source_file(params)

  return false unless File.exists?(filepath)

  file = File.open(filepath, 'r')

  _started = false
  _data = []
  _return_val = false

  file.each_line do |line|
    if mch = /^[[:blank:]]*def ([a-z0-9_]+)/.match(line)
      if _data.blank? # current method has no mock data
        next
      else
        if mch[1] == params[:action]  # hit
          key = MockCache.cache_key(params)
          MockCache.write_mock_response(key, _data.join)
          _return_val = true
        else # miss, but there exists a mock
          key = MockCache.cache_key(controller: params[:controller],
                                       action: mch[1])
          MockCache.write_mock_response(key, _data.join)
        end
        _data = []
        next
      end
    end

    if line =~ mock_tokenizer
      _started = !_started
      next
    end
    if _started
      _data << line.gsub(/^[[:blank:]]*\#/, "")
    end
  end
  file.close
  _return_val
end

#mock_tokenizerObject

tokenizer of mock data comment block



9
10
11
# File 'lib/kamen/parser.rb', line 9

def mock_tokenizer
  @@mock_tokenizer ||= Regexp.new(/^[[:blank:]]*\#.*#{TOKENIZER}/)
end