Class: Prolego::Query
- Inherits:
-
Object
- Object
- Prolego::Query
- Defined in:
- lib/prolego.rb
Instance Attribute Summary collapse
-
#error ⇒ Object
readonly
Returns the value of attribute error.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
-
#whiny ⇒ Object
readonly
Returns the value of attribute whiny.
Instance Method Summary collapse
- #command(predicate, args) ⇒ Object
- #epilog ⇒ Object
- #escape(arg) ⇒ Object
-
#initialize(filepath, whiny: false) ⇒ Query
constructor
A new instance of Query.
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
#error ⇒ Object (readonly)
Returns the value of attribute error.
8 9 10 |
# File 'lib/prolego.rb', line 8 def error @error end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
8 9 10 |
# File 'lib/prolego.rb', line 8 def status @status end |
#whiny ⇒ Object (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 |
#epilog ⇒ Object
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 |