Class: Githug::Level

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

Constant Summary collapse

LEVELS =
[nil, "init", "add", "commit", "config", "clone",
"clone_to_folder", "ignore", "status", "rm", "rm_cached",
"stash", "rename", "log", "tag", "push_tags", "commit_amend", "reset",
"reset_soft", "checkout_file", "remote", "remote_url", "pull",
"remote_add", "push", "diff", "blame", "branch", "checkout",
"checkout_tag", "checkout_tag_over_branch", "branch_at", "merge", "repack", "cherry-pick",
"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, in_stream=, #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 Githug::UI

Instance Attribute Details

#level_noObject

Returns the value of attribute level_no.



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

def level_no
  @level_no
end

#level_pathObject

Returns the value of attribute level_path.



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

def level_path
  @level_path
end

Class Method Details

.load(level_name) ⇒ Object



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

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

.load_from_file(path) ⇒ Object



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

def load_from_file(path)
  setup(path)
end

.setup(path) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/githug/level.rb', line 29

def setup(path)
  level_name = File.basename(path, File.extname(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_no = LEVELS.index(level_name) || 1
  level.level_path = level_path
  level
end

Instance Method Details

#description(description) ⇒ Object



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

def description(description)
  @description = description
end

#difficulty(num) ⇒ Object



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

def difficulty(num)
  @difficulty = num
end

#full_descriptionObject



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

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



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

def hint(&hint)
  @hint = hint
end

#hints(hints) ⇒ Object



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

def hints(hints)
  @hints = hints
end

#init_from_levelObject



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

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

#repo(location = "") ⇒ Object



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

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

#setup(&block) ⇒ Object



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

def setup(&block)
  @setup = block
end

#setup_levelObject



83
84
85
86
# File 'lib/githug/level.rb', line 83

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

#show_hintObject



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/githug/level.rb', line 102

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



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

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

#solveObject



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

def solve
  _solution
rescue
  false
end

#testObject



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

def test
  @solution.call
end