Class: Exercism::Rb::Exercise
- Inherits:
-
Object
- Object
- Exercism::Rb::Exercise
- Defined in:
- lib/exercism/rb/exercise.rb
Constant Summary collapse
- SLUG_PATTERN =
/\A[a-z0-9][a-z0-9-]*\z/
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#root ⇒ Object
readonly
Returns the value of attribute root.
-
#slug ⇒ Object
readonly
Returns the value of attribute slug.
-
#track ⇒ Object
readonly
Returns the value of attribute track.
Instance Method Summary collapse
- #ensure_exists! ⇒ Object
- #exists? ⇒ Boolean
-
#initialize(slug:, track: Config.track, root: Config.root(track), path: nil) ⇒ Exercise
constructor
A new instance of Exercise.
- #solution_file ⇒ Object
- #test_file ⇒ Object
Constructor Details
#initialize(slug:, track: Config.track, root: Config.root(track), path: nil) ⇒ Exercise
Returns a new instance of Exercise.
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.(root) @path = File.(path || File.join(@root, @slug)) end |
Instance Attribute Details
#path ⇒ Object (readonly)
Returns the value of attribute path.
8 9 10 |
# File 'lib/exercism/rb/exercise.rb', line 8 def path @path end |
#root ⇒ Object (readonly)
Returns the value of attribute root.
8 9 10 |
# File 'lib/exercism/rb/exercise.rb', line 8 def root @root end |
#slug ⇒ Object (readonly)
Returns the value of attribute slug.
8 9 10 |
# File 'lib/exercism/rb/exercise.rb', line 8 def slug @slug end |
#track ⇒ Object (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
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
21 22 23 |
# File 'lib/exercism/rb/exercise.rb', line 21 def exists? Dir.exist?(@path) end |
#solution_file ⇒ Object
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_file ⇒ Object
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 |