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/- FILE_KINDS =
{ "test" => {config: "test file", fallback: "test file (*_test.rb)"}, "solution" => {config: "solution file", fallback: "solution file (.rb)"} }.freeze
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
- #exercism_config? ⇒ Boolean
- #exists? ⇒ Boolean
-
#initialize(slug:, track: Config.track, root: Config.root(track), path: nil) ⇒ Exercise
constructor
A new instance of Exercise.
- #solution_file(ambiguity_hint: nil) ⇒ Object
- #solution_files(ambiguity_hint: nil) ⇒ Object
- #test_file(ambiguity_hint: nil) ⇒ Object
- #test_files(ambiguity_hint: nil) ⇒ Object
Constructor Details
#initialize(slug:, track: Config.track, root: Config.root(track), path: nil) ⇒ Exercise
Returns a new instance of Exercise.
16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/exercism/rb/exercise.rb', line 16 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)) @exercism_config = nil end |
Instance Attribute Details
#path ⇒ Object (readonly)
Returns the value of attribute path.
14 15 16 |
# File 'lib/exercism/rb/exercise.rb', line 14 def path @path end |
#root ⇒ Object (readonly)
Returns the value of attribute root.
14 15 16 |
# File 'lib/exercism/rb/exercise.rb', line 14 def root @root end |
#slug ⇒ Object (readonly)
Returns the value of attribute slug.
14 15 16 |
# File 'lib/exercism/rb/exercise.rb', line 14 def slug @slug end |
#track ⇒ Object (readonly)
Returns the value of attribute track.
14 15 16 |
# File 'lib/exercism/rb/exercise.rb', line 14 def track @track end |
Instance Method Details
#ensure_exists! ⇒ Object
32 33 34 35 36 |
# File 'lib/exercism/rb/exercise.rb', line 32 def ensure_exists! return self if exists? raise Error, "Exercise not found: #{@path}" end |
#exercism_config? ⇒ Boolean
38 39 40 |
# File 'lib/exercism/rb/exercise.rb', line 38 def exercism_config? File.file?(config_path) end |
#exists? ⇒ Boolean
28 29 30 |
# File 'lib/exercism/rb/exercise.rb', line 28 def exists? Dir.exist?(@path) end |
#solution_file(ambiguity_hint: nil) ⇒ Object
58 59 60 |
# File 'lib/exercism/rb/exercise.rb', line 58 def solution_file(ambiguity_hint: nil) pick_one(solution_files(ambiguity_hint: ambiguity_hint), kind: "solution file (.rb)", ambiguity_hint: ambiguity_hint) end |
#solution_files(ambiguity_hint: nil) ⇒ Object
48 49 50 51 52 |
# File 'lib/exercism/rb/exercise.rb', line 48 def solution_files(ambiguity_hint: nil) exercise_files("solution", ambiguity_hint: ambiguity_hint) do fallback_solution_files end end |
#test_file(ambiguity_hint: nil) ⇒ Object
54 55 56 |
# File 'lib/exercism/rb/exercise.rb', line 54 def test_file(ambiguity_hint: nil) pick_one(test_files(ambiguity_hint: ambiguity_hint), kind: "test file (*_test.rb)", ambiguity_hint: ambiguity_hint) end |
#test_files(ambiguity_hint: nil) ⇒ Object
42 43 44 45 46 |
# File 'lib/exercism/rb/exercise.rb', line 42 def test_files(ambiguity_hint: nil) exercise_files("test", ambiguity_hint: ambiguity_hint) do fallback_test_files end end |