Permalink
Browse files

reworked rule logic for pattern or block

  • Loading branch information...
1 parent d163f67 commit 9ff2c30b13fd57b580f2939100530b0b69664e56 bglusman committed Jan 29, 2012
Showing with 20 additions and 17 deletions.
  1. +2 −2 lib/outlaw.rb
  2. +2 −2 lib/outlaw/law_parser.rb
  3. +9 −6 lib/outlaw/rule.rb
  4. +7 −7 test/outlaw_test.rb
View
@@ -6,8 +6,8 @@
module Outlaw
- def self.outlaw(restriction, message)
- rule = Outlaw::LawParser.parse(restriction, message)
+ def self.outlaw(pattern, message)
+ rule = Rule.new(pattern, message)
Outlaw::Enforcement.add(rule)
end
View
@@ -1,7 +1,7 @@
module Outlaw
module LawParser
extend self
- def parse(restriction, message="")
+ def parse(restriction)
tokens = restriction.split
parsed_restriction = []
tokens.each do |token|
@@ -18,7 +18,7 @@ def parse(restriction, message="")
parsed_restriction += build_regex(token)
end
end
- Rule.new(message, restriction, &build_block(parsed_restriction))
+ build_block(parsed_restriction)
end
private
View
@@ -1,16 +1,19 @@
module Outlaw
class Rule
NoDetectionBlockProvided = Class.new(StandardError)
- attr_reader :message, :restriction
- def initialize(message, restriction, &detection_block)
- raise NoDetectionBlockProvided unless detection_block
- @message = message
- @restriction = restriction
+ attr_reader :message, :pattern, :detection_block
+ def initialize(pattern, message=nil, &detection_block)
+ @pattern = pattern
+ @message = message ? message : "Don't do this: #{pattern}"
@detection_block = detection_block
end
def violation?(code)
- @detection_block.call(code)
+ if detection_block.nil?
+ LawParser.parse(pattern).call(code)
+ else
+ detection_block.call(code)
+ end
end
class << self
View
@@ -25,7 +25,7 @@ def sumthin
it "correctly builds rule for class var" do
code1 = "@@"
- rule1 = LawParser.parse(code1)
+ rule1 = Rule.new(code1)
class_var_file = %{
def badthing(here)
@@ -40,7 +40,7 @@ def badthing(here)
it "correctly builds rule for protected" do
code2 = "protected"
- rule2 = LawParser.parse(code2)
+ rule2 = Rule.new(code2)
protected_file = %{
class Whatever
@@ -61,7 +61,7 @@ def not_really
it "correctly builds rule for eval" do
code3 = "eval"
- rule3 = LawParser.parse(code3)
+ rule3 = Rule.new(code3)
eval_file = %{
def not_really
@@ -79,7 +79,7 @@ def not_really
it "correctly builds rule for module" do
code4 = "module :name end"
- rule4 = LawParser.parse(code4)
+ rule4 = Rule.new(code4)
module_file = %{
module Thing
@@ -95,7 +95,7 @@ module Thing
it "correctly builds rule for core" do
code5 = "class :symbol < :core_class"
- rule5 = LawParser.parse(code5)
+ rule5 = Rule.new(code5)
core_file = %{
class Whatever < String
@@ -112,7 +112,7 @@ def badthing(here)
it "correctly builds rule for rescue nil" do
code7 = "rescue nil"
- rule7 = LawParser.parse(code7)
+ rule7 = Rule.new(code7)
nil_file = %{
begin
@@ -129,7 +129,7 @@ def badthing(here)
it "correctly builds rule for inherit struct.new" do
code8 = "class :symbol < Struct.new"
- rule8 = LawParser.parse(code8)
+ rule8 = Rule.new(code8)
struct_file = %{
class MyClass < Struct.new("Customer", :name, :address)

0 comments on commit 9ff2c30

Please sign in to comment.