Class: Makitzo::World::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/makitzo/world/query.rb

Overview

Query enables selection of a subset of hosts based on name/role

Defined Under Namespace

Classes: StringArray

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeQuery

Returns a new instance of Query.



17
18
19
# File 'lib/makitzo/world/query.rb', line 17

def initialize
  @roles, @hosts = StringArray.new, StringArray.new
end

Instance Attribute Details

#hostsObject (readonly)

Returns the value of attribute hosts.



15
16
17
# File 'lib/makitzo/world/query.rb', line 15

def hosts
  @hosts
end

#rolesObject (readonly)

Returns the value of attribute roles.



15
16
17
# File 'lib/makitzo/world/query.rb', line 15

def roles
  @roles
end

Class Method Details

.allObject

Returns a Query which will match all hosts



11
12
13
# File 'lib/makitzo/world/query.rb', line 11

def self.all
  new
end

Instance Method Details

#empty?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/makitzo/world/query.rb', line 21

def empty?
  @roles.empty? && @hosts.empty?
end

#exec(config) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/makitzo/world/query.rb', line 35

def exec(config)
  if !empty?
    hosts, all_hosts = [], config.hosts
    
    if @roles.length > 0
      hosts |= all_hosts.select { |host| (@roles & (host.roles.map { |r| r.name })).any? }
    end
    
    if @hosts.length > 0
      host_patterns = @hosts.map { |h| Regexp.new('^' + h.gsub('.', '\\.').gsub('*', '.*?') + '$') }
      hosts |= all_hosts.select { |host| host_patterns.any? { |hp| host.name =~ hp } }
    end
  else
    hosts = config.hosts
  end
  
  hosts
end

#includes?(host) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/makitzo/world/query.rb', line 31

def includes?(host)
  empty? || @hosts.include?(host.to_s) || (host.roles.any? { |r| @roles.include?(r.to_s) })
end

#merge!(query) ⇒ Object



25
26
27
28
29
# File 'lib/makitzo/world/query.rb', line 25

def merge!(query)
  @roles |= query.roles
  @hosts |= query.hosts
  self
end