marmalade 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,43 @@
1
+ 15
2
+ 2
3
+ 1 1
4
+ 2 2
5
+ 1
6
+ 1 9
7
+ 2
8
+ 5 3
9
+ 6 4
10
+ 2
11
+ 4 10
12
+ 6 2
13
+ 2
14
+ 5 8
15
+ 8 4
16
+ 1
17
+ 263 96
18
+ 2
19
+ 482 213
20
+ 481 212
21
+ 2
22
+ 242 321
23
+ 376 144
24
+ 2
25
+ 389 448
26
+ 25 153
27
+ 2
28
+ 261 4
29
+ 383 193
30
+ 1
31
+ 224 9746
32
+ 2
33
+ 6150 4479
34
+ 6151 4480
35
+ 2
36
+ 1603 8126
37
+ 2388 5863
38
+ 2
39
+ 5654 961
40
+ 7939 2636
41
+ 2
42
+ 5731 1199
43
+ 2376 4556
@@ -0,0 +1,3 @@
1
+ # Rope Intranet Example
2
+
3
+ A working example of the 'Rope Intranet' puzzle at http://code.google.com/codejam/contest/619102/dashboard#s=p0
@@ -0,0 +1,34 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems'
4
+ require 'marmalade'
5
+
6
+ def intersect?(segment_a, segment_b)
7
+ # They intersect if only one side is higher than the other
8
+ (segment_a[0] - segment_b[0]) * (segment_a[1] - segment_b[1]) < 0
9
+ end
10
+
11
+ # Brute force check all wires
12
+ def count_intersections(wires)
13
+ intersections = 0
14
+ wires.length.times do
15
+ wire = wires.pop
16
+ puts_dbg "Wire: #{wire.inspect}"
17
+ wires.each do |other_wire|
18
+ puts_dbg "Other wire: #{other_wire.inspect}"
19
+ intersections += intersect?(wire, other_wire) ? 1 : 0
20
+ end
21
+ end
22
+ intersections
23
+ end
24
+
25
+ Marmalade.jam do
26
+ read_num_cases
27
+ test_cases do
28
+ read :wire_count, :type => :int
29
+ read :wires, :count => @wire_count, :type => :int, :split => true
30
+ run_case do
31
+ puts count_intersections(@wires)
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,28 @@
1
+ require 'fileutils'
2
+
3
+ # Builds a project scaffolding
4
+ # TODO: Currently Ruby 1.9-only -- make this work with 1.8
5
+ class ProjectBuilder
6
+
7
+ def create(project_name)
8
+ template_path = File.join(File.expand_path('../../../', __FILE__), 'templates')
9
+
10
+ # Create project directory
11
+ puts "Creating directory #{project_name}"
12
+ Dir.mkdir(project_name)
13
+
14
+ # Copy all template files into it
15
+ main_file = File.join(project_name, "#{project_name}.rb")
16
+ copy_file(File.join(template_path, 'main.rb'), main_file)
17
+ copy_file(File.join(template_path, 'main_spec.rb'), File.join(project_name, "#{project_name}_spec.rb"))
18
+ copy_file(File.join(template_path, 'Rakefile'), File.join(project_name, 'Rakefile'))
19
+ end
20
+
21
+ private
22
+
23
+ def copy_file(from, to)
24
+ puts "Creating #{to}"
25
+ FileUtils.copy(from, to)
26
+ end
27
+
28
+ end
@@ -1,3 +1,8 @@
1
+ # TODO: RDoc this
2
+ # TODO: read_array
3
+ # TODO: read_integer_array
4
+ # TODO: read_values
5
+
1
6
  module Marmalade
2
7
  class Puzzle
3
8
  attr_accessor :reader
@@ -9,7 +14,9 @@ module Marmalade
9
14
  end
10
15
 
11
16
  def read(assignments, options = {})
12
- # TODO: Check if we're in a 'run_case' block, if so throw an error
17
+ if @running_case
18
+ raise MarmaladeError.new("Cannot call read while in a run_case block")
19
+ end
13
20
  options = @options.merge(options)
14
21
  assigns = reader.read(assignments, options)
15
22
  assigns.each do |k, v|
@@ -17,8 +24,14 @@ module Marmalade
17
24
  end
18
25
  end
19
26
 
