Class: Gitscrub::Level

Inherits:
Object
  • Object
show all
Includes:
UI
Defined in:
lib/gitscrub/level.rb

Constant Summary collapse

LEVELS =
[nil, "init", "add", "commit", "config", "clone", "clone_to_folder", "status", "diff", "blame",  "contribute"]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from UI

ask, colorize, error, gets, in_stream=, line, #method_missing, out_stream=, print, puts, request, success, word_box

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Gitscrub::UI

Instance Attribute Details

#level_noObject

Returns the value of attribute level_no.



7
8
9
# File 'lib/gitscrub/level.rb', line 7

def level_no
  @level_no
end

#level_pathObject

Returns the value of attribute level_path.



7
8
9
# File 'lib/gitscrub/level.rb', line 7

def level_path
  @level_path
end

Class Method Details

.load(level_no) ⇒ Object



11
12
13
14
15
16
17
18
19
20
# File 'lib/gitscrub/level.rb', line 11

def load(level_no)
  level = new
  level_path = "#{File.dirname(__FILE__)}/../../levels/#{LEVELS[level_no]}"
  location = "#{level_path}.rb"
  return false unless File.exists?(location)
  level.instance_eval(File.read(location))
  level.level_no = level_no
  level.level_path = level_path
  level
end

Instance Method Details

#description(description) ⇒ Object



33
34
35
# File 'lib/gitscrub/level.rb', line 33

def description(description)
  @description = description
end

#difficulty(num) ⇒ Object



29
30
31
# File 'lib/gitscrub/level.rb', line 29

def difficulty(num)
  @difficulty = num
end

#full_descriptionObject



49
50
51
52
53
54
55
56
# File 'lib/gitscrub/level.rb', line 49

def full_description
  UI.puts
  UI.puts "Level: #{level_no}"
  UI.puts "Difficulty: #{"*"*@difficulty}"
  UI.puts
  UI.puts @description
  UI.puts
end

#hint(&hint) ⇒ Object



45
46
47
# File 'lib/gitscrub/level.rb', line 45

def hint(&hint)
  @hint = hint
end

#init_from_levelObject



24
25
26
27
# File 'lib/gitscrub/level.rb', line 24

def init_from_level
  FileUtils.cp_r("#{level_path}/.", ".")
  FileUtils.mv(".gitscrub", ".git")
end

#repo(location = "") ⇒ Object



63
64
65
# File 'lib/gitscrub/level.rb', line 63

def repo(location = "")
  @repo ||= Repository.new(location)
end

#setup(&block) ⇒ Object



41
42
43
# File 'lib/gitscrub/level.rb', line 41

def setup(&block)
  @setup = block 
end

#setup_levelObject



58
59
60
61
# File 'lib/gitscrub/level.rb', line 58

def setup_level
  repo.reset
  @setup.call if @setup
end

#show_hintObject



74
75
76
77
78
79
80
81
# File 'lib/gitscrub/level.rb', line 74

def show_hint
  UI.word_box("Gitscrub")
  if @hint
    @hint.call
  else
    UI.puts("No hints available for this level")
  end
end

#solution(&block) ⇒ Object



37
38
39
# File 'lib/gitscrub/level.rb', line 37

def solution(&block)
  @solution = block
end

#solveObject



67
68
69
70
71
# File 'lib/gitscrub/level.rb', line 67

def solve
  @solution.call
rescue
  false
end