module Kamen::Parser

begin/=end is temporarily not supported

Constants

TOKENIZER

Public Instance Methods

find_source_file(params) click to toggle source

build source file fullpath

# 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) click to toggle source

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.
# 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_tokenizer() click to toggle source

tokenizer of mock data comment block

# File lib/kamen/parser.rb, line 9
def mock_tokenizer
  @@mock_tokenizer ||= Regexp.new(/^[[:blank:]]*\#.*#{TOKENIZER}/)
end