Class: CouchbaseOrm::Base

Inherits:
Document show all
Extended by:
EmbedsMany, EmbedsOne, EnsureUnique, Enum, HasMany, IgnoredProperties, Index, Join
Includes:
ActiveRecord::AttributeMethods::Dirty, ActiveRecord::Timestamp, ActiveRecord::Validations, Associations, N1ql, Persistence, QueryHelper, Relation, ValidatesEmbedded, Views
Defined in:
lib/couchbase-orm/base.rb

Constant Summary

Constants included from N1ql

N1ql::NO_VALUE

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from HasMany

build_index, build_index_n1ql, build_index_view, has_many

Methods included from IgnoredProperties

ignored_properties

Methods included from EmbedsOne

embeds_one

Methods included from EmbedsMany

embeds_many

Methods included from N1ql

sanitize

Methods included from Associations

#destroy_associations!, #reset_associations, #update_has_and_belongs_to_many_reverse_association

Methods included from Persistence

#assign_attributes, #delete, #destroy, #destroyed?, #init_with, #new_record?, #persisted?, #reload, #save, #save!, #touch, #update, #update!, #update_attribute, #update_columns

Methods included from Encrypt

#decode_encrypted_attributes, #encode_encrypted_attributes

Methods inherited from Document

#[], #[]=, #attributes, descendants, inherited, #initialize

Methods included from ActiveRecordCompat

#_has_attribute?, #_write_attribute, #attribute_for_inspect, #attribute_names, #attribute_present?, #format_for_inspect, #has_attribute?, #primary_key_values_present?, #read_attribute

Constructor Details

This class inherits a constructor from CouchbaseOrm::Document

Class Attribute Details

.uuid_generatorObject



320
321
322
# File 'lib/couchbase-orm/base.rb', line 320

def uuid_generator
  @uuid_generator ||= IdGenerator
end

Class Method Details

.bucketObject



308
309
310
# File 'lib/couchbase-orm/base.rb', line 308

def bucket
  @bucket ||= BucketProxy.new(Connection.bucket)
end

.bucket=(bucket) ⇒ Object



304
305
306
# File 'lib/couchbase-orm/base.rb', line 304

def bucket=(bucket)
  @bucket = bucket.is_a?(BucketProxy) ? bucket : BucketProxy.new(bucket)
end

.clusterObject



312
313
314
# File 'lib/couchbase-orm/base.rb', line 312

def cluster
  Connection.cluster
end

.collectionObject



316
317
318
# File 'lib/couchbase-orm/base.rb', line 316

def collection
  CollectionProxy.new(bucket.default_collection)
end

.connect(**options) ⇒ Object



300
301
302
# File 'lib/couchbase-orm/base.rb', line 300

def connect(**options)
  @bucket = BucketProxy.new(::MTLibcouchbase::Bucket.new(**options))
end

.exists?(id) ⇒ Boolean Also known as: has_key?

Returns:



352
353
354
355
# File 'lib/couchbase-orm/base.rb', line 352

def exists?(id)
  CouchbaseOrm.logger.debug { "Data - Exists? #{id}" }
  collection.exists(id).exists
end

.find(*ids, **options) ⇒ Object



326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
# File 'lib/couchbase-orm/base.rb', line 326

def find(*ids, **options)
  CouchbaseOrm.logger.debug { "Base.find(l##{ids.length}) #{ids}" }

  chunck = (options.delete(:chunck) || 25).to_i
  quiet = options.delete(:quiet) || false
  ids = ids.flatten.select(&:present?)
  if ids.empty?
    raise CouchbaseOrm::Error::EmptyNotAllowed.new('no id(s) provided') unless quiet
    return nil if quiet
  end

  records = ids.each_slice(chunck).each_with_object([]) do |chunck_ids, res|
    data = Array.wrap(_find_records(chunck_ids, quiet))
    res.push(*data) unless data.empty?
  end

  ids.length > 1 ? records : records.first
end

.find_by_id(*ids, **options) ⇒ Object Also known as: []



345
346
347
348
# File 'lib/couchbase-orm/base.rb', line 345

def find_by_id(*ids, **options)
  options[:quiet] = true
  find(*ids, **options)
end

Instance Method Details

#==(other) ⇒ Object

Public: Overrides == to compare via class and entity id.

other - Another object to compare to

Returns a boolean.



413
414
415
# File 'lib/couchbase-orm/base.rb', line 413

def ==(other)
  super || other.instance_of?(self.class) && !id.nil? && other.id == id
end

#eql?(other) ⇒ Boolean

Public: Overrides eql? to use == in the comparison.

other - Another object to compare to

Returns a boolean.

Returns:



404
405
406
# File 'lib/couchbase-orm/base.rb', line 404

def eql?(other)
  self == other
end

#hashObject

Public: Hashes identifying properties of the instance

Ruby normally hashes an object to be used in comparisons. In our case we may have two techincally different objects referencing the same entity id.

Returns a string representing the unique key.



395
396
397
# File 'lib/couchbase-orm/base.rb', line 395

def hash
  "#{self.class.name}-#{self.id}-#{@__metadata__.cas}-#{@attributes.hash}".hash
end

#id=(value) ⇒ Object



375
376
377
378
379
380
# File 'lib/couchbase-orm/base.rb', line 375

def id=(value)
  raise 'ID cannot be changed' if @__metadata__.cas && value

  attribute_will_change!(:id)
  _write_attribute('id', value)
end

#to_modelObject

Public: Allows for access to ActiveModel functionality.

Returns self.



385
386
387
# File 'lib/couchbase-orm/base.rb', line 385

def to_model
  self
end