Sha256: 0f227896a0e7392f8433c59b4ff0b7643f764ca44af78b064a46cd120c2fd40e

Contents?: true

Size: 1.82 KB

Versions: 27

Compression:

Stored size: 1.82 KB

Contents

# frozen_string_literal: true

# MIT License
#
# Copyright (c) 2024 Zerocracy
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

require 'time'
require 'others'
require_relative '../fbe'
require_relative 'fb'

# Injects a fact if it's absent in the factbase.
def Fbe.if_absent(fb: Fbe.fb)
  attrs = {}
  f = others(map: attrs) do |*args|
    k = args[0]
    if k.end_with?('=')
      @map[k[0..-2].to_sym] = args[1]
    else
      @map[k.to_sym]
    end
  end
  yield f
  q = attrs.except('_id', '_time', '_version').map do |k, v|
    vv = v.to_s
    if v.is_a?(String)
      vv = "'#{vv.gsub('"', '\\\\"').gsub("'", "\\\\'")}'"
    elsif v.is_a?(Time)
      vv = v.utc.iso8601
    end
    "(eq #{k} #{vv})"
  end.join(' ')
  q = "(and #{q})"
  return unless fb.query(q).each.to_a.empty?
  n = fb.insert
  attrs.each { |k, v| n.send("#{k}=", v) }
  n
end

Version data entries

27 entries across 27 versions & 1 rubygems

Version Path
fbe-0.0.35 lib/fbe/if_absent.rb
fbe-0.0.34 lib/fbe/if_absent.rb
fbe-0.0.33 lib/fbe/if_absent.rb
fbe-0.0.32 lib/fbe/if_absent.rb
fbe-0.0.31 lib/fbe/if_absent.rb
fbe-0.0.30 lib/fbe/if_absent.rb
fbe-0.0.29 lib/fbe/if_absent.rb
fbe-0.0.28 lib/fbe/if_absent.rb
fbe-0.0.27 lib/fbe/if_absent.rb
fbe-0.0.26 lib/fbe/if_absent.rb
fbe-0.0.25 lib/fbe/if_absent.rb
fbe-0.0.24 lib/fbe/if_absent.rb
fbe-0.0.23 lib/fbe/if_absent.rb
fbe-0.0.22 lib/fbe/if_absent.rb
fbe-0.0.21 lib/fbe/if_absent.rb
fbe-0.0.20 lib/fbe/if_absent.rb
fbe-0.0.19 lib/fbe/if_absent.rb
fbe-0.0.18 lib/fbe/if_absent.rb
fbe-0.0.17 lib/fbe/if_absent.rb
fbe-0.0.16 lib/fbe/if_absent.rb