Class: Githug::Level

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

Constant Summary collapse

LEVELS =
[nil, "init", "config", "add", "commit", "clone",
"clone_to_folder", "ignore", "include", "status",
"number_of_files_committed", "rm", "rm_cached", "stash", "rename",
"restructure", "log", "tag", "push_tags", "commit_amend",
"commit_in_future", "reset", "reset_soft", "checkout_file", "remote",
"remote_url", "pull", "remote_add", "push", "diff", "blame", "branch",
"checkout", "checkout_tag", "checkout_tag_over_branch", "branch_at",
"delete_branch", "push_branch", "merge", "fetch", "rebase", "repack", "cherry-pick",
"grep", "rename_commit", "squash", "merge_squash", "reorder", "bisect",
"stage_lines", "find_old_branch", "revert", "restore", "conflict", "contribute"]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from UI

ask, colorize, error, gets, #method_missing, print, puts, request, success, word_box

Dynamic Method Handling

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

Instance Attribute Details

#level_nameObject

Returns the value of attribute level_name.



16
17
18
# File 'lib/githug/level.rb', line 16

def level_name
  @level_name
end

#level_noObject

Returns the value of attribute level_no.



16
17
18
# File 'lib/githug/level.rb', line 16

def level_no
  @level_no
end

#level_pathObject

Returns the value of attribute level_path.



16
17
18
# File 'lib/githug/level.rb', line 16

def level_path
  @level_path
end

Class Method Details

.listObject



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

def list
  return LEVELS - [nil]
end

.load(level_name) ⇒ Object



20
21
22
23
# File 'lib/githug/level.rb', line 20

def load(level_name)
  path = "#{File.dirname(__FILE__)}/../../levels/#{level_name}.rb"
  setup(path)
end

.load_from_file(path) ⇒ Object



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

def load_from_file(path)
  setup(path)
end

.setup(path) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/githug/level.rb', line 33

def setup(path)
  level_path = path.chomp(File.extname(path))
  level = self.new

  return false unless File.exists?(path)

  level.instance_eval(File.read(path))
  level.level_name = File.basename(path, File.extname(path))
  level.level_no = LEVELS.index(level.level_name) || 1
  level.level_path = level_path
  level
end

Instance Method Details

#description(description) ⇒ Object



57
58
59
# File 'lib/githug/level.rb', line 57

def description(description)
  @description = description
end

#difficulty(num) ⇒ Object



53
54
55
# File 'lib/githug/level.rb', line 53

def difficulty(num)
  @difficulty = num
end

#full_descriptionObject



78
79
80
81
82
83
84
85
86
# File 'lib/githug/level.rb', line 78

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

#hint(&hint) ⇒ Object



70
71
72
# File 'lib/githug/level.rb', line 70

def hint(&hint)
  @hint = hint
end

#hints(hints) ⇒ Object



74
75
76
# File 'lib/githug/level.rb', line 74

def hints(hints)
  @hints = hints
end

#init_from_levelObject



48
49
50
51
# File 'lib/githug/level.rb', line 48

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

#repo(location = "") ⇒ Object



93
94
95
# File 'lib/githug/level.rb', line 93

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

#setup(&block) ⇒ Object



66
67
68
# File 'lib/githug/level.rb', line 66

def setup(&block)
  @setup = block
end

#setup_levelObject



88
89
90
91
# File 'lib/githug/level.rb', line 88

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

#show_hintObject



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/githug/level.rb', line 107

def show_hint
  UI.word_box("Githug")
  profile = Profile.load
  current_hint_index = profile.current_hint_index
  if @hints
    puts @hints[current_hint_index]
    if current_hint_index < @hints.size - 1
      profile.current_hint_index += 1
      profile.save
    else
      profile.current_hint_index = 0
      profile.save
    end
  elsif @hint
    @hint.call
  else
    UI.puts("No hints available for this level.")
  end
end

#solution(&block) ⇒ Object



61
62
63
64
# File 'lib/githug/level.rb', line 61

def solution(&block)
  singleton = class << self; self end
  singleton.send :define_method, :_solution, &block
end

#solveObject



97
98
99
100
101
# File 'lib/githug/level.rb', line 97

def solve
  _solution
rescue
  false
end

#testObject



103
104
105
# File 'lib/githug/level.rb', line 103

def test
  _solution
end