27
+ def read_num_cases
28
+ read :num_cases, :type => :int
29
+ end
30
+
20
31
  def test_cases(options = {}, &block)
21
- # TODO: Make sure @num_cases is set
32
+ unless @num_cases.is_a?(Fixnum)
33
+ raise MarmaladeError.new("@num_cases has not been set or is not an integer")
34
+ end
22
35
  options = options.merge(@options)
23
36
  1.upto(@num_cases) do |case_num|
24
37
  @case_num = case_num
@@ -29,9 +42,11 @@ module Marmalade
29
42
  end
30
43
 
31
44
  def run_case(&block)
45
+ @running_case = true
32
46
  if @options[:case].nil? || @options[:case] == @case_num
33
47
  instance_eval(&block)
34
48
  end
49
+ @running_case = false
35
50
  end
36
51
 
37
52
  def puts(*args)
@@ -45,7 +60,10 @@ module Marmalade
45
60
  private
46
61
 
47
62
  def puts_with_case(*args)
48
- print "Case ##{@case_num}: ", *args
63
+ unless @case_num.nil?
64
+ print "Case ##{@case_num}: "
65
+ end
66
+ print *args
49
67
  print "\n"
50
68
  end
51
69
  end
@@ -1,3 +1,3 @@
1
1
  module Marmalade
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/lib/marmalade.rb CHANGED
@@ -1,18 +1,30 @@
1
1
  require 'rubygems'
2
2
  require 'trollop'
3
3
 
4
+ require 'marmalade/project_builder'
4
5
  require 'marmalade/file_reader'
5
6
  require 'marmalade/puzzle'
6
7
  require 'marmalade/version'
7
8
 
9
+ # A class to use for throwing error internally.
10
+ class MarmaladeError < Exception; end
11
+
8
12
  module Marmalade
9
13
 
10
14
  def self.jam(options = {}, &block)
11
15
  options = parse_options.merge(options)
12
- File.open(options[:file], 'r') do |file|
13
- reader = FileReader.new(file)
14
- puzzle = Puzzle.new(reader, options)
15
- puzzle.instance_eval(&block)
16
+ begin
17
+ file_name = options[:file]
18
+ unless File.exist?(file_name)
19
+ raise MarmaladeError.new("Cannot find input file #{file_name}")
20
+ end
21
+ File.open(options[:file], 'r') do |file|
22
+ reader = FileReader.new(file)
23
+ puzzle = Puzzle.new(reader, options)
24
+ puzzle.instance_eval(&block)
25
+ end
26
+ rescue MarmaladeError => e
27
+ puts "** Error: #{e.message}"
16
28
  end
17
29
  end
18
30
 
@@ -18,4 +18,11 @@ describe Marmalade do
18
18
  2 => { :dummy => 9, :lines => ['a', 'b', 'c', 'd'] },
19
19
  3 => { :dummy => 3, :lines => ['hello world'] } }
20
20
  end
21
+
22
+ it "will throw an error if the input file does not exist" do
23
+ Marmalade.expects(:puts).with("** Error: Cannot find input file nothing.txt")
24
+ ran = false
25
+ Marmalade.jam(:file => 'nothing.txt') { ran = true }
26
+ ran.should be_false
27
+ end
21
28
  end
@@ -0,0 +1,47 @@
1
+ require 'spec_helper'
2
+
3
+ describe ProjectBuilder do
4
+
5
+ TEMP_DIR = '/tmp'
6
+ PROJ_NAME = 'foo'
7
+ PROJ_PATH = File.join(TEMP_DIR, PROJ_NAME)
8
+
9
+ before do
10
+ clean_project_dir
11
+ @builder = ProjectBuilder.new
12
+ # Stub out puts so we don't get any output when we run tests
13
+ @builder.stubs(:puts)
14
+ Dir.chdir(TEMP_DIR) do
15
+ @builder.create(PROJ_NAME)
16
+ end
17
+ end
18
+
19
+ after do
20
+ clean_project_dir
21
+ end
22
+
23
+ it "should create the project path" do
24
+ File.exist?(PROJ_PATH).should be_true
25
+ end
26
+
27
+ it "should create a main Ruby file with the name of the project" do
28
+ project_file_exists?("#{PROJ_NAME}.rb").should be_true
29
+ end
30
+
31
+ it "should create a spec file with the name of the project" do
32
+ project_file_exists?("#{PROJ_NAME}_spec.rb").should be_true
33
+ end
34
+
35
+ it "should create a Rakefile" do
36
+ project_file_exists?("Rakefile").should be_true
37
+ end
38
+
39
+ def project_file_exists?(file_name)
40
+ File.exist?(File.join(PROJ_PATH, file_name))
41
+ end
42
+
43
+ def clean_project_dir
44
+ FileUtils.rm_rf(PROJ_PATH)
45
+ end
46
+
47
+ end
@@ -3,19 +3,43 @@ require 'spec_helper'
3
3
  describe Marmalade::Puzzle do
