lib/sfn/lint/rule_set.rb in sfn-3.0.28 vs lib/sfn/lint/rule_set.rb in sfn-3.0.30
- old
+ new
@@ -5,55 +5,48 @@
# Named collection of rules
class RuleSet
# Helper class for ruleset generation
class Creator
-
attr_reader :items, :provider
def initialize(provider)
@provider = provider
@items = []
end
class RuleSet < Creator
-
def rule(name, &block)
r = Rule.new(provider)
r.instance_exec(&block)
items << Sfn::Lint::Rule.new(name, r.items, r.fail_message, provider)
end
-
end
class Rule < Creator
-
- def definition(expr, evaluator=nil, &block)
+ def definition(expr, evaluator = nil, &block)
items << Sfn::Lint::Definition.new(expr, provider, evaluator, &block)
end
- def fail_message(val=nil)
- unless(val.nil?)
+ def fail_message(val = nil)
+ unless val.nil?
@fail_message = val
end
@fail_message
end
-
end
-
end
class << self
-
@@_rule_set_registry = Smash.new
# RuleSet generator helper for quickly building simple rule sets
#
# @param name [String] name of rule set
# @param provider [String, Symbol] target provider
# @yieldblock rule set content
- def build(name, provider=:aws, &block)
+ def build(name, provider = :aws, &block)
provider = Bogo::Utility.snake(provider).to_sym
rs = Creator::RuleSet.new(provider)
rs.instance_exec(&block)
self.new(name, provider, rs.items)
end
@@ -70,23 +63,22 @@
# Get registered rule set
#
# @param name [String] name of rule set
# @param provider [String] target provider
# @return [RuleSet, NilClass]
- def get(name, provider=:aws)
+ def get(name, provider = :aws)
provider = Bogo::Utility.snake(provider)
@@_rule_set_registry.get(provider, name)
end
# Get all rule sets for specified provider
#
# @param provider [String] target provider
# @return [Array<RuleSet>]
- def get_all(provider=:aws)
+ def get_all(provider = :aws)
@@_rule_set_registry.fetch(provider, {}).values
end
-
end
include Bogo::Memoization
# @return [Symbol] name
@@ -100,11 +92,11 @@
#
# @param name [String, Symbol] name of rule set
# @param provider [String, Symbol] name of target provider
# @param rules [Array<Rule>] list of rules defining this set
# @return [self]
- def initialize(name, provider=:aws, rules=[])
+ def initialize(name, provider = :aws, rules = [])
@name = name.to_sym
@provider = Bogo::Utility.snake(provider).to_sym
@rules = rules.dup.uniq.freeze
validate_rules!
end
@@ -136,11 +128,11 @@
#
# @param template [Hash]
# @return [TrueClass, Array<String>] true on success, list failure messages on failure
def apply(template)
failures = collect_failures(template)
- if(failures.empty?)
+ if failures.empty?
true
else
failures.map do |failure|
failure[:rule].generate_fail_message(failure[:result])
end
@@ -155,23 +147,22 @@
def collect_failures(template)
results = rules.map do |rule|
result = rule.apply(template)
result == true ? true : Smash.new(:rule => rule, :result => result)
end
- results.delete_if{|i| i == true}
+ results.delete_if { |i| i == true }
results
end
# Check that provided rules provider match rule set defined provider
def validate_rules!
non_match = rules.find_all do |rule|
rule.provider != provider
end
- unless(non_match.empty?)
+ unless non_match.empty?
raise ArgumentError.new "Rule set defines `#{provider}` as provider but includes rules for " \
- "non matching providers. (#{non_match.map(&:provider).map(&:to_s).uniq.sort.join(', ')})"
+ "non matching providers. (#{non_match.map(&:provider).map(&:to_s).uniq.sort.join(', ')})"
end
end
-
end
end
end