Class: Prolego::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/prolego.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filepath, whiny: false) ⇒ Query

Returns a new instance of Query.



10
11
12
13
# File 'lib/prolego.rb', line 10

def initialize(filepath, whiny: false)
  @file  = filepath
  @whiny = whiny
end

Instance Attribute Details

#errorObject (readonly)

Returns the value of attribute error.



8
9
10
# File 'lib/prolego.rb', line 8

def error
  @error
end

#statusObject (readonly)

Returns the value of attribute status.



8
9
10
# File 'lib/prolego.rb', line 8

def status
  @status
end

#whinyObject (readonly)

Returns the value of attribute whiny.



8
9
10
# File 'lib/prolego.rb', line 8

def whiny
  @whiny
end

Instance Method Details

#command(predicate, args) ⇒ Object



15
16
17
18
19
# File 'lib/prolego.rb', line 15

def command(predicate, args)
  command = "swipl -qf '#{@file}' -g '#{escape(predicate)}(#{escape(args)}),halt'"
  @output, @error, status = Open3.capture3(command)
  @status = status.exitstatus
end

#epilogObject



25
26
27
28
# File 'lib/prolego.rb', line 25

def epilog
  fail "Prolog exit status #{@status}. Check errors with query.error" if @status != 0 && whiny
  JSON.parse @output.gsub("'", "\"") rescue @output
end

#escape(arg) ⇒ Object



21
22
23
# File 'lib/prolego.rb', line 21

def escape(arg)
  arg.to_s.gsub(/^\[|"|'|\]$/, "[" => "", "]" => "", "'" => "'\"'\"'", "\"" => "'\"'\"'")
end