Class: Sfn::Lint::Rule
- Inherits:
-
Object
- Object
- Sfn::Lint::Rule
- Defined in:
- lib/sfn/lint/rule.rb
Overview
Composition of definitions
Instance Attribute Summary collapse
-
#definitions ⇒ Array<Definition>
readonly
Definitions composing rule.
-
#fail_message ⇒ String
readonly
Message describing failure.
-
#name ⇒ Symbol
readonly
Name of rule.
-
#provider ⇒ Symbol
readonly
Target provider.
Instance Method Summary collapse
-
#add_definition(definition) ⇒ self
Add a new definition to the collection.
-
#apply(template) ⇒ TrueClass, Array<Smash[:definition, :failures]>
Apply all definitions to template.
-
#fail?(template) ⇒ TrueClass, FalseClass
Check if template fails this rule.
-
#generate_fail_message(results) ⇒ Object
Generate the failure message for this rule with given failure result set.
-
#initialize(name, definitions, fail_message, provider = :aws) ⇒ self
constructor
Create a new rule.
-
#pass?(template) ⇒ TrueClass, FalseClass
Check if template passes this rule.
-
#remove_definition(definition) ⇒ self
Remove a definition from the collection.
-
#validate_definitions! ⇒ Object
Check that provided definitions provider match rule defined provider.
Constructor Details
#initialize(name, definitions, fail_message, provider = :aws) ⇒ self
Create a new rule
24 25 26 27 28 29 30 |
# File 'lib/sfn/lint/rule.rb', line 24 def initialize(name, definitions, , provider = :aws) @name = name.to_sym @definitions = definitions.dup.uniq.freeze @fail_message = @provider = Bogo::Utility.snake(provider).to_sym validate_definitions! end |
Instance Attribute Details
#definitions ⇒ Array<Definition> (readonly)
Returns definitions composing rule.
11 12 13 |
# File 'lib/sfn/lint/rule.rb', line 11 def definitions @definitions end |
#fail_message ⇒ String (readonly)
Returns message describing failure.
13 14 15 |
# File 'lib/sfn/lint/rule.rb', line 13 def @fail_message end |
#name ⇒ Symbol (readonly)
Returns name of rule.
9 10 11 |
# File 'lib/sfn/lint/rule.rb', line 9 def name @name end |
#provider ⇒ Symbol (readonly)
Returns target provider.
15 16 17 |
# File 'lib/sfn/lint/rule.rb', line 15 def provider @provider end |
Instance Method Details
#add_definition(definition) ⇒ self
Add a new definition to the collection
86 87 88 89 90 91 92 |
# File 'lib/sfn/lint/rule.rb', line 86 def add_definition(definition) new_defs = definitions.dup new_defs << definition @definitions = new_defs.uniq.freeze validate_definitions! self end |
#apply(template) ⇒ TrueClass, Array<Smash[:definition, :failures]>
Apply all definitions to template
53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/sfn/lint/rule.rb', line 53 def apply(template) results = definitions.map do |definition| result = definition.apply(template) result == true ? result : Smash.new(:definition => definition, :failures => result) end if results.all? { |item| item == true } true else results.delete_if { |item| item == true } results end end |
#fail?(template) ⇒ TrueClass, FalseClass
Check if template fails this rule
78 79 80 |
# File 'lib/sfn/lint/rule.rb', line 78 def fail?(template) !pass?(template) end |
#generate_fail_message(results) ⇒ Object
Generate the failure message for this rule with given failure result set.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/sfn/lint/rule.rb', line 34 def (results) msg = .dup unless results.empty? failed_items = results.map do |item| f_item = item[:failures] next if f_item.nil? || f_item == true || f_item == false f_item end.flatten.compact.map(&:to_s) unless failed_items.empty? msg = "#{msg} (failures: `#{failed_items.join("`, `")}`)" end end msg end |
#pass?(template) ⇒ TrueClass, FalseClass
Check if template passes this rule
70 71 72 |
# File 'lib/sfn/lint/rule.rb', line 70 def pass?(template) apply(template) == true end |
#remove_definition(definition) ⇒ self
Remove a definition from the collection
98 99 100 101 102 103 |
# File 'lib/sfn/lint/rule.rb', line 98 def remove_definition(definition) new_defs = definitions.dup new_defs.delete(definition) @definitions = new_defs.uniq.freeze self end |
#validate_definitions! ⇒ Object
Check that provided definitions provider match rule defined provider
106 107 108 109 110 111 112 113 114 |
# File 'lib/sfn/lint/rule.rb', line 106 def validate_definitions! non_match = definitions.find_all do |definition| definition.provider != provider end unless non_match.empty? raise ArgumentError.new "Rule defines `#{provider}` as provider but includes definitions for " \ "non matching providers. (#{non_match.map(&:provider).map(&:to_s).uniq.sort.join(", ")})" end end |