Class: MiniDefender::RulesFactory

Inherits:
Object
  • Object
show all
Defined in:
lib/mini_defender/rules_factory.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.blueprintsObject



5
6
7
# File 'lib/mini_defender/rules_factory.rb', line 5

def self.blueprints
  @@blueprints ||= []
end

.register(klass) ⇒ Object



9
10
11
# File 'lib/mini_defender/rules_factory.rb', line 9

def self.register(klass)
  blueprints << klass
end

Instance Method Details

#init_rule(signature) ⇒ Object

Raises:

  • (ArgumentError)


33
34
35
36
37
38
39
40
41
# File 'lib/mini_defender/rules_factory.rb', line 33

def init_rule(signature)
  @blueprints ||= RulesFactory.blueprints

  signature = signature.split(':')
  blueprint = @blueprints.find { |b| b.available? && b.signature == signature[0] }
  raise ArgumentError, "The rule name #{signature[0]} is invalid or not available" if blueprint.nil?

  blueprint.make(signature[1..].join(':').split(','))
end

#init_set(rule_set) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/mini_defender/rules_factory.rb', line 13

def init_set(rule_set)
  if rule_set.is_a?(String)
    rule_set = rule_set.split('|')
  end

  unless rule_set.is_a?(Array)
    raise ArgumentError, 'Rule set must be a string or an array'
  end

  rule_set = rule_set.map do |rule|
    unless rule.is_a?(String) || rule.is_a?(Rule)
      raise ArgumentError, 'Rule must be a string or an instance of MiniDefender::Rule'
    end

    rule.is_a?(String) ? init_rule(rule) : rule
  end

  rule_set.sort_by! { |r| r.priority }
end