Class: Exercism::Rb::Exercise

Inherits:
Object
  • Object
show all
Defined in:
lib/exercism/rb/exercise.rb

Constant Summary collapse

SLUG_PATTERN =
/\A[a-z0-9][a-z0-9-]*\z/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(slug:, track: Config.track, root: Config.root(track), path: nil) ⇒ Exercise

Returns a new instance of Exercise.

Raises:



10
11
12
13
14
15
16
17
18
19
# File 'lib/exercism/rb/exercise.rb', line 10

def initialize(slug:, track: Config.track, root: Config.root(track), path: nil)
  slug = slug.to_s.strip
  raise Error, "Exercise is required." if slug.empty?
  raise Error, "Invalid slug: #{slug.inspect}. Use something like assembly-line." unless slug.match?(SLUG_PATTERN)

  @slug = slug
  @track = track
  @root = File.expand_path(root)
  @path = File.expand_path(path || File.join(@root, @slug))
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



8
9
10
# File 'lib/exercism/rb/exercise.rb', line 8

def path
  @path
end

#rootObject (readonly)

Returns the value of attribute root.



8
9
10
# File 'lib/exercism/rb/exercise.rb', line 8

def root
  @root
end

#slugObject (readonly)

Returns the value of attribute slug.



8
9
10
# File 'lib/exercism/rb/exercise.rb', line 8

def slug
  @slug
end

#trackObject (readonly)

Returns the value of attribute track.



8
9
10
# File 'lib/exercism/rb/exercise.rb', line 8

def track
  @track
end

Instance Method Details

#ensure_exists!Object

Raises:



25
26
27
28
29
# File 'lib/exercism/rb/exercise.rb', line 25

def ensure_exists!
  return self if exists?

  raise Error, "Exercise not found: #{@path}"
end

#exists?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/exercism/rb/exercise.rb', line 21

def exists?
  Dir.exist?(@path)
end

#solution_fileObject



36
37
38
39
# File 'lib/exercism/rb/exercise.rb', line 36

def solution_file
  ensure_exists!
  pick_one(files_matching { |name| name.end_with?(".rb") && !name.end_with?("_test.rb") }, kind: "solution file (.rb)")
end

#test_fileObject



31
32
33
34
# File 'lib/exercism/rb/exercise.rb', line 31

def test_file
  ensure_exists!
  pick_one(files_matching { |name| name.end_with?("_test.rb") }, kind: "test file (*_test.rb)")
end