Class: Githug::Level

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

Constant Summary collapse

LEVELS =

LEVELS = [nil, “init”, “config”, “alias”, “alias2”, “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", "rebase_onto", "repack", "cherry-pick",
"grep", "rename_commit", "squash", "merge_squash", "reorder", "bisect",
"stage_lines", "find_old_branch", "revert", "restore", "conflict",
"submodule","contribute"]
[nil, "config", "clone", "alias_st", "alias_lg",  "log", 
  "add", "commit", "ignore", "stash", "unstash",
   "pull",  "pull_rebase", "conflict", "rebase_conflict", "push_branch",
   # "fetch",
    "revert", "merge", "cherry-pick",
  "congratulation", "rm", "rm_cached", "tag", "push_tags", "commit_amend",
  "reset", "reset_soft", "checkout_file", "remote",
          "remote_url", "remote_add", "push", "diff", "blame", "branch",
          "checkout", "checkout_tag", "checkout_tag_over_branch", "branch_at",
          "delete_branch", "fetch", "rebase", "rebase_onto", 
          # "repack", 
          "grep", "rename_commit", "squash", "merge_squash", "reorder", "bisect",
          "stage_lines", "find_old_branch",  "restore",
          "submodule","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.



34
35
36
# File 'lib/githug/level.rb', line 34

def level_name
  @level_name
end

#level_noObject

Returns the value of attribute level_no.



34
35
36
# File 'lib/githug/level.rb', line 34

def level_no
  @level_no
end

#level_pathObject

Returns the value of attribute level_path.



34
35
36
# File 'lib/githug/level.rb', line 34

def level_path
  @level_path
end

Class Method Details

.listObject



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

def list
  return LEVELS - [nil]
end

.load(level_name) ⇒ Object



38
39
40
41
# File 'lib/githug/level.rb', line 38

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

.load_from_file(path) ⇒ Object



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

def load_from_file(path)
  setup(path)
end

.setup(path) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/githug/level.rb', line 51

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



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

def description(description)
  @description = description
end

#difficulty(num) ⇒ Object



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

def difficulty(num)
  @difficulty = num
end

#full_descriptionObject



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

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



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

def hint(&hint)
  @hint = hint
end

#hints(hints) ⇒ Object



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

def hints(hints)
  @hints = hints
end

#init_from_levelObject



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

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

#repo(location = "") ⇒ Object



111
112
113
# File 'lib/githug/level.rb', line 111

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

#setup(&block) ⇒ Object



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

def setup(&block)
  @setup = block
end

#setup_levelObject



106
107
108
109
# File 'lib/githug/level.rb', line 106

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

#show_hintObject



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/githug/level.rb', line 125

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



79
80
81
82
# File 'lib/githug/level.rb', line 79

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

#solveObject



115
116
117
118
119
# File 'lib/githug/level.rb', line 115

def solve
  _solution
rescue
  false
end

#testObject



121
122
123
# File 'lib/githug/level.rb', line 121

def test
  _solution
end