Load list of rules dynamically

This commit is contained in:
Vitalii Elenhaupt 2017-11-01 12:49:03 +02:00
parent 1822b2b0a2
commit dcc0be892a
No known key found for this signature in database
GPG key ID: 7558EF3A4056C706
4 changed files with 12 additions and 16 deletions

View file

@ -1,12 +1,6 @@
require "../../spec/spec_helper" require "../../spec/spec_helper"
module Ameba module Ameba
describe RULES do
it "contains available rules" do
Ameba::RULES.empty?.should be_false
end
end
describe Rule do describe Rule do
describe "#catch" do describe "#catch" do
it "accepts and returns source" do it "accepts and returns source" do
@ -20,5 +14,11 @@ module Ameba
DummyRule.new.name.should eq "DummyRule" DummyRule.new.name.should eq "DummyRule"
end end
end end
describe ".rules" do
it "returns a list of all defined rules" do
Rule.rules.includes?(DummyRule).should be_true
end
end
end end
end end

View file

@ -4,4 +4,4 @@ require "../src/ameba"
struct DummyRule < Ameba::Rule struct DummyRule < Ameba::Rule
def test(source) def test(source)
end end
end end

View file

@ -25,7 +25,7 @@ module Ameba
end end
def catch(source : Source) def catch(source : Source)
RULES.each do |rule| Rule.rules.each do |rule|
rule.new.test(source) rule.new.test(source)
end end
end end

View file

@ -1,12 +1,4 @@
module Ameba module Ameba
RULES = [
Rules::ComparisonToBoolean,
Rules::LineLength,
Rules::TrailingBlankLines,
Rules::TrailingWhitespace,
Rules::UnlessElse,
]
abstract struct Rule abstract struct Rule
abstract def test(source : Source) abstract def test(source : Source)
@ -21,5 +13,9 @@ module Ameba
def name def name
self.class.name.gsub("Ameba::Rules::", "") self.class.name.gsub("Ameba::Rules::", "")
end end
def self.rules
{{ @type.subclasses }}
end
end end
end end