Class: Bookingit::CodeBlockInterpreter

Inherits:
Object
  • Object
show all
Defined in:
lib/bookingit/code_block_interpreter.rb

Instance Method Summary collapse

Constructor Details

#initialize(code) ⇒ CodeBlockInterpreter

Returns a new instance of CodeBlockInterpreter.



3
4
5
6
# File 'lib/bookingit/code_block_interpreter.rb', line 3

def initialize(code)
  @code = code.strip
  @result = nil
end

Instance Method Details

#otherwise(&block) ⇒ Object



64
65
66
67
# File 'lib/bookingit/code_block_interpreter.rb', line 64

def otherwise(&block)
  @result = block.call if @result.nil?
  self
end

#resultObject



69
70
71
72
# File 'lib/bookingit/code_block_interpreter.rb', line 69

def result
  raise "You didn't handle every possible case" if @result.nil?
  @result
end

#when_file(&block) ⇒ Object



8
9
10
11
12
13
14
# File 'lib/bookingit/code_block_interpreter.rb', line 8

def when_file(&block)
  if @code =~ /^\s*file:\/\/(.*)$/
    path = $1
    @result = block.call(path)
  end
  self
end

#when_file_in_git(&block) ⇒ Object



16
17
18
19
20
21
22
23
24
25
# File 'lib/bookingit/code_block_interpreter.rb', line 16

def when_file_in_git(&block)
  when_git_reference do |repo_path,path_in_repo,reference|
    if reference !~ /^(.+)\.\.(.+)$/ && reference !~ /^(.+)\!(.+)$/ && reference !~ /^\.\.(.+)$/
      chdir repo_path do
        @result = block.call(path_in_repo,reference)
      end
    end
  end
  self
end

#when_git_diff(&block) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/bookingit/code_block_interpreter.rb', line 27

def when_git_diff(&block)
  when_git_reference do |repo_path,path_in_repo,reference|
    if reference =~ /^(.+)\.\.(.+)$/
      chdir repo_path do
        @result = block.call(path_in_repo,reference)
      end
    elsif reference =~ /^\.\.(.+)$/
      tag_or_sha = $1
      chdir repo_path do
        @result = block.call(path_in_repo,"#{tag_or_sha}^..#{tag_or_sha}")
      end
    end
  end
  self
end

#when_shell_command(&block) ⇒ Object



55
56
57
58
59
60
61
62
# File 'lib/bookingit/code_block_interpreter.rb', line 55

def when_shell_command(&block)
  if @code.strip =~ /^\s*sh:\/\/(.+)#([^#]+)$/
    path = $1
    command,exit_type = parse_shell_command($2)
    @result = block.call(shell_command(path: path,command: command, exit_type: exit_type))
  end
  self
end

#when_shell_command_in_git(&block) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/bookingit/code_block_interpreter.rb', line 43

def when_shell_command_in_git(&block)
  when_git_reference do |repo_path,path_in_repo,reference|
    if reference =~ /^([^!]+)\!(.+)$/
      reference = $1
      command,exit_type  = parse_shell_command($2)
      chdir repo_path do
        @result = block.call(reference,shell_command(path: path_in_repo,command: command, exit_type: exit_type))
      end
    end
  end
end