Sha256: a7ba76cb0f41d8a752fa883268a3a3f85969f9455da2db3b95a121075a2f4325

Contents?: true

Size: 1.56 KB

Versions: 1

Compression:

Stored size: 1.56 KB

Contents

require 'ferry/configuration'

module Ferry
  class Configuration
    class Filter
      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
      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
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ferry-2.0.0 lib/ferry/configuration/filter.rb