Class: Makitzo::World::Query
- Inherits:
-
Object
- Object
- Makitzo::World::Query
- 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
-
#hosts ⇒ Object
readonly
Returns the value of attribute hosts.
-
#roles ⇒ Object
readonly
Returns the value of attribute roles.
Class Method Summary collapse
-
.all ⇒ Object
Returns a Query which will match all hosts.
Instance Method Summary collapse
- #empty? ⇒ Boolean
- #exec(config) ⇒ Object
- #includes?(host) ⇒ Boolean
-
#initialize ⇒ Query
constructor
A new instance of Query.
- #merge!(query) ⇒ Object
Constructor Details
#initialize ⇒ Query
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
#hosts ⇒ Object (readonly)
Returns the value of attribute hosts.
15 16 17 |
# File 'lib/makitzo/world/query.rb', line 15 def hosts @hosts end |
#roles ⇒ Object (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
.all ⇒ Object
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
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
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 |