Support negation of conditions by prefixing them with "^"

This commit is contained in:
Paul van Tilburg 2014-11-16 00:32:32 +01:00
parent 4abf514b90
commit 6c78b621c4
1 changed files with 19 additions and 13 deletions

32
hued
View File

@ -124,19 +124,25 @@ class Hued
return true if @conditions.empty? return true if @conditions.empty?
@conditions.map do |cond| @conditions.map do |cond|
if cond.is_a? Hash cond_negate = false
cond_name, cond_value = cond.to_a.first res = if cond.is_a? Hash
case cond_name cond_name, cond_value = cond.to_a.first
when "from" if cond_name[0] == "^"
Time.now >= Chronic.parse(cond_value) cond_negate = true
when "until" cond_name = cond_name[1..-1]
Time.now <= Chronic.parse(cond_value) end
when "found host" case cond_name
system("ping -W3 -c1 -q #{cond_value} > /dev/null") when "from"
end Time.now >= Chronic.parse(cond_value)
else when "until"
@log.warn "Unknown condition type/form #{cond.inspect}" Time.now <= Chronic.parse(cond_value)
end when "found host"
system("ping -W3 -c1 -q #{cond_value} > /dev/null")
end
else
@log.warn "Unknown condition type/form #{cond.inspect}"
end
cond_negate ? !res : res
end.all? end.all?
end end