factbase 0.0.6 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0d40da30fa9f74e67ebe0ec3b617d70af82e2c9a5dba43fdfda75ce92d056e8e
4
- data.tar.gz: 30140dd08c02d1cfe26ecd5c084a57b6fc02d1c3be89e06514f1dc98d4423ecb
3
+ metadata.gz: 4ff1dd0d8b331a0a2c90d6160e7e0594a186016338591d7fa3702b3fc7e29731
4
+ data.tar.gz: f95d83697fb8fb2aceab49547850a27548858db76a36a7a90a582257174214f0
5
5
  SHA512:
6
- metadata.gz: e98b15bc7b13cc799d171f66a774bdcf4e026846a7c36b558ab281ab24393b8f190104721838dbf53c4da8949b46a2fd7b09bcd0124896b5aa56f1aeea0535c0
7
- data.tar.gz: 422f7b63f44a93bd0ed2145163cc845a3da562ae59f8aa291c9537731bc116610849fd9080c04ca261ae411907b663ee1d063d4565ce580173892409ea93147a
6
+ metadata.gz: 52582171bb9e9fa4be1d2c0670e3faa38de883675eb0db4a0efed94ad8d0d72437c8d1ce54d9e6442ecc4573ec68957cc99138d1a6b4580dfed7822397a863ca
7
+ data.tar.gz: 857be193e43d1340f7c17c9d7230322e0bcd994c228f279a56731168eda36c463d68e8e3177b350f76f6bb70a27a1391e25edf0bccc905333e1502b361423467
@@ -1,6 +1,25 @@
1
+ # Copyright (c) 2024 Yegor Bugayenko
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the 'Software'), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in all
11
+ # copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ # SOFTWARE.
1
20
  ---
2
21
  name: pdd
3
- on:
22
+ 'on':
4
23
  push:
5
24
  branches:
6
25
  - master
@@ -1,3 +1,22 @@
1
+ # Copyright (c) 2024 Yegor Bugayenko
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the 'Software'), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in all
11
+ # copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ # SOFTWARE.
1
20
  ---
2
21
  name: rake
3
22
  on:
@@ -1,6 +1,25 @@
1
+ # Copyright (c) 2024 Yegor Bugayenko
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the 'Software'), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in all
11
+ # copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ # SOFTWARE.
1
20
  ---
2
21
  name: xcop
3
- on:
22
+ 'on':
4
23
  push:
5
24
  pull_request:
6
25
  jobs:
data/factbase.gemspec CHANGED
@@ -26,7 +26,7 @@ Gem::Specification.new do |s|
26
26
  s.required_rubygems_version = Gem::Requirement.new('>= 0') if s.respond_to? :required_rubygems_version=
27
27
  s.required_ruby_version = '>=2.3'
28
28
  s.name = 'factbase'
29
- s.version = '0.0.6'
29
+ s.version = '0.0.8'
30
30
  s.license = 'MIT'
31
31
  s.summary = 'Factbase'
32
32
  s.description = 'Fact base in memory and on disc'
@@ -37,5 +37,8 @@ Gem::Specification.new do |s|
37
37
  s.executables = s.files.grep(%r{^bin/}) { |f| File.basename(f) }
38
38
  s.rdoc_options = ['--charset=UTF-8']
39
39
  s.extra_rdoc_files = ['README.md', 'LICENSE.txt']
40
+ s.add_runtime_dependency 'json', '~> 2.7'
41
+ s.add_runtime_dependency 'nokogiri', '~> 1.10'
42
+ s.add_runtime_dependency 'yaml', '~> 0.3'
40
43
  s.metadata['rubygems_mfa_required'] = 'true'
41
44
  end
data/lib/factbase/fact.rb CHANGED
@@ -20,6 +20,7 @@
20
20
  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
21
  # SOFTWARE.
22
22
 
23
+ require 'json'
23
24
  require_relative '../factbase'
24
25
 
25
26
  # Fact.
@@ -32,25 +33,35 @@ class Factbase::Fact
32
33
  @map = map
33
34
  end
34
35
 
36
+ def to_s
37
+ @map.to_json
38
+ end
39
+
35
40
  def method_missing(*args)
36
41
  k = args[0].to_s
37
42
  if k.end_with?('=')
