fbe 0.0.40 → 0.0.41

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: c5738119a9d73b0ec3e019726bb057059059d0bd5fe08815b0290477902aa4c9
4
- data.tar.gz: f2a54bd36e5672e060d9066fe3aa67077e52dc705da9a2cc3f01002dd9ef1bd4
3
+ metadata.gz: 562ad71216aae43bcbc061bc11381c765eacae86bf7f7deb5e5c2a7a68e9d279
4
+ data.tar.gz: '0942fcb9385e3828e1d50dfde029c8368e3b9f2cd5cd5b0630b732f6b5cf32cf'
5
5
  SHA512:
6
- metadata.gz: c394e6be7d4a376681ba87e308b28b21187454f68725c2fd3774dda934c0fa9a7caadced6e982da9c8f79c6b276abdf4fc2d26e64e325e801e8a0487ddb7dcc2
7
- data.tar.gz: 7815c260f71e1b77fee5a61faaafc41bd5a98843ef250209fb267fc0dc3cd81b466a257ac7452df664bf5e859ab1ae78cc7e23d3e9bc6ad58d24aa33c57750be
6
+ metadata.gz: d683b6cd0ecbf15ec069fee0bf0b92ad1387010bf2ea9cb0fd12d85d3f245d03552941c0ca054cf3d2b3a3730a1bc2419ae16383b442c55bfe674e7e9b8aac42
7
+ data.tar.gz: 4d74ef7c0765883a5f2829cc4b960650736aa2fc7dc0e829bba50de93a34c334196a5f37a324611b8d5b5d8bbeac4e7b4ef725937e4ea7d58ab9215ec50e0722
data/lib/fbe/if_absent.rb CHANGED
@@ -27,7 +27,8 @@ require 'others'
27
27
  require_relative '../fbe'
28
28
  require_relative 'fb'
29
29
 
30
- # Injects a fact if it's absent in the factbase.
30
+ # Injects a fact if it's absent in the factbase, otherwise (it is already
31
+ # there) returns NIL.
31
32
  def Fbe.if_absent(fb: Fbe.fb)
32
33
  attrs = {}
33
34
  f =
@@ -50,7 +51,7 @@ def Fbe.if_absent(fb: Fbe.fb)
50
51
  "(eq #{k} #{vv})"
51
52
  end.join(' ')
52
53
  q = "(and #{q})"
53
- return unless fb.query(q).each.to_a.empty?
54
+ return nil unless fb.query(q).each.to_a.empty?
54
55
  n = fb.insert
55
56
  attrs.each { |k, v| n.send("#{k}=", v) }
56
57
  n
@@ -0,0 +1,59 @@
1
+ # frozen_string_literal: true
2
+
3
+ # MIT License
4
+ #
5
+ # Copyright (c) 2024 Zerocracy
6
+ #
7
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ # of this software and associated documentation files (the "Software"), to deal
9
+ # in the Software without restriction, including without limitation the rights
10
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ # copies of the Software, and to permit persons to whom the Software is
12
+ # furnished to do so, subject to the following conditions:
13
+ #
14
+ # The above copyright notice and this permission notice shall be included in all
15
+ # copies or substantial portions of the Software.
16
+ #
17
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
+ # SOFTWARE.
24
+
25
+ require 'time'
26
+ require 'others'
27
+ require_relative '../fbe'
28
+ require_relative 'fb'
29
+
30
+ # Injects a fact if it's absent in the factbase, otherwise (it is already
31
+ # there) returns the existing one.
32
+ def Fbe.just_one(fb: Fbe.fb)
33
+ attrs = {}
34
+ f =
35
+ others(map: attrs) do |*args|
36
+ k = args[0]
37
+ if k.end_with?('=')
38
+ @map[k[0..-2].to_sym] = args[1]
39
+ else
40
+ @map[k.to_sym]
41
+ end
42
+ end
43
+ yield f
44
+ q = attrs.except('_id', '_time', '_version').map do |k, v|
45
+ vv = v.to_s
46
+ if v.is_a?(String)
47
+ vv = "'#{vv.gsub('"', '\\\\"').gsub("'", "\\\\'")}'"
48
+ elsif v.is_a?(Time)
49
+ vv = v.utc.iso8601
50
+ end
51
+ "(eq #{k} #{vv})"
52
+ end.join(' ')
53
+ q = "(and #{q})"
54
+ before = fb.query(q).each.to_a.first
55
+ return before unless before.nil?
56
+ n = fb.insert
57
+ attrs.each { |k, v| n.send("#{k}=", v) }
58
+ n
59
+ end
data/lib/fbe.rb CHANGED
@@ -27,5 +27,5 @@
27
27
  # License:: MIT
28
28
  module Fbe
29
29
  # Current version of the gem (changed by .rultor.yml on every release)
30
- VERSION = '0.0.40'
30
+ VERSION = '0.0.41'
31
31
  end
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ # MIT License
4
+ #
5
+ # Copyright (c) 2024 Zerocracy
6
+ #
7
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ # of this software and associated documentation files (the "Software"), to deal
9
+ # in the Software without restriction, including without limitation the rights
10
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ # copies of the Software, and to permit persons to whom the Software is
12
+ # furnished to do so, subject to the following conditions:
13
+ #
14
+ # The above copyright notice and this permission notice shall be included in all
15
+ # copies or substantial portions of the Software.
16
+ #
17
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
+ # SOFTWARE.
24
+
25
+ require 'minitest/autorun'
26
+ require 'factbase'
27
+ require_relative '../test__helper'
28
+ require_relative '../../lib/fbe/just_one'
29
+
30
+ # Test.
31
+ # Author:: Yegor Bugayenko (yegor256@gmail.com)
32
+ # Copyright:: Copyright (c) 2024 Zerocracy
33
+ # License:: MIT
34
+ class TestJustOne < Minitest::Test
35
+ def test_ignores
36
+ fb = Factbase.new
37
+ fb.insert.foo = 'hello dude'
38
+ n =
39
+ Fbe.just_one(fb:) do |f|
40
+ f.foo = 'hello dude'
41
+ end
42
+ assert(!n.nil?)
43
+ end
44
+
45
+ def test_injects
46
+ fb = Factbase.new
47
+ n =
48
+ Fbe.just_one(fb:) do |f|
49
+ f.foo = 42
50
+ end
51
+ assert_equal(42, n.foo)
52
+ end
53
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fbe
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.40
4
+ version: 0.0.41
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yegor Bugayenko
@@ -230,6 +230,7 @@ files:
230
230
  - lib/fbe/if_absent.rb
231
231
  - lib/fbe/issue.rb
232
232
  - lib/fbe/iterate.rb
233
+ - lib/fbe/just_one.rb
233
234
  - lib/fbe/middleware.rb
234
235
  - lib/fbe/middleware/quota.rb
235
236
  - lib/fbe/octo.rb
@@ -248,6 +249,7 @@ files:
248
249
  - test/fbe/test_if_absent.rb
249
250
  - test/fbe/test_issue.rb
250
251
  - test/fbe/test_iterate.rb
252
+ - test/fbe/test_just_one.rb
251
253
  - test/fbe/test_octo.rb
252
254
  - test/fbe/test_overwrite.rb
253
255
  - test/fbe/test_pmp.rb