fbe 0.36.1 → 0.37.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -0
- data/lib/fbe/octo.rb +1 -0
- data/lib/fbe/overwrite.rb +34 -6
- data/lib/fbe.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b9c4277fcddc577e646187cc3d3743de609385c1021bbd21382d4aaf0056cbd8
|
4
|
+
data.tar.gz: 140f785a6b8f9707c916eccc070999d84eeb3eeb7fd78dbe263506601a806632
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b03e2a36505b3699fa81962ad8534067de0301f21d565a3aef08c0bb3591ab2fec20fcb8fa66204501ea02edfd722de31f87a9b2e293bdf3b924e6c80cb43713
|
7
|
+
data.tar.gz: 912f5403cb51c1eec1b9c4fe6014f6f089ca2057fabfb8169e17c349b3a2ff5ed2d072a06e8327a6b6d650ac2b8c0580b58b0cb47ddc61d3bed9e4d0542e0244
|
data/Gemfile.lock
CHANGED
data/lib/fbe/octo.rb
CHANGED
data/lib/fbe/overwrite.rb
CHANGED
@@ -16,8 +16,8 @@ require_relative 'fb'
|
|
16
16
|
# an exception will be raised.
|
17
17
|
#
|
18
18
|
# @param [Factbase::Fact] fact The fact to modify (must have _id property)
|
19
|
-
# @param [String]
|
20
|
-
# @param [Any] values The value to set (can be any type, including array)
|
19
|
+
# @param [String, Hash] property_or_hash The name of the property to set, or a hash of properties
|
20
|
+
# @param [Any] values The value to set (can be any type, including array) - ignored if first param is Hash
|
21
21
|
# @param [Factbase] fb The factbase to use (defaults to Fbe.fb)
|
22
22
|
# @return [nil] Nothing
|
23
23
|
# @raise [RuntimeError] If fact is nil, has no _id, or property is not a String
|
@@ -25,11 +25,39 @@ require_relative 'fb'
|
|
25
25
|
# @note If property already has the same single value, no changes are made
|
26
26
|
# @example Update a user's status
|
27
27
|
# user = fb.query('(eq login "john")').first
|
28
|
-
#
|
28
|
+
# Fbe.overwrite(user, 'status', 'active')
|
29
29
|
# # All properties preserved, only 'status' is set to 'active'
|
30
|
-
|
30
|
+
# @example Update multiple properties at once
|
31
|
+
# user = fb.query('(eq login "john")').first
|
32
|
+
# Fbe.overwrite(user, status: 'active', role: 'admin')
|
33
|
+
# # All properties preserved, 'status' and 'role' are updated
|
34
|
+
def Fbe.overwrite(fact, property_or_hash, values = nil, fb: Fbe.fb, fid: '_id')
|
31
35
|
raise 'The fact is nil' if fact.nil?
|
32
36
|
raise 'The fb is nil' if fb.nil?
|
37
|
+
if property_or_hash.is_a?(Hash)
|
38
|
+
before = {}
|
39
|
+
fact.all_properties.each do |k|
|
40
|
+
before[k.to_s] = fact[k]
|
41
|
+
end
|
42
|
+
property_or_hash.each do |k, v|
|
43
|
+
raise "The value for #{k} is nil" if v.nil?
|
44
|
+
before[k.to_s] = v.is_a?(Array) ? v : [v]
|
45
|
+
end
|
46
|
+
id = fact[fid]&.first
|
47
|
+
raise "There is no #{fid} in the fact, cannot use Fbe.overwrite" if id.nil?
|
48
|
+
raise "No facts by #{fid} = #{id}" if fb.query("(eq #{fid} #{id})").delete!.zero?
|
49
|
+
fb.txn do |fbt|
|
50
|
+
n = fbt.insert
|
51
|
+
before.each do |k, vv|
|
52
|
+
next unless n[k].nil?
|
53
|
+
vv.each do |v|
|
54
|
+
n.send(:"#{k}=", v)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
return
|
59
|
+
end
|
60
|
+
property = property_or_hash
|
33
61
|
raise "The property is not a String but #{property.class} (#{property})" unless property.is_a?(String)
|
34
62
|
raise 'The values is nil' if values.nil?
|
35
63
|
values = [values] unless values.is_a?(Array)
|
@@ -41,8 +69,8 @@ def Fbe.overwrite(fact, property, values, fb: Fbe.fb, fid: '_id')
|
|
41
69
|
return
|
42
70
|
end
|
43
71
|
before = {}
|
44
|
-
fact.all_properties.each do |
|
45
|
-
before[
|
72
|
+
fact.all_properties.each do |k|
|
73
|
+
before[k.to_s] = fact[k]
|
46
74
|
end
|
47
75
|
id = fact[fid]&.first
|
48
76
|
raise "There is no #{fid} in the fact, cannot use Fbe.overwrite" if id.nil?
|
data/lib/fbe.rb
CHANGED