Sha256: f90485c1df56b5b88fb183287a31ffd0241303572620206d065486700b1339a4

Contents?: true

Size: 1.92 KB

Versions: 36

Compression:

Stored size: 1.92 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, otherwise (it is already
# there) returns the existing one.
def Fbe.just_one(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})"
  before = fb.query(q).each.to_a.first
  return before unless before.nil?
  n = fb.insert
  attrs.each { |k, v| n.send("#{k}=", v) }
  n
end

Version data entries

36 entries across 36 versions & 1 rubygems

Version Path
fbe-0.0.76 lib/fbe/just_one.rb
fbe-0.0.75 lib/fbe/just_one.rb
fbe-0.0.74 lib/fbe/just_one.rb
fbe-0.0.73 lib/fbe/just_one.rb
fbe-0.0.72 lib/fbe/just_one.rb
fbe-0.0.71 lib/fbe/just_one.rb
fbe-0.0.70 lib/fbe/just_one.rb
fbe-0.0.69 lib/fbe/just_one.rb
fbe-0.0.68 lib/fbe/just_one.rb
fbe-0.0.67 lib/fbe/just_one.rb
fbe-0.0.66 lib/fbe/just_one.rb
fbe-0.0.65 lib/fbe/just_one.rb
fbe-0.0.64 lib/fbe/just_one.rb
fbe-0.0.63 lib/fbe/just_one.rb
fbe-0.0.62 lib/fbe/just_one.rb
fbe-0.0.61 lib/fbe/just_one.rb
fbe-0.0.60 lib/fbe/just_one.rb
fbe-0.0.59 lib/fbe/just_one.rb
fbe-0.0.58 lib/fbe/just_one.rb
fbe-0.0.57 lib/fbe/just_one.rb