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

#as_json, #decode_encrypted_attributes, #encode_encrypted_attributes, #to_json

Methods inherited from Document

#[], #[]=, #attributes, #initialize

Methods included from ActiveRecordCompat

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

Constructor Details

This class inherits a constructor from CouchbaseOrm::Document

Class Attribute Details

.uuid_generatorObject



274
275
276
# File 'lib/couchbase-orm/base.rb', line 274

def uuid_generator
  @uuid_generator ||= IdGenerator
end

Class Method Details

.bucketObject



262
263
264
# File 'lib/couchbase-orm/base.rb', line 262

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

.bucket=(bucket) ⇒ Object



258
259
260
# File 'lib/couchbase-orm/base.rb', line 258

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

.clusterObject



266
267
268
# File 'lib/couchbase-orm/base.rb', line 266

def cluster
  Connection.cluster
end

.collectionObject



270
271
272
# File 'lib/couchbase-orm/base.rb', line 270

def collection
  CollectionProxy.new(bucket.default_collection)
end

.connect(**options) ⇒ Object



254
255
256
# File 'lib/couchbase-orm/base.rb', line 254

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

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

Returns:



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

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

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



280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# File 'lib/couchbase-orm/base.rb', line 280

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: []



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

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.



367
368
369
# File 'lib/couchbase-orm/base.rb', line 367

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:



358
359
360
# File 'lib/couchbase-orm/base.rb', line 358

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.



349
350
351
# File 'lib/couchbase-orm/base.rb', line 349

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

#id=(value) ⇒ Object



329
330
331
332
333
334
# File 'lib/couchbase-orm/base.rb', line 329

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.



339
340
341
# File 'lib/couchbase-orm/base.rb', line 339

def to_model
  self
end