38
- k = k[0..-2]
43
+ kk = k[0..-2]
44
+ raise "Invalid prop name '#{kk}'" unless kk.match?(/^[a-z][a-zA-Z0-9]+$/)
39
45
  @mutex.synchronize do
40
- @map[k] = [] if @map[k].nil?
41
- @map[k] << args[1]
46
+ before = @map[kk]
47
+ return if before == args[1]
48
+ if before.nil?
49
+ @map[kk] = args[1]
50
+ return
51
+ end
52
+ @map[kk] = [@map[kk]] unless @map[kk].is_a?(Array)
53
+ @map[kk] << args[1]
42
54
  end
43
55
  nil
44
56
  elsif k == '[]'
45
- kk = args[1].to_s
46
- @mutex.synchronize do
47
- @map[kk] = [] if @map[kk].nil?
48
- end
49
- @map[kk]
57
+ @map[args[1].to_s]
50
58
  else
51
59
  v = @map[k]
52
- raise "Can't find '#{k}'" if v.nil?
53
- v[0]
60
+ if v.nil?
61
+ raise "Can't get '#{k}', the fact is empty" if @map.empty?
62
+ raise "Can't find '#{k}' attribute in [#{@map.keys.join(', ')}]"
63
+ end
64
+ v.is_a?(Array) ? v[0] : v
54
65
  end
55
66
  end
56
67
 
data/lib/factbase/term.rb CHANGED
@@ -84,7 +84,7 @@ class Factbase::Term
84
84
  def exists(fact)
85
85
  assert_args(1)
86
86
  k = @operands[0].to_s
87
- !fact[k].empty?
87
+ !fact[k].nil?
88
88
  end
89
89
 
90
90
  def absent(fact)
@@ -97,8 +97,9 @@ class Factbase::Term
97
97
  assert_args(2)
98
98
  k = @operands[0].to_s
99
99
  v = fact[k]
100
- return false if v.empty?
101
- v.include?(@operands[1])
100
+ return true if v == @operands[1]
101
+ return v.include?(@operands[1]) if v.is_a?(Array)
102
+ false
102
103
  end
103
104
 
104
105
  def lt(fact)
data/lib/factbase.rb CHANGED
@@ -20,6 +20,10 @@
20
20
  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
21
  # SOFTWARE.
22
22
 
23
+ require 'json'
24
+ require 'nokogiri'
25
+ require 'yaml'
26
+
23
27
  # Factbase.
24
28
  # Author:: Yegor Bugayenko (yegor256@gmail.com)
25
29
  # Copyright:: Copyright (c) 2024 Yegor Bugayenko
@@ -82,7 +86,39 @@ class Factbase
82
86
  # rubocop:enable Security/MarshalLoad
83
87
  end
84
88
 
89
+ # Convert the entire factbase into JSON.
90
+ # @return [String] The factbase in JSON format
85
91
  def to_json(_ = nil)
86
92
  @maps.to_json
87
93
  end
94
+
95
+ # Convert the entire factbase into XML.
96
+ # @return [String] The factbase in XML format
97
+ def to_xml
98
+ Nokogiri::XML::Builder.new(encoding: 'UTF-8') do |xml|
99
+ xml.fb do
100
+ @maps.each do |m|
101
+ xml.f do
102
+ m.each do |k, vv|
103
+ if vv.is_a?(Array)
104
+ xml.send(:"#{k}") do
105
+ vv.each do |v|
106
+ xml.send(:v, v)
107
+ end
108
+ end
109
+ else
110
+ xml.send(:"#{k}", vv)
111
+ end
112
+ end
113
+ end
114
+ end
115
+ end
116
+ end.to_xml
117
+ end
118
+
119
+ # Convert the entire factbase into YAML.
120
+ # @return [String] The factbase in YAML format
121
+ def to_yaml
122
+ YAML.dump({ 'facts' => @maps })
123
+ end
88
124
  end
@@ -1,6 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- #
4
3
  # Copyright (c) 2024 Yegor Bugayenko
5
4
  #
6
5
  # Permission is hereby granted, free of charge, to any person obtaining a copy
@@ -34,9 +33,30 @@ class TestFact < Minitest::Test
34
33
  map = {}
35
34
  f = Factbase::Fact.new(Mutex.new, map)
36
35
  f.foo = 42
37
- assert_equal(42, f.foo)
36
+ assert_equal(42, f.foo, f.to_s)
38
37
  f.foo = 256
