Sha256: 2a9fe291c755d8e0086af2f9641679f73b8381b2b8f20655879a210cfa95d7e7

Contents?: true

Size: 1.26 KB

Versions: 3

Compression:

Stored size: 1.26 KB

Contents

module Daigaku
  class Solution
    FILE_SUFFIX = '_solution.rb'.freeze

    attr_reader :code, :path, :errors

    def initialize(unit_path)
      @unit_path = unit_path
      @path      = solution_path(unit_path)
      @code      = File.read(@path).strip if File.file?(@path)
      @verified  = store_state
    end

    def verify!
      result = Daigaku::Test.new(@unit_path).run(code)
      self.store_state = result.passed?
      result
    end

    def verified?
      !!@verified
    end

    def store_key
      unless @store_key
        part_path  = path.split('/')[-3..-1].join('/').gsub(FILE_SUFFIX, '')
        @store_key = Storeable.key(part_path, prefix: 'verified')
      end

      @store_key
    end

    private

    def solution_path(path)
      local_path    = Daigaku.config.solutions_path
      sub_directory = Storeable.key(directory_from(path))
      file          = Storeable.key(File.basename(path)) + FILE_SUFFIX

      File.join(local_path, sub_directory, file)
    end

    def directory_from(path)
      path.split('/')[-3..-2].join('/').gsub(FILE_SUFFIX, '')
    end

    def store_state=(verified)
      @verified = verified
      QuickStore.store.set(store_key, verified?)
    end

    def store_state
      QuickStore.store.get(store_key)
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
daigaku-0.6.0 lib/daigaku/solution.rb
daigaku-0.5.0 lib/daigaku/solution.rb
daigaku-0.4.0 lib/daigaku/solution.rb