Class: Task

Inherits:
Object
  • Object
show all
Defined in:
lib/atcoder_tools/task.rb

Overview

module Atcoder

Instance Method Summary collapse

Constructor Details

#initialize(contest, name) ⇒ Task

Returns a new instance of Task.



8
9
10
11
12
13
# File 'lib/atcoder_tools/task.rb', line 8

def initialize(contest, name)
  @contest = contest
  @name = name
  @tests = []
  @test_outputs = []
end

Instance Method Details

#create!Object



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/atcoder_tools/task.rb', line 27

def create!
  html = URI.open(url) do |f|
    charset = f.charset
    f.read
  end

  doc = Nokogiri::HTML.parse(html, nil, 'utf8')
  samples = doc.css('.lang-ja > .part > section > pre').map { |e| e.children.text }
  inputs, outputs = samples.partition.with_index { |_sample, i| i.even? }

  erb = ERB.new(File.read(File.dirname(__dir__)+"/atcoder_tools/sources/task.rb.erb"))
  File.write(task_file_path, erb.result(binding))
end

#read_meta_dataObject



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
# File 'lib/atcoder_tools/task.rb', line 59

def 
  meta_flag = false
  test_no = nil
  tests = []
  mode = nil
  File.foreach(task_file_path) do |line|
    line.chomp!
    if line == "# METADATA::START"
      meta_flag = true
      next
    end
    if line == "# METADATA::FINISH"
      meta_flag = false
      next
    end
    unless meta_flag
      next
    end
    if line =~ /\#\stest-/
      test_no = line.scan(/\#\stest-(\d+)/)[0][0].to_i
      tests.push({ no: test_no })
    end
    if line =~ /\#\sinput:/
      test = tests.find{|test| test[:no] == test_no}
      test.merge!({ input: line.scan(/\#\sinput\:\s\"(.*)\"/)[0][0] })
    end
    if line =~ /\#\soutput:/
      test = tests.find{|test| test[:no] == test_no}
      test.merge!({ output: line.scan(/\#\soutput\:\s\"(.*)\"/)[0][0] })
    end
    if line =~ /\#\sMODE:/
      mode = line.scan(/\#\sMODE:\s(.+)/)[0][0]
    end
  end
  @tests = tests
  @mode = mode
end

#runObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/atcoder_tools/task.rb', line 41

def run
  
  update_spec
  if @mode == "DEBUG"
    io = IO.popen("ruby #{task_file_path}", "w+")
    io.puts(@tests[0][:input])
    io.close_write
    puts io.readlines
  end
  if @mode == "RUN"
    system("echo put your inputs")
    system("ruby #{task_file_path}")
  end
  if @mode == "TEST"
    system("bundle exec rspec #{test_file_path}")
  end
end

#task_file_pathObject



19
20
21
# File 'lib/atcoder_tools/task.rb', line 19

def task_file_path
  "#{@contest.name}/#{@name}.rb"
end

#test_file_pathObject



23
24
25
# File 'lib/atcoder_tools/task.rb', line 23

def test_file_path
  ".atcoder/#{@contest.name}/#{@name}/spec.rb"
end

#update_specObject



97
98
99
100
101
# File 'lib/atcoder_tools/task.rb', line 97

def update_spec
  FileUtils.mkdir_p ".atcoder/#{@contest.name}/#{@name}"
  erb = ERB.new(File.read(File.dirname(__dir__)+"/atcoder_tools/sources/spec.rb.erb"))
  File.write(test_file_path, erb.result(binding))
end

#urlObject



15
16
17
# File 'lib/atcoder_tools/task.rb', line 15

def url
  "https://atcoder.jp/contests/#{@contest.name}/tasks/#{@contest.name}_#{@name}"
end