Class: Banalize::Registry
- Defined in:
- lib/banalize/registry.rb,
lib/banalize/exception.rb
Overview
Class defining use of Banalize policies DSL. Sets some sane default values for each of the DSL methods and registers new policy in the list of policies.
Instance attributes
Other attributes are inherited from parent Parser class.
Class methods
Class methods define DSL for Banalizer. These are:
Defined Under Namespace
Classes: ArgumentError, Error, RuntimeError
Constant Summary collapse
- @@parsers =
TODO: how to load parsers ????
[]
Instance Attribute Summary collapse
-
#default ⇒ Object
Instance level accessor for the defaults .
-
#errors ⇒ Object
Instance of Errors class to hold all error messages from tests.
Attributes inherited from Parser
#code, #comments, #lines, #path, #shebang
Attributes included from Parser::ShellVariables
Class Method Summary collapse
-
.config ⇒ Hash
Return configuration for the policy.
-
.default(hash = nil) ⇒ Object
Set defaults for the policy.
- .description(hlp = nil) ⇒ Object
- .parser(name) ⇒ Object
-
.policy(name = nil) ⇒ Object
Name of this policy.
-
.register(myname, &block) ⇒ Object
Define new policy from loading Ruby file with policy.
-
.severity(sev = nil) ⇒ Object
Use lowest severity by default.
-
.style(p = ) ⇒ Object
Default style is ‘bug’.
-
.synopsis(desc = nil) ⇒ Object
Short summary of the policy.
Instance Method Summary collapse
-
#initialize(bash) ⇒ Registry
constructor
Creates new instance of policy check.
-
#run ⇒ Object
This method must be overwritten in children classes.
Methods included from Parser::ShellVariables
Methods included from Parser::PodStyleComments
Constructor Details
#initialize(bash) ⇒ Registry
Creates new instance of policy check.
96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/banalize/registry.rb', line 96 def initialize bash raise RuntimeError, "File does not exist: #{bash}" unless File.exists? bash @path = bash @errors = Errors.new self # Make class level default variable accessible as instance level # variable and accessor super @path @default = self.class.default.merge( $styles[self.class.config[:policy]] || {} ) end |
Instance Attribute Details
#default ⇒ Object
Instance level accessor for the defaults
Instance defaults hold same data as in class level default method. Instance level data are merged on initializing with personal styles data.
116 117 118 |
# File 'lib/banalize/registry.rb', line 116 def default @default end |
#errors ⇒ Object
Instance of Errors class to hold all error messages from tests
119 120 121 |
# File 'lib/banalize/registry.rb', line 119 def errors @errors end |
Class Method Details
.config ⇒ Hash
Return configuration for the policy.
Same as config parameter of ‘other’ checks.
202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/banalize/registry.rb', line 202 def self.config { policy: policy, synopsis: synopsis, style: style, severity: severity, description: description, klass: name, default: default } end |
.default(hash = nil) ⇒ Object
Set defaults for the policy. Defaults are hash with values used in #run method. When defining defaults, define them as:
“‘
default :max => 20
“‘
During run defaults accessible as default
Defaults can be overwriten by personal style configuration file. See CONFIGURATION for details.
151 152 153 |
# File 'lib/banalize/registry.rb', line 151 def self.default hash=nil @default ||= hash end |
.description(hlp = nil) ⇒ Object
166 167 168 169 170 171 172 |
# File 'lib/banalize/registry.rb', line 166 def self.description hlp=nil if hlp @description = hlp else @description ||= "No description available for #{self.name}" end end |
.parser(name) ⇒ Object
87 88 89 |
# File 'lib/banalize/registry.rb', line 87 def self.parser name @@parsers << name end |
.policy(name = nil) ⇒ Object
Name of this policy.
131 132 133 134 135 136 137 |
# File 'lib/banalize/registry.rb', line 131 def self.policy name=nil if name @policy = name.to_sym else @policy ||= self.name.underscore.to_sym end end |
.register(myname, &block) ⇒ Object
Define new policy from loading Ruby file with policy.
## Example
# First argument to the `banalizer` method call defines
# policy name and short description (AKA synopsis).
# Both `description` and `policy_name` methods are
# optional in the block.
banalizer "Check that format of shebang is #!/usr/bin/env bash" do
severity 5 # Default is 1
synopsis "Can provide alternative description here"
style 'bug'
policy_name "Don't need to specify policy name,it is defined from argument to `describe` method"
# All method calls are optional. Only required things
# are policy description and definition of method
# `run`. Method `run` must be overwritten, otherwise
# it simply raises execption.
#
# Run method must return something that evaluates into
# `true` (policy check successful) or `false`.
#
def run
lines.first =~ %r{^\#!/usr/bin/env\s+bash}
end
end
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/banalize/registry.rb', line 64 def self.register myname, &block klass = myname.to_s.gsub(/\W/, '_').camelize c = Object.const_set klass, Class.new(self , &block) c.synopsis myname c.default({}) # make sure defaults are initialized c.severity Policy::DEFAULT[:severity] unless c.severity # Set default severity if it's not defined in block # Override these with Styles file begin c.description $styles[myname][:description] c.severity $styles[myname][:severity] rescue NoMethodError => e end return c end |
.severity(sev = nil) ⇒ Object
Use lowest severity by default
184 185 186 187 188 189 190 |
# File 'lib/banalize/registry.rb', line 184 def self.severity sev=nil if sev @severity = Banalize::Policy::Severity.to_i(sev) else @severity ||= Banalize::Policy::Severity.to_i(sev) end end |
.style(p = ) ⇒ Object
Default style is ‘bug’
177 178 179 |
# File 'lib/banalize/registry.rb', line 177 def self.style p=Policy::DEFAULT[:style] @style ||= p end |
.synopsis(desc = nil) ⇒ Object
Short summary of the policy. Synopsis comes from the name of the policy which is first parameter to the ‘banalizer` method, but can be overwwritten by calling systemu DSL method in the block for the `banalizer` method.
162 163 164 |
# File 'lib/banalize/registry.rb', line 162 def self.synopsis desc=nil @synopsis ||= desc.to_s.humanize end |
Instance Method Details
#run ⇒ Object
This method must be overwritten in children classes.
123 124 125 |
# File 'lib/banalize/registry.rb', line 123 def run raise ArgumentError, "You must override #run method in class ''#{self.class.policy_name}'" end |