Class: Banalize::Policy
- Inherits:
-
Object
- Object
- Banalize::Policy
- Defined in:
- lib/banalize/policy.rb,
lib/banalize/exception.rb,
lib/banalize/policy/severity.rb
Overview
Class defining use of Banalize policies. Mainly for accessing attributes created by Banalize::Files class methods.
Defined Under Namespace
Classes: ArgumentError, Error, RuntimeError, Severity
Constant Summary collapse
- DEFAULT =
Default settings for policies
{ style: :core, severity: Severity.default, description: 'No description' }
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
Class Method Summary collapse
- .method_missing(symbol, *args, &block) ⇒ Object
-
.search(search = nil) ⇒ Hash
Find policy or list of policies by search criteria.
Instance Method Summary collapse
-
#initialize(policy) ⇒ Policy
constructor
A new instance of Policy.
-
#method_missing(symbol, *args, &block) ⇒ Object
Return values of config hash.
Constructor Details
#initialize(policy) ⇒ Policy
Returns a new instance of Policy.
17 18 19 20 21 22 23 |
# File 'lib/banalize/policy.rb', line 17 def initialize policy @config = Files.policies.find { |x| x[:policy] == policy.to_sym } || raise(RuntimeError, "Policy ''#{policy}' not found among policy files") end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(symbol, *args, &block) ⇒ Object
Return values of config hash
87 88 89 90 |
# File 'lib/banalize/policy.rb', line 87 def method_missing symbol, *args, &block return config[symbol] if config.has_key? symbol super symbol, *args, &block end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
25 26 27 |
# File 'lib/banalize/policy.rb', line 25 def config @config end |
Class Method Details
.method_missing(symbol, *args, &block) ⇒ Object
92 93 94 |
# File 'lib/banalize/policy.rb', line 92 def self.method_missing symbol, *args, &block self.new(*args).send symbol end |
.search(search = nil) ⇒ Hash
Find policy or list of policies by search criteria.
Can perform policy search by:
-
policy name (Symbol, String or Regegp)
-
or Hash with :policy and/or :severity keys or nil.
-
‘nil` - no filtering. Simply list of all policies returned.
-
‘:severity` searched for policies with severity value same as
search or higher.
-
‘:style` - can be Symbol or Array of symbols. If it’s :core
or includes :core, all policies returned.
-
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/banalize/policy.rb', line 45 def self.search search=nil case search when nil # If nothing given return all Files.policies when Symbol, String [Files.policies.find { |x| x[:policy] == search.to_sym }] when Regexp Files.policies.find_all { |x| x[:policy] =~ search } when Hash res = Files.policies # if search.has_key? :style search[:style] = [search[:style]].flatten res = if search[:style].include?(:core) res # `core` - includes everything else res.select { |x| search[:style].include? x[:style] } end end # # Find policies with severity this or higher # res = res.select { |x| x[:severity] >= search[:severity] } if search.has_key? :severity res else raise ArgumentError, "Unknown search criteria: #{search.inspect}" end.compact.sort { |a,b| a[:policy] <=> b[:policy] } # By default sort by policy name end |