4
4
 
5
5
  describe "#read" do
6
+ before do
7
+ @reader = mock()
8
+ @puzzle = Marmalade::Puzzle.new(@reader)
9
+ end
10
+
6
11
  it "will pass the options to the reader and assign instance varialbes based on the results" do
7
- reader = mock()
8
- reader.expects(:read).with([:foo, :bar], { :a => 1, :b => 2}).returns({:foo => "a", :bar => 2})
9
- puzzle = Marmalade::Puzzle.new(reader, :a => 1)
10
- puzzle.read([:foo, :bar], :b => 2)
11
- puzzle.instance_eval do
12
+ @reader.expects(:read).with([:foo, :bar], { :a => 1 }).returns({:foo => "a", :bar => 2})
13
+ @puzzle.read([:foo, :bar], :a => 1)
14
+ @puzzle.instance_eval do
12
15
  @foo.should == 'a'
13
16
  @bar.should == 2
14
17
  end
15
18
  end
19
+
20
+ it "will read in the number of cases with read_num_cases" do
21
+ @reader.expects(:read).with(:num_cases, :type => :int).returns({:num_cases => 1234})
22
+ @puzzle.read_num_cases
23
+ @puzzle.instance_eval do
24
+ @num_cases.should == 1234
25
+ end
26
+ end
27
+
28
+ it "will raise an error if it is called in a run_case block" do
29
+ expect do
30
+ @puzzle.run_case do
31
+ read :foo
32
+ end
33
+ end.to raise_error(MarmaladeError, /Cannot call read while in a run_case block/)
34
+ end
35
+
16
36
  end
17
37
 
18
38
  describe "#test_cases" do
39
+ before do
40
+ @puzzle = Marmalade::Puzzle.new(mock('reader'))
41
+ end
42
+
19
43
  it "will run through all the test cases" do
20
44
  run_test_cases.should == [1, 2, 3]
21
45
  end
@@ -29,13 +53,16 @@ describe Marmalade::Puzzle do
29
53
  run_test_cases(:step => true)
30
54
  end
31
55
 
56
+ it "will raise an error if num_cases hasn't been set" do
57
+ expect { @puzzle.test_cases }.to raise_error(MarmaladeError, /has not been set/)
58
+ end
59
+
32
60
  def run_test_cases(options = {})
33
- puzzle = Marmalade::Puzzle.new(mock('reader'))
34
- puzzle.instance_eval do
61
+ @puzzle.instance_eval do
35
62
  @num_cases = 3
36
63
  end
37
64
  case_numbers = []
38
- puzzle.test_cases(options) do
65
+ @puzzle.test_cases(options) do
39
66
  case_numbers << @case_num
40
67
  end
41
68
  case_numbers
@@ -48,28 +75,43 @@ describe Marmalade::Puzzle do
48
75
  end
49
76
 
50
77
  it "will evaluate the given block if no case options are specified" do
78
+ value = nil
79
+ build_puzzle(3).run_case do
80
+ value = 'foo'
81
+ end
82
+ value.should == 'foo'
83
+ end
84
+
85
+ it "will set @running_case to true while in the block" do
86
+ running = false
51
87
  build_puzzle(3).run_case do
52
- 'foo'
53
- end.should == 'foo'
88
+ running = @running_case
89
+ end
90
+ running.should be_true
54
91
  end
55
92
 
56
93
  it "will evaluate the block if the given case and the current case match" do
94
+ value = nil
57
95
  build_puzzle(3, :case => 3).run_case do
58
- 'foo'
59
- end.should == 'foo'
96
+ value = 'foo'
97
+ end
98
+ value.should == 'foo'
60
99
  end
61
100
 
