This repository has been archived by the owner on Jan 17, 2019. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathimplementation.rb
110 lines (90 loc) · 2.14 KB
/
implementation.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
require 'pathname'
require_relative 'file_bundle'
module Xapi
# Implementation is a language-specific implementation of an exercise.
class Implementation
IGNORE = [
Regexp.new("HINTS\.md$"),
Regexp.new("example", Regexp::IGNORECASE),
Regexp.new("\/\.$"),
]
attr_reader :track_id, :repo, :problem, :root, :file_bundle
attr_writer :files
def initialize(track_id, repo, problem, root)
@track_id = track_id
@repo = repo
@problem = problem
@root = Pathname.new(root)
@file_bundle = FileBundle.new(dir, IGNORE)
end
def exists?
File.exist?(dir)
end
def files
@files ||= Hash[file_bundle.paths.map {|path|
[path.relative_path_from(dir).to_s, File.read(path)]
}].merge("README.md" => readme)
end
def zip
@zip ||= file_bundle.zip do |io|
io.put_next_entry('README.md')
io.print readme
end
end
def readme
@readme ||= assemble_readme
end
def exercise_dir
if File.exist?(track_dir.join('exercises'))
File.join('exercises', problem.slug)
else
problem.slug
end
end
def git_url
[repo, "tree/master", exercise_dir].join("/")
end
private
def dir
@dir ||= track_dir.join(exercise_dir)
end
def track_dir
@track_dir ||= root.join('tracks', track_id)
end
def assemble_readme
<<-README
# #{problem.name}
#{problem.blurb}
#{readme_body}
#{problem.source_markdown}
#{incomplete_solutions_body}
README
end
def readme_body
[
problem.description,
track_hint,
implementation_hint,
].reject(&:empty?).join("\n").strip
end
def incomplete_solutions_body
<<-README
## Submitting Incomplete Problems
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
README
end
def track_hint
read track_dir.join('SETUP.md')
end
def implementation_hint
read File.join(dir, 'HINTS.md')
end
def read(f)
if File.exist?(f)
File.read(f)
else
""
end
end
end
end