class AutomateIt::TagManager::Struct
TagManager::Struct¶ ↑
A TagManager driver for querying a data structure. It’s not useful on its own, but can be subclassed by other drivers that actually load tags.
Constants
- TAG_NEGATION
- TAG_TOKENIZER
- TAG_WORD
Public Instance Methods
hosts_tagged_with(query)
click to toggle source
See TagManager#hosts_tagged_with
# File lib/automateit/tag_manager/struct.rb, line 63 def hosts_tagged_with(query) hosts = @struct.values.flatten.uniq return hosts.select{|hostname| tagged?(query, hostname)} end
setup(opts={})
click to toggle source
Options:
-
:struct –
Hash
to use for queries.
Calls superclass method
AutomateIt::Plugin::Driver#setup
# File lib/automateit/tag_manager/struct.rb, line 14 def setup(opts={}) super(opts) @struct ||= {} @tags ||= Set.new @struct_updated = false @our_hostname_tags ||= Set.new @our_platform_tags ||= Set.new if opts[:struct] @struct_updated = true @struct.merge!(AutomateIt::TagManager::TagParser.expand(opts[:struct])) end end
tagged?(query, hostname=nil)
click to toggle source
See TagManager#tagged?
# File lib/automateit/tag_manager/struct.rb, line 73 def tagged?(query, hostname=nil) query = query.to_s selected_tags = hostname ? tags_for(hostname) : tags # XXX Tokenizer discards unknown characters, which may hide errors in the query tokens = query.scan(TAG_TOKENIZER) if tokens.size > 1 booleans = tokens.map do |token| if matches = token.match(/^(#{TAG_NEGATION})(#{TAG_WORD})$/) selected_tags.include?(matches[2]) && matches[1].empty? else token end end code = booleans.join(" ") begin return eval(code) # XXX What could possibly go wrong? rescue Exception => e raise ArgumentError.new("Invalid query -- #{query}") end else return selected_tags.include?(query) end end