Class: Sfn::Lint::RuleSet
- Inherits:
-
Object
- Object
- Sfn::Lint::RuleSet
- Includes:
- Bogo::Memoization
- Defined in:
- lib/sfn/lint/rule_set.rb
Overview
Named collection of rules
Defined Under Namespace
Classes: Creator
Constant Summary collapse
- @@_rule_set_registry =
Smash.new
Instance Attribute Summary collapse
-
#name ⇒ Symbol
readonly
Name.
-
#provider ⇒ Symbol
readonly
Target provider.
-
#rules ⇒ Array<Rule>
readonly
Rules of set.
Class Method Summary collapse
-
.build(name, provider = :aws, &block) ⇒ Object
RuleSet generator helper for quickly building simple rule sets.
-
.get(name, provider = :aws) ⇒ RuleSet, NilClass
Get registered rule set.
-
.get_all(provider = :aws) ⇒ Array<RuleSet>
Get all rule sets for specified provider.
-
.register(rule_set) ⇒ TrueClass
Register a rule set.
Instance Method Summary collapse
-
#add_rule(rule) ⇒ self
Add a new rule to the collection.
-
#apply(template) ⇒ TrueClass, Array<String>
Apply rule set to template.
-
#collect_failures(template) ⇒ Array<Rule>
Process template through rules defined in this set and store failure information.
-
#initialize(name, provider = :aws, rules = []) ⇒ self
constructor
Create new rule set.
-
#remove_rule(rule) ⇒ self
Remove a rule from the collection.
-
#validate_rules! ⇒ Object
Check that provided rules provider match rule set defined provider.
Constructor Details
#initialize(name, provider = :aws, rules = []) ⇒ self
Create new rule set
97 98 99 100 101 102 |
# File 'lib/sfn/lint/rule_set.rb', line 97 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 |
Instance Attribute Details
#name ⇒ Symbol (readonly)
Returns name.
85 86 87 |
# File 'lib/sfn/lint/rule_set.rb', line 85 def name @name end |
#provider ⇒ Symbol (readonly)
Returns target provider.
87 88 89 |
# File 'lib/sfn/lint/rule_set.rb', line 87 def provider @provider end |
#rules ⇒ Array<Rule> (readonly)
Returns rules of set.
89 90 91 |
# File 'lib/sfn/lint/rule_set.rb', line 89 def rules @rules end |
Class Method Details
.build(name, provider = :aws, &block) ⇒ Object
RuleSet generator helper for quickly building simple rule sets
47 48 49 50 51 52 |
# File 'lib/sfn/lint/rule_set.rb', line 47 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 |
.get(name, provider = :aws) ⇒ RuleSet, NilClass
Get registered rule set
68 69 70 71 |
# File 'lib/sfn/lint/rule_set.rb', line 68 def get(name, provider = :aws) provider = Bogo::Utility.snake(provider) @@_rule_set_registry.get(provider, name) end |
.get_all(provider = :aws) ⇒ Array<RuleSet>
Get all rule sets for specified provider
77 78 79 |
# File 'lib/sfn/lint/rule_set.rb', line 77 def get_all(provider = :aws) @@_rule_set_registry.fetch(provider, {}).values end |
.register(rule_set) ⇒ TrueClass
Register a rule set
58 59 60 61 |
# File 'lib/sfn/lint/rule_set.rb', line 58 def register(rule_set) @@_rule_set_registry.set(rule_set.provider, rule_set.name, rule_set) true end |
Instance Method Details
#add_rule(rule) ⇒ self
Add a new rule to the collection
108 109 110 111 112 113 114 |
# File 'lib/sfn/lint/rule_set.rb', line 108 def add_rule(rule) new_rules = rules.dup new_rules << rule @rules = new_rules.uniq.freeze validate_rules! self end |
#apply(template) ⇒ TrueClass, Array<String>
Apply rule set to template.
131 132 133 134 135 136 137 138 139 140 |
# File 'lib/sfn/lint/rule_set.rb', line 131 def apply(template) failures = collect_failures(template) if failures.empty? true else failures.map do |failure| failure[:rule].(failure[:result]) end end end |
#collect_failures(template) ⇒ Array<Rule>
Process template through rules defined in this set and store failure information
147 148 149 150 151 152 153 154 |
# File 'lib/sfn/lint/rule_set.rb', line 147 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 end |
#remove_rule(rule) ⇒ self
Remove a rule from the collection
120 121 122 123 124 125 |
# File 'lib/sfn/lint/rule_set.rb', line 120 def remove_rule(rule) new_rules = rules.dup new_rules.delete(rule) @rules = new_rules.uniq.freeze self end |
#validate_rules! ⇒ Object
Check that provided rules provider match rule set defined provider
157 158 159 160 161 162 163 164 165 |
# File 'lib/sfn/lint/rule_set.rb', line 157 def validate_rules! non_match = rules.find_all do |rule| rule.provider != provider end 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(", ")})" end end |