Method: Ferry::Configuration::Filter#initialize

Defined in:
lib/ferry/configuration/filter.rb

#initialize(type, values = nil) ⇒ Filter

Returns a new instance of Filter.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/ferry/configuration/filter.rb', line 6

def initialize type, values = nil
  raise "Invalid filter type #{type}" unless [:host,:role].include? type
  av = Array(values).dup
  @mode = case
          when av.size == 0 then :none
          when av.include?(:all) then :all
          else type
          end
  @rex = case @mode
    when :host
      av.map!{|v| (v.is_a?(String) && v =~ /^(?<name>[-A-Za-z0-9.]+)(,\g<name>)*$/) ? v.split(',') : v }
      av.flatten!
      av.map! do |v|
        case v
        when Regexp then v
        else
          vs = v.to_s
          vs =~ /^[-A-Za-z0-9.]+$/ ? vs : Regexp.new(vs)
        end
      end
      Regexp.union av
    when :role
      av.map!{|v| v.is_a?(String) ? v.split(',') : v }
      av.flatten!
      av.map! do |v|
        case v
        when Regexp then v
        else
          vs = v.to_s
          vs =~ %r{^/(.+)/$} ? Regexp.new($1) : %r{^#{vs}$}
        end
      end
      Regexp.union av
    else
      nil
    end
end