gitscrub 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ *.swp
6
+ .rvmrc
7
+ git_scrub/*
8
+ .profile.yml
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in gitscrub.gemspec
4
+ gemspec
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/bin/gitscrub ADDED
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ require 'gitscrub/cli'
3
+
4
+ Gitscrub::CLI.start
data/gitscrub.gemspec ADDED
@@ -0,0 +1,27 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "gitscrub/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "gitscrub"
7
+ s.version = Gitscrub::VERSION
8
+ s.authors = ["Gary Rennie"]
9
+ s.email = ["webmaster@gazler.com"]
10
+ s.homepage = ""
11
+ s.summary = %q{An interactive way to learn git.}
12
+ s.description = %q{An interactive way to learn git.}
13
+
14
+ s.rubyforge_project = "gitscrub"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ # specify any dependencies here; for example:
22
+ s.add_development_dependency "rspec", "~>2.8.0"
23
+
24
+ s.add_dependency "grit", "~>2.4.1"
25
+ s.add_dependency "thor", "~>0.14.6"
26
+ # s.add_runtime_dependency "rest-client"
27
+ end
data/levels/1.rb ADDED
@@ -0,0 +1,5 @@
1
+ difficulty 1
2
+ description "Initialize an empty repository"
3
+ solution do
4
+ Grit::Repo.new(".")
5
+ end
@@ -0,0 +1,46 @@
1
+ require 'thor'
2
+ require 'gitscrub'
3
+ module Gitscrub
4
+ class CLI < Thor
5
+
6
+
7
+ default_task :setup
8
+
9
+ desc :setup, "Initialize the game"
10
+
11
+ def setup
12
+ UI.word_box("Gitscrub")
13
+ make_directory
14
+ game = Game.new
15
+ game.play_level
16
+ end
17
+
18
+ desc :check, "check your current solution"
19
+
20
+ def check
21
+ true
22
+ end
23
+
24
+ desc :reset, "reset the current level"
25
+
26
+ def reset
27
+ true
28
+ end
29
+
30
+ no_tasks do
31
+
32
+ def make_directory
33
+ unless File.exists?("./git_scrub") || Dir.pwd.split("/").last == "git_scrub"
34
+ if UI.ask("No gitscrubber directory found, do you wish to create one?")
35
+ Dir.mkdir("./git_scrub")
36
+ else
37
+ UI.puts("Exiting")
38
+ exit
39
+ end
40
+ end
41
+ end
42
+
43
+ end
44
+
45
+ end
46
+ end
@@ -0,0 +1,30 @@
1
+ module Gitscrub
2
+ class Game
3
+
4
+ attr_accessor :profile
5
+
6
+ def initialize
7
+ @profile = Profile.load
8
+ end
9
+
10
+ def play_level
11
+ solve = true
12
+ if profile.level == 0
13
+ UI.puts("Welcome to Gitscrub")
14
+ profile.level = 1
15
+ profile.save
16
+ solve = false
17
+ else
18
+ level = Level.load(profile.level)
19
+ if solve
20
+ if level.solve
21
+ UI.puts "Congratulations, you have solved the level"
22
+ else
23
+ UI.puts "Sorry, this solution is not quite right!"
24
+ end
25
+ end
26
+ end
27
+ end
28
+
29
+ end
30
+ end
@@ -0,0 +1,37 @@
1
+ module Gitscrub
2
+ class Level
3
+
4
+ attr_accessor :ldifficulty, :ldescription, :lsolution
5
+
6
+ class << self
7
+
8
+ def load(level_no)
9
+ level = new
10
+ location = "#{File.dirname(__FILE__)}/../../levels/#{level_no}.rb"
11
+ level.instance_eval(File.read(location))
12
+ level
13
+ end
14
+
15
+ end
16
+
17
+ def difficulty(num)
18
+ @ldifficulty = num
19
+ end
20
+
21
+ def description(description)
22
+ @ldescription = description
23
+ end
24
+
25
+ def solution(&block)
26
+ @lsolution = block
27
+ end
28
+
29
+ def solve
30
+ lsolution.call
31
+ true
32
+ rescue
33
+ false
34
+ end
35
+
36
+ end
37
+ end
@@ -0,0 +1,41 @@
1
+ require 'yaml'
2
+ module Gitscrub
3
+ class Profile
4
+ PROFILE_FILE = ".profile.yml"
5
+
6
+ attr_accessor :settings
7
+
8
+ class << self
9
+ def load
10
+ settings = {
11
+ :level => 0
12
+ }
13
+
14
+ settings.merge! YAML::load(File.open(PROFILE_FILE)) if File.exists?(PROFILE_FILE)
15
+ self.new(settings)
16
+ end
17
+ end
18
+
19
+
20
+ def method_missing(method, *args, &block)
21
+ if method.to_s.end_with?("=")
22
+ method = method.to_s.chop.to_sym
23
+ return settings[method] = args[0] if settings.include?(method)
24
+ end
25
+ return(settings[method]) if settings.include?(method)
26
+ super
27
+ end
28
+
29
+ def initialize(settings)
30
+ @settings = settings
31
+ end
32
+
33
+ def save
34
+ File.open(PROFILE_FILE, 'w') do |out|
35
+ YAML.dump(settings, out)
36
+ end
37
+ end
38
+
39
+
40
+ end
41
+ end
@@ -0,0 +1,56 @@
1
+ module Gitscrub
2
+ class UI
3
+
4
+ @@out_stream
5
+ @@in_stream
6
+
7
+ class << self
8
+
9
+ def out_stream=(out)
10
+ @@out_stream = out
11
+ end
12
+
13
+ def in_stream=(in_stream)
14
+ @@in_stream = in_stream
15
+ end
16
+
17
+ def puts(string)
18
+ @@out_stream.puts(string)
19
+ end
20
+
21
+ def print(string)
22
+ @@out_stream.print(string)
23
+ end
24
+
25
+ def gets
26
+ @@in_stream.gets
27
+ end
28
+
29
+ def line
30
+ puts("*"*80)
31
+ end
32
+
33
+ def word_box(string)
34
+ space_length = (80/2) - ((string.length/2)+1)
35
+ line
36
+ print "*"
37
+ print " "*space_length
38
+ print string
39
+ print " "*space_length
40
+ puts "*"
41
+ line
42
+ end
43
+
44
+ def request(msg)
45
+ print(msg)
46
+ gets.chomp
47
+ end
48
+
49
+ def ask(msg)
50
+ request("#{msg} [yn] ") == 'y'
51
+ end
52
+
53
+ end
54
+
55
+ end
56
+ end
@@ -0,0 +1,3 @@
1
+ module Gitscrub
2
+ VERSION = "0.0.2"
3
+ end
data/lib/gitscrub.rb ADDED
@@ -0,0 +1,10 @@
1
+ require 'grit'
2
+ require "gitscrub/version"
3
+
4
+ require 'gitscrub/ui'
5
+ require 'gitscrub/game'
6
+ require 'gitscrub/profile'
7
+ require 'gitscrub/level'
8
+
9
+ Gitscrub::UI.in_stream = STDIN
10
+ Gitscrub::UI.out_stream = STDOUT
@@ -0,0 +1,43 @@
1
+ require 'spec_helper'
2
+ require 'gitscrub/cli'
3
+
4
+ describe Gitscrub::CLI do
5
+
6
+ before(:each) do
7
+ game = mock.as_null_object
8
+ @cli = Gitscrub::CLI.new
9
+ Gitscrub::Game.stub(:new).and_return(game)
10
+ end
11
+
12
+ it "should print the logo" do
13
+ Gitscrub::UI.should_receive(:word_box).with("Gitscrub")
14
+ @cli.stub(:make_directory)
15
+ @cli.setup
16
+ end
17
+
18
+ it "should create a directory if one does not exist" do
19
+ Gitscrub::UI.stub(:ask).and_return(true)
20
+ Dir.should_receive(:mkdir).with("./git_scrub")
21
+ @cli.make_directory
22
+ end
23
+
24
+ it "should not make a directory if you are in the game directory" do
25
+ Dir.stub(:pwd).and_return("/home/git_scrub")
26
+ Gitscrub::UI.should_not_receive(:ask)
27
+ @cli.make_directory
28
+ end
29
+
30
+ it "should create a directory if one does not exist" do
31
+ Gitscrub::UI.stub(:ask).and_return(false)
32
+ lambda {@cli.make_directory}.should raise_error(SystemExit)
33
+ end
34
+
35
+ it "should check the current solution" do
36
+ @cli.check.should eql(true)
37
+ end
38
+
39
+ it "should reset the current level" do
40
+ @cli.check.should eql(true)
41
+ end
42
+
43
+ end
@@ -0,0 +1,41 @@
1
+ require 'spec_helper'
2
+
3
+ describe Gitscrub::Game do
4
+
5
+ before(:each) do
6
+ @profile = mock
7
+ Gitscrub::Profile.stub(:new).and_return(@profile)
8
+ @game = Gitscrub::Game.new
9
+ @profile.stub(:level).and_return(1)
10
+ @profile.stub(:save)
11
+ @level = mock
12
+ Gitscrub::UI.stub(:puts)
13
+ Gitscrub::Level.stub(:load).and_return(@level)
14
+ end
15
+
16
+ it "should have a profile" do
17
+ @game.profile.should eql(@profile)
18
+ end
19
+
20
+ it "should show a description if the level is 0" do
21
+ @level.should_not_receive(:solve)
22
+ @profile.stub(:level).and_return(0)
23
+ @profile.should_receive(:save)
24
+ Gitscrub::UI.should_receive(:puts).with("Welcome to Gitscrub")
25
+ @profile.should_receive(:level=).with(1)
26
+ @game.play_level
27
+ end
28
+
29
+ it "should echo congratulations if the level is solved" do
30
+ @level.stub(:solve).and_return(true)
31
+ Gitscrub::UI.should_receive(:puts).with("Congratulations, you have solved the level")
32
+ @game.play_level
33
+ end
34
+
35
+ it "should echo congratulations if the level is solved" do
36
+ @level.stub(:solve).and_return(false)
37
+ Gitscrub::UI.should_receive(:puts).with("Sorry, this solution is not quite right!")
38
+ @game.play_level
39
+ end
40
+
41
+ end
@@ -0,0 +1,35 @@
1
+ require 'spec_helper'
2
+ require 'grit'
3
+
4
+ describe Gitscrub::Level do
5
+
6
+ before(:each) do
7
+ @file = <<-eof
8
+ difficulty 1
9
+ description "A test description"
10
+ solution do
11
+ Grit::Repo.new("gitscrub/notadir")
12
+ end
13
+ eof
14
+ File.stub(:read).and_return(@file)
15
+ @level = Gitscrub::Level.load(1)
16
+ end
17
+
18
+ it "should load the level" do
19
+ File.stub(:dirname).and_return("")
20
+ File.should_receive(:read).with('/../../levels/1.rb').and_return(@file)
21
+ level = Gitscrub::Level.load(1)
22
+ level.ldifficulty.should eql(1)
23
+ level.ldescription.should eql("A test description")
24
+ end
25
+
26
+ it "should solve the problem" do
27
+ @level.solve.should eql(false)
28
+ end
29
+
30
+ it "should return true if the requirements have been met" do
31
+ Grit::Repo.stub(:new).and_return(true)
32
+ @level.solve.should eql(true)
33
+ end
34
+
35
+ end
@@ -0,0 +1,36 @@
1
+ require 'spec_helper'
2
+
3
+ describe Gitscrub::Profile do
4
+
5
+ before(:each) do
6
+ end
7
+
8
+ it "should load the profile" do
9
+ settings = {:level => 1}
10
+ File.should_receive(:exists?).with(Gitscrub::Profile::PROFILE_FILE).and_return(true)
11
+ File.should_receive(:open).with(Gitscrub::Profile::PROFILE_FILE).and_return("settings")
12
+ YAML.should_receive(:load).with("settings").and_return(settings)
13
+ Gitscrub::Profile.should_receive(:new).with(settings)
14
+ Gitscrub::Profile.load
15
+ end
16
+
17
+ it "should load the defaults if the file does not exist" do
18
+ defaults = {:level => 0}
19
+ Gitscrub::Profile.should_receive(:new).with(defaults)
20
+ Gitscrub::Profile.load
21
+ end
22
+
23
+ it "should allow method acces to getters and setters" do
24
+ profile = Gitscrub::Profile.load
25
+ profile.level.should eql(0)
26
+ profile.level = 1
27
+ profile.level.should eql(1)
28
+ end
29
+
30
+ it "should save the file" do
31
+ profile = Gitscrub::Profile.load
32
+ File.should_receive(:open).with(Gitscrub::Profile::PROFILE_FILE, "w")
33
+ profile.save
34
+ end
35
+
36
+ end
@@ -0,0 +1,69 @@
1
+ require 'spec_helper'
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Gitscrub::UI do
6
+
7
+ before(:each) do
8
+ @ui = Gitscrub::UI
9
+ @out = StringIO.new
10
+ @in = StringIO.new
11
+ @ui.out_stream = @out
12
+ @ui.in_stream = @in
13
+ end
14
+
15
+ it "should put to the stream" do
16
+ @ui.puts("hello")
17
+ @out.string.should eql("hello\n")
18
+ end
19
+
20
+ it "should print without a new line" do
21
+ @ui.print("hello")
22
+ @out.string.should eql("hello")
23
+ end
24
+
25
+ it "should fetch gets from input stream" do
26
+ @in.puts "bar"
27
+ @in.rewind
28
+ @ui.gets.should == "bar\n"
29
+ end
30
+
31
+ it "should make a line" do
32
+ @ui.line
33
+ @out.string.should eql("*"*80+"\n")
34
+ end
35
+
36
+ it "should make a wordbox" do
37
+ word_box = <<-eof
38
+ ********************************************************************************
39
+ * Gitscrub *
40
+ ********************************************************************************
41
+ eof
42
+ @ui.word_box("Gitscrub")
43
+ @out.string.should eql(word_box)
44
+ end
45
+
46
+
47
+ it "should request text input" do
48
+ @in.puts "bar"
49
+ @in.rewind
50
+ @ui.request("foo").should == "bar"
51
+ @out.string.should == "foo"
52
+ end
53
+
54
+ it "should ask for yes/no and return true when yes" do
55
+ @ui.should_receive(:request).with('foo? [yn] ').and_return('y')
56
+ @ui.ask("foo?").should be_true
57
+ end
58
+
59
+ it "should ask for yes/no and return false when no" do
60
+ @ui.stub(:request).and_return('n')
61
+ @ui.ask("foo?").should be_false
62
+ end
63
+
64
+ it "should ask for yes/no and return false for any input" do
65
+ @ui.stub(:request).and_return('aklhasdf')
66
+ @ui.ask("foo?").should be_false
67
+ end
68
+
69
+ end
@@ -0,0 +1 @@
1
+ require './lib/gitscrub.rb'
metadata ADDED
@@ -0,0 +1,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gitscrub
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.2
6
+ platform: ruby
7
+ authors:
8
+ - Gary Rennie
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2012-03-07 00:00:00 +00:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: rspec
18
+ prerelease: false
19
+ requirement: &id001 !ruby/object:Gem::Requirement
20
+ none: false
21
+ requirements:
22
+ - - ~>
23
+ - !ruby/object:Gem::Version
24
+ version: 2.8.0
25
+ type: :development
26
+ version_requirements: *id001
27
+ - !ruby/object:Gem::Dependency
28
+ name: grit
29
+ prerelease: false
30
+ requirement: &id002 !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ~>
34
+ - !ruby/object:Gem::Version
35
+ version: 2.4.1
36
+ type: :runtime
37
+ version_requirements: *id002
38
+ - !ruby/object:Gem::Dependency
39
+ name: thor
40
+ prerelease: false
41
+ requirement: &id003 !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ~>
45
+ - !ruby/object:Gem::Version
46
+ version: 0.14.6
47
+ type: :runtime
48
+ version_requirements: *id003
49
+ description: An interactive way to learn git.
50
+ email:
51
+ - webmaster@gazler.com
52
+ executables:
53
+ - gitscrub
54
+ extensions: []
55
+
56
+ extra_rdoc_files: []
57
+
58
+ files:
59
+ - .gitignore
60
+ - .rspec
61
+ - Gemfile
62
+ - Rakefile
63
+ - bin/gitscrub
64
+ - gitscrub.gemspec
65
+ - levels/1.rb
66
+ - lib/gitscrub.rb
67
+ - lib/gitscrub/cli.rb
68
+ - lib/gitscrub/game.rb
69
+ - lib/gitscrub/level.rb
70
+ - lib/gitscrub/profile.rb
71
+ - lib/gitscrub/ui.rb
72
+ - lib/gitscrub/version.rb
73
+ - spec/gitscrub/cli_spec.rb
74
+ - spec/gitscrub/game_spec.rb
75
+ - spec/gitscrub/level_spec.rb
76
+ - spec/gitscrub/profile_spec.rb
77
+ - spec/gitscrub/ui_spec.rb
78
+ - spec/spec_helper.rb
79
+ has_rdoc: true
80
+ homepage: ""
81
+ licenses: []
82
+
83
+ post_install_message:
84
+ rdoc_options: []
85
+
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: "0"
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ none: false
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: "0"
100
+ requirements: []
101
+
102
+ rubyforge_project: gitscrub
103
+ rubygems_version: 1.5.2
104
+ signing_key:
105
+ specification_version: 3
106
+ summary: An interactive way to learn git.
107
+ test_files: []
108
+