Class: Embiggen::ShortenerList

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/embiggen/shortener_list.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(domains) ⇒ ShortenerList

Returns a new instance of ShortenerList.



11
12
13
# File 'lib/embiggen/shortener_list.rb', line 11

def initialize(domains)
  @domains = Set.new(domains.map { |domain| host_pattern(domain) })
end

Instance Attribute Details

#domainsObject (readonly)

Returns the value of attribute domains.



9
10
11
# File 'lib/embiggen/shortener_list.rb', line 9

def domains
  @domains
end

Instance Method Details

#+(other) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/embiggen/shortener_list.rb', line 19

def +(other)
  other_patterns = if other.respond_to?(:domains)
                     other.domains
                   else
                     Set.new(other.map { |d| host_pattern(d) })
                   end

  self.class.allocate.tap do |result|
    result.instance_variable_set(:@domains, domains | other_patterns)
  end
end

#<<(domain) ⇒ Object



31
32
33
34
35
# File 'lib/embiggen/shortener_list.rb', line 31

def <<(domain)
  domains << host_pattern(domain)

  self
end

#delete(domain) ⇒ Object



37
38
39
# File 'lib/embiggen/shortener_list.rb', line 37

def delete(domain)
  domains.delete(host_pattern(domain))
end

#host_pattern(domain) ⇒ Object



43
44
45
# File 'lib/embiggen/shortener_list.rb', line 43

def host_pattern(domain)
  /\b#{Regexp.escape(domain)}\z/i
end

#include?(uri) ⇒ Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/embiggen/shortener_list.rb', line 15

def include?(uri)
  domains.any? { |domain| uri.host =~ domain }
end