Class: Ferry::Configuration::Filter

Inherits:
Object
  • Object
show all
Defined in:
lib/ferry/configuration/filter.rb

Instance Method Summary collapse

Constructor Details

#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

Instance Method Details

#filter(servers) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/ferry/configuration/filter.rb', line 43

def filter servers
  as = Array(servers)
  case @mode
  when :none then return []
  when :all  then return servers
  when :host
    as.select {|s| @rex.match s.to_s}
  when :role
    as.select { |s| s.is_a?(String) ? false : s.roles.any? {|r| @rex.match r} }
  end
end