Module: CouchbaseOrm

Defined in:
lib/couchbase-orm.rb,
lib/couchbase-orm/base.rb,
lib/couchbase-orm/n1ql.rb,
lib/couchbase-orm/error.rb,
lib/couchbase-orm/views.rb,
lib/couchbase-orm/encrypt.rb,
lib/couchbase-orm/version.rb,
lib/couchbase-orm/relation.rb,
lib/couchbase-orm/types/raw.rb,
lib/couchbase-orm/connection.rb,
lib/couchbase-orm/types/date.rb,
lib/couchbase-orm/types/hash.rb,
lib/couchbase-orm/persistence.rb,
lib/couchbase-orm/types/array.rb,
lib/couchbase-orm/associations.rb,
lib/couchbase-orm/id_generator.rb,
lib/couchbase-orm/types/nested.rb,
lib/couchbase-orm/utilities/enum.rb,
lib/couchbase-orm/utilities/join.rb,
lib/couchbase-orm/json_transcoder.rb,
lib/couchbase-orm/types/date_time.rb,
lib/couchbase-orm/types/encrypted.rb,
lib/couchbase-orm/types/timestamp.rb,
lib/couchbase-orm/utilities/index.rb,
lib/couchbase-orm/extensions/string.rb,
lib/couchbase-orm/attributes/dynamic.rb,
lib/couchbase-orm/proxies/n1ql_proxy.rb,
lib/couchbase-orm/utilities/has_many.rb,
lib/couchbase-orm/proxies/bucket_proxy.rb,
lib/couchbase-orm/proxies/results_proxy.rb,
lib/couchbase-orm/utilities/query_helper.rb,
lib/couchbase-orm/utilities/ensure_unique.rb,
lib/couchbase-orm/proxies/collection_proxy.rb,
lib/rails/generators/couchbase_orm_generator.rb,
lib/couchbase-orm/utilities/ignored_properties.rb,
lib/rails/generators/couchbase_orm/config/config_generator.rb

Overview

:nodoc:

Defined Under Namespace

Modules: ActiveRecordCompat, Associations, AttributesDynamic, Encrypt, EnsureUnique, Enum, Extensions, Generators, HasMany, IgnoredProperties, Index, Join, N1ql, Persistence, QueryHelper, Relation, Types, Views Classes: Base, BucketProxy, CollectionProxy, Connection, Document, Error, IdGenerator, JsonTranscoder, N1qlProxy, NestedDocument, ResultsProxy

Constant Summary collapse

VERSION =
'1.2.9'

Class Method Summary collapse

Class Method Details

.loggerLogger

if COUCHBASE_ORM_DEBUG environement variable exist then logger is set to Logger::DEBUG level else logger is set to Logger::INFO level

Returns:

  • (Logger)

    current logger setted for CouchbaseOrm



26
27
28
29
30
# File 'lib/couchbase-orm.rb', line 26

def self.logger
  @@logger ||= defined?(Rails) ? Rails.logger : Logger.new(STDOUT).tap { |l|
                                                  l.level = Logger::INFO unless ENV['COUCHBASE_ORM_DEBUG']
                                                }
end

.logger=(logger) ⇒ Logger

Allows you to set a logger for CouchbaseOrm, which can be usueful for logging messages or errors related to CouchbaseOrm

Examples:

Setting the logger in code

require 'logger'
my_logger = Logger.new(STDOUT)
my_logger.level = Logger::DEBUG
CouchbaseOrm.logger =  my_logger

Parameters:

  • logger (Logger)

    your custom logger

Returns:

  • (Logger)

    the new logger setted



43
44
45
# File 'lib/couchbase-orm.rb', line 43

def self.logger=(logger)
  @@logger = logger
end

.try_load(id) ⇒ Object+

Attempts to load a record or records from the Couchbase database.

This method can handle both single IDs and arrays of IDs. It adapts its behavior based on the type and quantity of the input.

Parameters:

  • id (String, Array<String>)

    The ID or array of IDs of the records to load.

Returns:

  • (Object, Array<Object>)

    The loaded model(s). Returns an array of models if the input was an array, or a single model if the input was a single ID.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/couchbase-orm.rb', line 54

def self.try_load(id)
  result = nil
  was_array = id.is_a?(Array)
  query_id = if was_array && id.length == 1
               id.first
             else
               id
             end

  result = query_id.is_a?(Array) ? CouchbaseOrm::Base.bucket.default_collection.get_multi(query_id) : CouchbaseOrm::Base.bucket.default_collection.get(query_id)

  result = Array.wrap(result) if was_array

  if result&.is_a?(Array)
    return result.zip(id).map { |r, id| try_load_create_model(r, id) }.compact
  end

  try_load_create_model(result, id)
end

.try_load_create_model(result, id) ⇒ Object?

Creates a model from the fetched data and ID.

This method checks the type of the fetched document and matches it against the design documents of known models. If a match is found, it creates and returns an instance of the corresponding model.

Parameters:

  • result (Object)

    The fetched record data. Expected to have a `content` method that returns a hash.

  • id (String)

    The ID of the record.

Returns:

  • (Object, nil)

    The created model if a matching model is found, or `nil` if no match is found or if the document type is not present.



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/couchbase-orm.rb', line 82

def self.try_load_create_model(result, id)
  ddoc = result&.content&.[]('type')
  return nil unless ddoc

  ::CouchbaseOrm::Base.descendants.each do |model|
    if model.design_document == ddoc
      return model.new(result, id: id)
    end
  end
  nil
end