62
101
  it "will not evalulate the block if the case numbers don't match" do
102
+ value = nil
63
103
  build_puzzle(3, :case => 4).run_case do
64
- 'foo'
65
- end.should be_nil
104
+ value = 'foo'
105
+ end
106
+ value.should be_nil
66
107
  end
67
108
  end
68
109
 
69
110
  describe "#puts" do
70
111
  it "will print the message along with the current case number" do
71
112
  puzzle = build_puzzle(4)
72
- puzzle.expects(:print).with("Case #4: ", "hello")
113
+ puzzle.expects(:print).with("Case #4: ")
114
+ puzzle.expects(:print).with("hello")
73
115
  puzzle.expects(:print).with("\n")
74
116
  puzzle.puts("hello")
75
117
  end
@@ -78,7 +120,8 @@ describe Marmalade::Puzzle do
78
120
  describe "#puts_dbg" do
79
121
  it "will print the message with the case number if in debug mode" do
80
122
  puzzle = build_puzzle(4, :debug => true)
81
- puzzle.expects(:print).with("Case #4: ", "hello")
123
+ puzzle.expects(:print).with("Case #4: ")
124
+ puzzle.expects(:print).with("hello")
82
125
  puzzle.expects(:print).with("\n")
83
126
  puzzle.puts_dbg('hello')
84
127
  end
@@ -0,0 +1,27 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'rspec'
6
+ require 'rspec/core/rake_task'
7
+
8
+ desc 'Default: run the specs'
9
+ task :default => 'spec'
10
+
11
+ desc "Run specs"
12
+ RSpec::Core::RakeTask.new('spec') do |t|
13
+ t.pattern = '*_spec.rb'
14
+ end
15
+ rescue LoadError
16
+ # Nothing to see here...
17
+ end
18
+
19
+ desc "Package up source code"
20
+ task :package do |t|
21
+ # TODO: Make this Windows-friendly
22
+ project_name = `basename $(pwd)`.chomp
23
+ system "mkdir #{project_name}"
24
+ system "cp #{project_name}.rb #{project_name}"
25
+ system "tar czvf #{project_name}.tgz #{project_name}"
26
+ system "rm -rf #{project_name}"
27
+ end
data/templates/main.rb ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems'
4
+ require 'marmalade'
5
+
6
+ Marmalade.jam do
7
+ # This assumes your input file has the number of test cases as the first line.
8
+ # Remove the call to read_num_cases this if that is not so.
9
+ read_num_cases
10
+
11
+ test_cases do
12
+ # Test case-specific code goes here
13
+ end
14
+ end
@@ -0,0 +1,5 @@
1
+ require 'rubygems'
2
+ require 'rspec'
3
+
4
+ describe 'Project' do
5
+ end
metadata CHANGED
@@ -1,132 +1,129 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: marmalade
3
- version: !ruby/object:Gem::Version
4
- hash: 29
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
5
  prerelease:
6
- segments:
7
- - 0
8
- - 0
9
- - 1
10
- version: 0.0.1
11
6
  platform: ruby
12
- authors:
7
+ authors:
13
8
  - Rusty Geldmacher
14
9
  autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
-
18
- date: 2012-03-28 00:00:00 Z
19
- dependencies:
20
- - !ruby/object:Gem::Dependency
12
+ date: 2012-03-31 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
21
15
  name: trollop
22
- prerelease: false
23
- requirement: &id001 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
24
17
  none: false
25
- requirements:
18
+ requirements:
26
19
  - - ~>
27
- - !ruby/object:Gem::Version
28
- hash: 83
29
- segments:
30
- - 1
31
- - 16
32
- - 2
20
+ - !ruby/object:Gem::Version
33
21
  version: 1.16.2
34
22
  type: :runtime
35
- version_requirements: *id001
36
- - !ruby/object:Gem::Dependency
37
- name: rspec
38
23
  prerelease: false
39
- requirement: &id002 !ruby/object:Gem::Requirement
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 1.16.2
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec
32
+ requirement: !ruby/object:Gem::Requirement
40
33
  none: false
41
- requirements:
34
+ requirements:
42
35
  - - ~>
43
- - !ruby/object:Gem::Version
44
- hash: 43
45
- segments:
46
- - 2
47
- - 9
48
- - 0
36
+ - !ruby/object:Gem::Version
49
37
  version: 2.9.0