39
- assert_equal(42, f.foo)
40
- assert_equal([42, 256], f['foo'])
38
+ assert_equal(42, f.foo, f.to_s)
39
+ assert_equal([42, 256], f['foo'], f.to_s)
40
+ end
41
+
42
+ def test_fails_when_empty
43
+ f = Factbase::Fact.new(Mutex.new, {})
44
+ assert_raises do
45
+ f.something
46
+ end
47
+ end
48
+
49
+ def test_fails_when_not_found
50
+ f = Factbase::Fact.new(Mutex.new, {})
51
+ f.first = 42
52
+ assert_raises do
53
+ f.second
54
+ end
55
+ end
56
+
57
+ def test_set_by_name
58
+ f = Factbase::Fact.new(Mutex.new, {})
59
+ f.send('foo=', 42)
60
+ assert_equal(42, f.foo, f.to_s)
41
61
  end
42
62
  end
@@ -1,6 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- #
4
3
  # Copyright (c) 2024 Yegor Bugayenko
5
4
  #
6
5
  # Permission is hereby granted, free of charge, to any person obtaining a copy
@@ -1,6 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- #
4
3
  # Copyright (c) 2024 Yegor Bugayenko
5
4
  #
6
5
  # Permission is hereby granted, free of charge, to any person obtaining a copy
@@ -1,6 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- #
4
3
  # Copyright (c) 2024 Yegor Bugayenko
5
4
  #
6
5
  # Permission is hereby granted, free of charge, to any person obtaining a copy
@@ -1,6 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- #
4
3
  # Copyright (c) 2024 Yegor Bugayenko
5
4
  #
6
5
  # Permission is hereby granted, free of charge, to any person obtaining a copy
@@ -1,6 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- #
4
3
  # Copyright (c) 2024 Yegor Bugayenko
5
4
  #
6
5
  # Permission is hereby granted, free of charge, to any person obtaining a copy
data/test/test__helper.rb CHANGED
@@ -1,6 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- #
4
3
  # Copyright (c) 2024 Yegor Bugayenko
5
4
  #
6
5
  # Permission is hereby granted, free of charge, to any person obtaining a copy
@@ -1,6 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- #
4
3
  # Copyright (c) 2024 Yegor Bugayenko
5
4
  #
6
5
  # Permission is hereby granted, free of charge, to any person obtaining a copy
@@ -23,6 +22,8 @@
23
22
 
24
23
  require 'minitest/autorun'
25
24
  require 'json'
25
+ require 'nokogiri'
26
+ require 'yaml'
26
27
  require_relative '../lib/factbase'
27
28
 
28
29
  # Factbase main module test.
@@ -64,4 +65,28 @@ class TestFactbase < Minitest::Test
64
65
  json = JSON.parse(fb.to_json)
65
66
  assert(42, json[0]['foo'][1])
66
67
  end
68
+
69
+ def test_to_xml
70
+ fb = Factbase.new
71
+ f = fb.insert
72
+ f.foo = 42
73
+ f.foo = 256
74
+ fb.insert
75
+ xml = Nokogiri::XML.parse(fb.to_xml)
76
+ assert(!xml.xpath('/fb[count(f) = 2]').empty?)
77
+ assert(!xml.xpath('/fb/f/foo[v="42"]').empty?)
78
+ end
79
+
80
+ def test_to_yaml
81
+ fb = Factbase.new
82
+ f = fb.insert
83
+ f.foo = 42
84
+ f.foo = 256
85
+ fb.insert
86
+ yaml = YAML.load(fb.to_yaml)
87
+ assert_equal(2, yaml['facts'].size)
88
+ assert_equal(42, yaml['facts'][0]['foo'][0])
89
+ assert_equal(256, yaml['facts'][0]['foo'][1])
90
+ assert_equal(2, yaml['facts'][1]['id'])
91
+ end
67
92
  end
metadata CHANGED
@@ -1,15 +1,57 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: factbase
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yegor Bugayenko
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-05-11 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2024-05-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: json
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.7'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: nokogiri
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.10'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.10'
41
+ - !ruby/object:Gem::Dependency
42
+ name: yaml
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.3'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.3'
13
55
  description: Fact base in memory and on disc
14
56
  email: yegor256@gmail.com
15
57
  executables: []