Class: Wcl::Document

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

Overview

A parsed and evaluated WCL document.

Class Method Summary collapse

Instance Method Summary collapse

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

#blocksObject



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

#closeObject



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

#diagnosticsObject



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

#errorsObject



31
32
33
# File 'lib/wcl/document.rb', line 31

def errors
  diagnostics.select(&:error?)
end

#has_errors?Boolean

Returns:

  • (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

Raises:



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_hObject



73
74
75
# File 'lib/wcl/document.rb', line 73

def to_h
  { values: values, has_errors: has_errors?, diagnostics: diagnostics.map(&:inspect) }
end

#valuesObject



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