50
38
  type: :development
51
- version_requirements: *id002
52
- - !ruby/object:Gem::Dependency
53
- name: mocha
54
39
  prerelease: false
55
- requirement: &id003 !ruby/object:Gem::Requirement
40
+ version_requirements: !ruby/object:Gem::Requirement
56
41
  none: false
57
- requirements:
42
+ requirements:
58
43
  - - ~>
59
- - !ruby/object:Gem::Version
60
- hash: 61
61
- segments:
62
- - 0
63
- - 10
64
- - 5
44
+ - !ruby/object:Gem::Version
45
+ version: 2.9.0
46
+ - !ruby/object:Gem::Dependency
47
+ name: mocha
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
65
53
  version: 0.10.5
66
54
  type: :development
67
- version_requirements: *id003
68
- description: Marmalade gives you ready-to-go parsing and other helpers for Google Code Jam puzzles
69
- email:
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 0.10.5
62
+ description: Marmalade gives you ready-to-go parsing and other helpers for Google
63
+ Code Jam puzzles
64
+ email:
70
65
  - russell.geldmacher@gmail.com
71
- executables: []
72
-
66
+ executables:
67
+ - jam
73
68
  extensions: []
74
-
75
69
  extra_rdoc_files: []
76
-
77
- files:
70
+ files:
78
71
  - .gitignore
79
72
  - Gemfile
80
73
  - LICENSE
81
74
  - README.md
82
75
  - Rakefile
76
+ - bin/jam
77
+ - examples/reverse-words/B-small-practice.in
78
+ - examples/reverse-words/README.md
79
+ - examples/reverse-words/reverse-words.rb
80
+ - examples/rope-intranet/A-large-practice.in
81
+ - examples/rope-intranet/A-small-practice.in
82
+ - examples/rope-intranet/README.md
83
+ - examples/rope-intranet/rope-intranet.rb
83
84
  - lib/marmalade.rb
84
85
  - lib/marmalade/file_reader.rb
86
+ - lib/marmalade/project_builder.rb
85
87
  - lib/marmalade/puzzle.rb
86
88
  - lib/marmalade/version.rb
87
89
  - marmalade.gemspec
88
90
  - spec/fixtures/input_0.txt
89
91
  - spec/marmalade/file_reader_spec.rb
90
92
  - spec/marmalade/marmalade_spec.rb
93
+ - spec/marmalade/project_builder_spec.rb
91
94
  - spec/marmalade/puzzle_spec.rb
92
95
  - spec/spec_helper.rb
96
+ - templates/Rakefile
97
+ - templates/main.rb
98
+ - templates/main_spec.rb
93
99
  homepage: http://www.github.com/rustygeldmacher/marmalade
94
100
  licenses: []
95
-
96
101
  post_install_message:
97
102
  rdoc_options: []
98
-
99
- require_paths:
103
+ require_paths:
100
104
  - lib
101
- required_ruby_version: !ruby/object:Gem::Requirement
105
+ required_ruby_version: !ruby/object:Gem::Requirement
102
106
  none: false
103
- requirements:
104
- - - ">="
105
- - !ruby/object:Gem::Version
106
- hash: 3
107
- segments:
108
- - 0
109
- version: "0"
110
- required_rubygems_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ! '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ required_rubygems_version: !ruby/object:Gem::Requirement
111
112
  none: false
112
- requirements:
113
- - - ">="
114
- - !ruby/object:Gem::Version
115
- hash: 3
116
- segments:
117
- - 0
118
- version: "0"
113
+ requirements:
114
+ - - ! '>='
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
119
117
  requirements: []
120
-
121
118
  rubyforge_project:
122
- rubygems_version: 1.8.10
119
+ rubygems_version: 1.8.21
123
120
  signing_key:
124
121
  specification_version: 3
125
122
  summary: Helper for Google Code Jam puzzles
126
- test_files:
123
+ test_files:
127
124
  - spec/fixtures/input_0.txt
128
125
  - spec/marmalade/file_reader_spec.rb
129
126
  - spec/marmalade/marmalade_spec.rb
127
+ - spec/marmalade/project_builder_spec.rb
130
128
  - spec/marmalade/puzzle_spec.rb
131
129
  - spec/spec_helper.rb
132
- has_rdoc: