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/index_schema.rb,
lib/couchbase-orm/types/nested.rb,
lib/couchbase-orm/configuration.rb,
lib/couchbase-orm/index_migrator.rb,
lib/couchbase-orm/utilities/enum.rb,
lib/couchbase-orm/utilities/join.rb,
lib/couchbase-orm/index_migration.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/index_definition.rb,
lib/couchbase-orm/extensions/string.rb,
lib/couchbase-orm/attributes/dynamic.rb,
lib/couchbase-orm/proxies/n1ql_proxy.rb,
lib/couchbase-orm/utilities/embedded.rb,
lib/couchbase-orm/utilities/has_many.rb,
lib/couchbase-orm/index_config_loader.rb,
lib/couchbase-orm/index_schema/dumper.rb,
lib/couchbase-orm/index_schema/loader.rb,
lib/couchbase-orm/proxies/bucket_proxy.rb,
lib/couchbase-orm/utilities/embeds_one.rb,
lib/couchbase-orm/proxies/results_proxy.rb,
lib/couchbase-orm/utilities/embeds_many.rb,
lib/couchbase-orm/index_schema_migration.rb,
lib/couchbase-orm/utilities/query_helper.rb,
lib/couchbase-orm/index_migration_context.rb,
lib/couchbase-orm/utilities/ensure_unique.rb,
lib/couchbase-orm/proxies/collection_proxy.rb,
lib/couchbase-orm/index_migration_generator.rb,
lib/couchbase-orm/index_migration/operations.rb,
lib/rails/generators/couchbase_orm_generator.rb,
lib/couchbase-orm/utilities/ignored_properties.rb,
lib/couchbase-orm/utilities/validates_embedded.rb,
lib/couchbase-orm/index_migration/query_builder.rb,
lib/couchbase-orm/index_migration/command_recorder.rb,
lib/couchbase-orm/index_migration/index_introspector.rb,
lib/couchbase-orm/index_migration/index_state_fetcher.rb,
lib/couchbase-orm/index_migration/migration_generator.rb,
lib/couchbase-orm/validators/polymorphic_type_validator.rb,
lib/couchbase-orm/validators/embedded_associated_validator.rb,
lib/rails/generators/couchbase_orm/config/config_generator.rb

Overview

:nodoc:

Defined Under Namespace

Modules: ActiveRecordCompat, Associations, AttributesDynamic, Embedded, EmbedsMany, EmbedsOne, Encrypt, EnsureUnique, Enum, Extensions, Generators, HasMany, IgnoredProperties, Index, Join, N1ql, Persistence, QueryHelper, Relation, Types, ValidatesEmbedded, Views Classes: Base, BucketProxy, CollectionProxy, Configuration, Connection, Document, EmbeddedAssociatedValidator, Error, IdGenerator, IndexConfigLoader, IndexDefinition, IndexMigration, IndexMigrationContext, IndexMigrationGenerator, IndexMigrator, IndexSchema, IndexSchemaMigration, JsonTranscoder, N1qlProxy, NestedDocument, PolymorphicTypeValidator, ResultsProxy

Constant Summary collapse

VERSION =
'1.6.6'

Class Method Summary collapse

Class Method Details

.configObject



39
40
41
# File 'lib/couchbase-orm.rb', line 39

def self.config
  @config ||= Configuration.new
end

.configure {|config| ... } ⇒ Object

Yields:



35
36
37
# File 'lib/couchbase-orm.rb', line 35

def self.configure
  yield(config)
end

.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



50
51
52
53
54
# File 'lib/couchbase-orm.rb', line 50

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



67
68
69
# File 'lib/couchbase-orm.rb', line 67

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

.reset_config!Object



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

def self.reset_config!
  @config = Configuration.new
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.



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/couchbase-orm.rb', line 78

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.



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/couchbase-orm.rb', line 106

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

  models_to_visit = [::CouchbaseOrm::Base]
  visited_models = {}

  until models_to_visit.empty?
    model = models_to_visit.shift
    next if visited_models[model]

    visited_models[model] = true

    if model != ::CouchbaseOrm::Base && model.design_document == ddoc
      return model.instantiate(result.content, id, nil, model)
    end

    models_to_visit.concat(Array.wrap(model.descendants)) if model.respond_to?(:descendants)
  end

  nil
end