Skip to content

Commit eb11ee1

Browse files
authored
Merge pull request #2 from exercism/exploring-framework
Build out structure for exploring analyzers
2 parents 5496f0d + c6dfbce commit eb11ee1

14 files changed

+457
-57
lines changed

Gemfile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,18 @@ git_source(:github) do |repo_name|
55
"https://github.com/#{repo_name}.git"
66
end
77

8+
gem 'mandate'
89
gem 'rake'
910
gem 'json'
1011
gem 'activesupport'
1112

13+
gem 'parser'
14+
gem 'rubocop'
15+
16+
1217
group :test do
1318
gem 'minitest', '~> 5.10', '!= 5.10.2'
1419
gem 'minitest-stub-const'
1520
gem 'mocha'
21+
gem 'pry'
1622
end

Gemfile.lock

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,57 @@ GEM
66
i18n (>= 0.7, < 2)
77
minitest (~> 5.1)
88
tzinfo (~> 1.1)
9+
ast (2.4.0)
10+
coderay (1.1.2)
911
concurrent-ruby (1.1.4)
1012
i18n (1.5.3)
1113
concurrent-ruby (~> 1.0)
14+
jaro_winkler (1.5.2)
1215
json (2.1.0)
16+
mandate (0.2.0)
1317
metaclass (0.0.4)
18+
method_source (0.9.2)
1419
minitest (5.11.3)
1520
minitest-stub-const (0.6)
1621
mocha (1.7.0)
1722
metaclass (~> 0.0.1)
23+
parallel (1.13.0)
24+
parser (2.6.0.0)
25+
ast (~> 2.4.0)
26+
powerpack (0.1.2)
27+
pry (0.12.2)
28+
coderay (~> 1.1.0)
29+
method_source (~> 0.9.0)
30+
rainbow (3.0.0)
1831
rake (12.3.1)
32+
rubocop (0.64.0)
33+
jaro_winkler (~> 1.5.1)
34+
parallel (~> 1.10)
35+
parser (>= 2.5, != 2.5.1.1)
36+
powerpack (~> 0.1)
37+
rainbow (>= 2.2.2, < 4.0)
38+
ruby-progressbar (~> 1.7)
39+
unicode-display_width (~> 1.4.0)
40+
ruby-progressbar (1.10.0)
1941
thread_safe (0.3.6)
2042
tzinfo (1.2.5)
2143
thread_safe (~> 0.1)
44+
unicode-display_width (1.4.1)
2245

2346
PLATFORMS
2447
ruby
2548

2649
DEPENDENCIES
2750
activesupport
2851
json
52+
mandate
2953
minitest (~> 5.10, != 5.10.2)
3054
minitest-stub-const
3155
mocha
56+
parser
57+
pry
3258
rake
59+
rubocop
3360

3461
BUNDLED WITH
3562
1.16.4

lib/analyze_solution.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class AnalyzeSolution
2+
include Mandate
3+
4+
initialize_with :exercise_slug, :path
5+
6+
def call
7+
code_to_analyze = File.readlines(path / FILENAMES[exercise_slug])
8+
9+
classified_exercise = exercise_slug.tr('-', '_').classify
10+
results = "#{classified_exercise}::Analyze".constantize.(code_to_analyze)
11+
12+
File.open(path / "analysis.json","w") do |f|
13+
f.write(results.to_json)
14+
end
15+
end
16+
end

lib/analyzer.rb

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,30 @@
22
two_fer
33
}
44

5-
require_relative 'exercise_analyzer'
5+
FILENAMES = {
6+
"two-fer" => "two_fer.rb"
7+
}
8+
9+
require 'mandate'
10+
require 'json'
11+
require 'rubocop'
12+
require 'parser/current'
13+
require 'active_support/inflector'
14+
15+
require_relative "generic/extract_class_method"
16+
require_relative "generic/extract_instance_method"
17+
require_relative "generic/extract_module_or_class"
18+
19+
require_relative 'analyze_solution'
620

21+
require_relative 'analyzers/exercise_analyzer'
722
EXERCISES.each do |exercise|
8-
require_relative "exercises/#{exercise}/analyzer"
23+
require_relative "analyzers/#{exercise}/analyze"
924
end
1025

1126
module Analyzer
12-
def self.analyze(exercise, path)
13-
ExerciseAnalyzer.analyze(exercise, path)
27+
def self.analyze(exercise_slug, path)
28+
pathname = Pathname.new(path)
29+
AnalyzeSolution.(exercise_slug, pathname)
1430
end
1531
end

lib/analyzers/exercise_analyzer.rb

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
class ExerciseAnalyzer
2+
def initialize(code_to_analyze)
3+
@code_to_analyze = code_to_analyze
4+
@approve = false
5+
@refer_to_mentor = false
6+
@messages = []
7+
end
8+
9+
def call
10+
analyze!
11+
results
12+
end
13+
14+
15+
def results
16+
{
17+
approve: approve,
18+
refer_to_mentor: refer_to_mentor,
19+
messages: messages
20+
}
21+
end
22+
23+
private
24+
attr_reader :code_to_analyze
25+
26+
protected
27+
attr_accessor :approve, :refer_to_mentor, :messages
28+
29+
def root_node
30+
@root_node ||= begin
31+
buffer = Parser::Source::Buffer.new(nil)
32+
buffer.source = code_to_analyze
33+
builder = RuboCop::AST::Builder.new
34+
parser = Parser::CurrentRuby.new(builder)
35+
36+
parser.parse(buffer)
37+
end
38+
end
39+
end

lib/analyzers/two_fer/analyze.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module TwoFer
2+
class Analyze < ExerciseAnalyzer
3+
include Mandate
4+
5+
def analyze!
6+
end
7+
end
8+
end

lib/exercise_analyzer.rb

Lines changed: 0 additions & 31 deletions
This file was deleted.

lib/exercises/two_fer/analyzer.rb

Lines changed: 0 additions & 4 deletions
This file was deleted.

manual_test.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
$LOAD_PATH.unshift File.expand_path("../lib", __FILE__)
2+
require "analyzer"
3+
require 'pp'
4+
require 'pry'
5+
6+
source = %q{
7+
class TwoFer
8+
class << self
9+
def two_fer(name="you")
10+
"One for #{name}, one for me."
11+
end
12+
def foobar
13+
end
14+
end
15+
end
16+
}
17+
18+
puts "\n\n\n\n"
19+
pp TwoFer::Analyze.(source)
20+
puts "\n\n\n\n"

test/analyze_solution_test.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
require "test_helper"
2+
3+
class AnalyzeSolutionTest < Minitest::Test
4+
5+
def test_that_it_constantizes_correctly
6+
code = mock
7+
analyzer = mock
8+
results_json = mock
9+
results = mock(to_json: results_json)
10+
11+
File.expects(:readlines).with(SAFE_WRITE_PATH / "two_fer.rb").returns(code)
12+
File.expects(:open).
13+
with(SAFE_WRITE_PATH / "analysis.json", "w").yields(
14+
mock(write: results_json)
15+
)
16+
TwoFer::Analyze.expects(:new).with(code).returns(analyzer)
17+
analyzer.expects(:call).returns(results)
18+
AnalyzeSolution.('two-fer', SAFE_WRITE_PATH)
19+
end
20+
end

0 commit comments

Comments
 (0)