module AssociateJsonb

encoding: utf-8 frozen_string_literal: true

Constants

SUPPORTED_RAILS_VERSION
VERSION

Public Class Methods

add_hash_type(*classes) click to toggle source

Add class(es) to the list of classes that are able to be upated when `jsonb_set_enabled` is true

# File lib/associate_jsonb.rb, line 65
def self.add_hash_type(*classes)
  self.jsonb_set_added |= classes.flatten
end
disable_jsonb_set(klass = nil, *classes) click to toggle source

Disables the use of `jsonb_nested_set` for hash updates

if passed a class, or a list of classes, those classes will be removed from the list of enabled classes

# File lib/associate_jsonb.rb, line 57
def self.disable_jsonb_set(klass = nil, *classes)
  remove_hash_type(*Array(klass), *classes) unless klass.nil?
  self.jsonb_set_enabled = false
end
enable_jsonb_set(klass = nil, *classes) click to toggle source

Enables the use of `jsonb_nested_set` for hash updates

if passed a class, or a list of classes, those classes will be added todo the enabled classes. if no argument is given, and the enabled class list is empty, `ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Jsonb` is added to the list of enabled classes

# File lib/associate_jsonb.rb, line 43
def self.enable_jsonb_set(klass = nil, *classes)
  if klass.nil?
    add_hash_type jsonb_oid_class if jsonb_hash_types.empty?
  else
    add_hash_type(*Array(klass), *classes)
  end
  self.jsonb_set_enabled = true
end
is_hash?(value) click to toggle source

Returns true if the given value is a descendant of any of the classes in `jsonb_hash_types`

# File lib/associate_jsonb.rb, line 86
def self.is_hash?(value)
  !!value && self.jsonb_hash_types.any? { |type| value.is_a?(type) }
end
merge_hash?(value) click to toggle source

Returns true if `jsonb_set_enabled` is true and the value is an enabled hash type

# File lib/associate_jsonb.rb, line 79
def self.merge_hash?(value)
  !!jsonb_set_enabled && is_hash?(value)
end
remove_hash_type(*classes) click to toggle source

Remove class(es) from the list of classes that are able to be upated when `jsonb_set_enabled` is true

# File lib/associate_jsonb.rb, line 72
def self.remove_hash_type(*classes)
  self.jsonb_set_removed |= classes.flatten
end