Class: Factbase
- Inherits:
-
Object
- Object
- Factbase
- Defined in:
- lib/factbase.rb
Overview
Factbase.
- Author
-
Yegor Bugayenko ([email protected])
- Copyright
-
Copyright © 2024 Yegor Bugayenko
- License
-
MIT
Defined Under Namespace
Classes: Fact, Looged, Pre, Query, Spy, Syntax, Term, WhiteList
Instance Method Summary collapse
-
#empty? ⇒ Boolean
Is it empty?.
-
#export ⇒ Object
Export it into a chain of bytes.
-
#import(bytes) ⇒ Object
Import from a chain of bytes.
-
#initialize ⇒ Factbase
constructor
Constructor.
-
#insert ⇒ Factbase::Fact
Insert a new fact.
-
#query(query) ⇒ Object
Create a query capable of iterating.
-
#size ⇒ Integer
Size.
-
#to_json(_ = nil) ⇒ String
Convert the entire factbase into JSON.
-
#to_xml ⇒ String
Convert the entire factbase into XML.
-
#to_yaml ⇒ String
Convert the entire factbase into YAML.
Constructor Details
#initialize ⇒ Factbase
Constructor.
33 34 35 36 |
# File 'lib/factbase.rb', line 33 def initialize @maps = [] @mutex = Mutex.new end |
Instance Method Details
#empty? ⇒ Boolean
Is it empty?
40 41 42 |
# File 'lib/factbase.rb', line 40 def empty? @maps.empty? end |
#export ⇒ Object
Export it into a chain of bytes.
82 83 84 |
# File 'lib/factbase.rb', line 82 def export Marshal.dump(@maps) end |
#import(bytes) ⇒ Object
Import from a chain of bytes.
87 88 89 90 91 |
# File 'lib/factbase.rb', line 87 def import(bytes) # rubocop:disable Security/MarshalLoad @maps += Marshal.load(bytes) # rubocop:enable Security/MarshalLoad end |
#insert ⇒ Factbase::Fact
Insert a new fact.
52 53 54 55 56 57 58 59 |
# File 'lib/factbase.rb', line 52 def insert require_relative 'factbase/fact' map = {} @mutex.synchronize do @maps << map end Factbase::Fact.new(@mutex, map) end |
#query(query) ⇒ Object
Create a query capable of iterating.
There is a Lisp-like syntax, for example:
(eq title 'Object Thinking')
(gt time '2024-03-23T03:21:43')
(gt cost 42)
(exists seenBy)
(and
(eq foo 42)
(or
(gt 200)
(absent zzz)))
76 77 78 79 |
# File 'lib/factbase.rb', line 76 def query(query) require_relative 'factbase/query' Factbase::Query.new(@maps, @mutex, query) end |
#size ⇒ Integer
Size.
46 47 48 |
# File 'lib/factbase.rb', line 46 def size @maps.size end |
#to_json(_ = nil) ⇒ String
Convert the entire factbase into JSON.
95 96 97 |
# File 'lib/factbase.rb', line 95 def to_json(_ = nil) @maps.to_json end |
#to_xml ⇒ String
Convert the entire factbase into XML.
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/factbase.rb', line 101 def to_xml Nokogiri::XML::Builder.new(encoding: 'UTF-8') do |xml| xml.fb do @maps.each do |m| xml.f_ do m.each do |k, vv| if vv.is_a?(Array) xml.send(:"#{k}_") do vv.each do |v| xml.send(:v, v) end end else xml.send(:"#{k}_", vv) end end end end end end.to_xml end |
#to_yaml ⇒ String
Convert the entire factbase into YAML.
125 126 127 |
# File 'lib/factbase.rb', line 125 def to_yaml YAML.dump({ 'facts' => @maps }) end |