Class: Wcl::Document
- Inherits:
-
Object
- Object
- Wcl::Document
- Defined in:
- lib/wcl/document.rb
Overview
A parsed and evaluated WCL document.
Class Method Summary collapse
Instance Method Summary collapse
- #blocks ⇒ Object
- #blocks_of_type(kind) ⇒ Object
- #close ⇒ Object
- #diagnostics ⇒ Object
- #errors ⇒ Object
- #has_errors? ⇒ Boolean
-
#initialize(handle) ⇒ Document
constructor
A new instance of Document.
- #query(query_str) ⇒ Object
- #to_h ⇒ Object
- #values ⇒ Object
Constructor Details
#initialize(handle) ⇒ Document
Returns a new instance of Document.
6 7 8 9 10 11 12 13 |
# File 'lib/wcl/document.rb', line 6 def initialize(handle) @handle = handle @closed = false @values = nil @diagnostics = nil ObjectSpace.define_finalizer(self, self.class._invoke_release(handle)) end |
Class Method Details
._invoke_release(handle) ⇒ Object
15 16 17 |
# File 'lib/wcl/document.rb', line 15 def self._invoke_release(handle) proc { WasmRuntime.get.document_free(handle) rescue nil } end |
Instance Method Details
#blocks ⇒ Object
51 52 53 54 55 56 |
# File 'lib/wcl/document.rb', line 51 def blocks raise "Document is closed" if @closed json_str = WasmRuntime.get.document_blocks(@handle) Convert.json_to_blocks(json_str) end |
#blocks_of_type(kind) ⇒ Object
58 59 60 61 62 63 |
# File 'lib/wcl/document.rb', line 58 def blocks_of_type(kind) raise "Document is closed" if @closed json_str = WasmRuntime.get.document_blocks_of_type(@handle, kind) Convert.json_to_blocks(json_str) end |
#close ⇒ Object
65 66 67 68 69 70 71 |
# File 'lib/wcl/document.rb', line 65 def close return if @closed @closed = true WasmRuntime.get.document_free(@handle) ObjectSpace.undefine_finalizer(self) end |
#diagnostics ⇒ Object
35 36 37 38 39 |
# File 'lib/wcl/document.rb', line 35 def diagnostics raise "Document is closed" if @closed @diagnostics ||= Convert.json_to_diagnostics(WasmRuntime.get.document_diagnostics(@handle)) end |
#errors ⇒ Object
31 32 33 |
# File 'lib/wcl/document.rb', line 31 def errors diagnostics.select(&:error?) end |
#has_errors? ⇒ Boolean
25 26 27 28 29 |
# File 'lib/wcl/document.rb', line 25 def has_errors? raise "Document is closed" if @closed WasmRuntime.get.document_has_errors(@handle) end |
#query(query_str) ⇒ Object
41 42 43 44 45 46 47 48 49 |
# File 'lib/wcl/document.rb', line 41 def query(query_str) raise "Document is closed" if @closed json_str = WasmRuntime.get.document_query(@handle, query_str) result = JSON.parse(json_str) raise ValueError, result["error"] if result.key?("error") Convert.json_to_ruby(result["ok"]) end |
#to_h ⇒ Object
73 74 75 |
# File 'lib/wcl/document.rb', line 73 def to_h { values: values, has_errors: has_errors?, diagnostics: diagnostics.map(&:inspect) } end |
#values ⇒ Object
19 20 21 22 23 |
# File 'lib/wcl/document.rb', line 19 def values raise "Document is closed" if @closed @values ||= Convert.json_to_values(WasmRuntime.get.document_values(@handle)) end |