Class: Hub::SshConfig::HostPattern

Inherits:
Object
  • Object
show all
Defined in:
lib/hub/ssh_config.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pattern) ⇒ HostPattern

Returns a new instance of HostPattern.



29
30
31
# File 'lib/hub/ssh_config.rb', line 29

def initialize pattern
  @pattern = pattern.to_s.downcase
end

Class Method Details

.pattern_to_regexp(pattern) ⇒ Object



52
53
54
55
56
57
# File 'lib/hub/ssh_config.rb', line 52

def self.pattern_to_regexp pattern
  escaped = Regexp.escape(pattern)
  escaped.gsub!('\*', '.*')
  escaped.gsub!('\?', '.')
  /^#{escaped}$/i
end

Instance Method Details

#==(other) ⇒ Object



34
# File 'lib/hub/ssh_config.rb', line 34

def ==(other) other.to_s == self.to_s end

#match?(hostname) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/hub/ssh_config.rb', line 48

def match? hostname
  matcher.call hostname
end

#matcherObject



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/hub/ssh_config.rb', line 36

def matcher
  @matcher ||=
    if '*' == @pattern
      Proc.new { true }
    elsif @pattern !~ /[?*]/
      lambda { |hostname| hostname.to_s.downcase == @pattern }
    else
      re = self.class.pattern_to_regexp @pattern
      lambda { |hostname| re =~ hostname }
    end
end

#to_sObject



33
# File 'lib/hub/ssh_config.rb', line 33

def to_s() @pattern end