Class: SourceCode

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
RubyToBlock
Defined in:
app/models/source_code.rb

Overview

ソースコードを表現するモデル

Constant Summary collapse

MAX_REMIX_COUNT =
1000

Class Method Summary collapse

Instance Method Summary collapse

Methods included from RubyToBlock

#to_blocks

Class Method Details

.make_remix_filename(home_dir, filename) ⇒ Object

リミックス用のファイル名を生成する



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'app/models/source_code.rb', line 23

def self.make_remix_filename(home_dir, filename)
  home_dir = Pathname(home_dir).expand_path
  filename = filename.dup
  ext = filename.slice!(/\.rb(\.xml)?$/)
  filename.slice!(/(_remix(\d+)?)+$/)
  basename = "#{filename}_remix"
  MAX_REMIX_COUNT.times do |i|
    suffix = (i == 0 ? '' : sprintf('%02d', i + 1))
    remix_name = "#{basename}#{suffix}"
    if !home_dir.join("#{remix_name}.rb").exist? &&
        !home_dir.join("#{remix_name}.rb.xml").exist?
      return "#{remix_name}#{ext}"
    end
  end
  fail "reach max remix count...: #{filename}"
end

Instance Method Details

#check_syntaxObject

シンタックスをチェックする



41
42
43
44
45
46
47
48
49
50
51
52
# File 'app/models/source_code.rb', line 41

def check_syntax
  _, stderr_str, status = *open3_capture3_ruby_c
  return [] if status.success?

  stderr_str.lines.each.with_object([]) { |line, res|
    if (md = /^.*:(\d+): (.*)$/.match(line))
      res << { row: md[1].to_i, column: 0, message: md[2] }
    elsif (md = /( +)\^$/.match(line))
      res[-1][:column] = md[1].length
    end
  }
end

#digestObject

ハッシュ値を計算する



63
64
65
# File 'app/models/source_code.rb', line 63

def digest
  Digest::SHA256.hexdigest(data)
end

#run(path) ⇒ Object

プログラムを実行する



55
56
57
58
59
60
# File 'app/models/source_code.rb', line 55

def run(path)
  _, stderr_str, status = *open3_capture3_run_program(path)
  return [] if status.success?

  parse_ruby_error_messages(stderr_str)
end