Class: Hotdog::Commands::Search::UnaryExpressionNode

Inherits:
ExpressionNode show all
Defined in:
lib/hotdog/commands/search.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(op, expression) ⇒ UnaryExpressionNode

Returns a new instance of UnaryExpressionNode.



205
206
207
208
# File 'lib/hotdog/commands/search.rb', line 205

def initialize(op, expression)
  @op = op
  @expression = expression
end

Instance Attribute Details

#expressionObject (readonly)

Returns the value of attribute expression.



203
204
205
# File 'lib/hotdog/commands/search.rb', line 203

def expression
  @expression
end

Instance Method Details

#evaluate(environment) ⇒ Object



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/hotdog/commands/search.rb', line 209

def evaluate(environment)
  case @op
  when "!", "~", /\Anot\z/i
    values = @expression.evaluate(environment)
    if values.empty?
      environment.execute("        SELECT DISTINCT host_id FROM hosts_tags;\n      EOS\n    else\n      environment.execute(<<-EOS % values.map { \"?\" }.join(\", \"), values).map { |row| row.first }\n        SELECT DISTINCT host_id FROM hosts_tags WHERE host_id NOT IN (%s);\n      EOS\n    end\n  else\n    raise(SyntaxError.new(\"unknown unary operator: \#{@op}\"))\n  end\nend\n").map { |row| row.first }