Class: CouchbaseOrm::Base

Inherits:
Document show all
Extended by:
EnsureUnique, Enum, HasMany, IgnoredProperties, Index, Join
Includes:
ActiveRecord::AttributeMethods::Dirty, ActiveRecord::Timestamp, ActiveRecord::Validations, Associations, N1ql, Persistence, QueryHelper, Relation, 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 N1ql

sanitize

Methods included from Associations

#destroy_associations!, #reset_associations, #update_has_and_belongs_to_many_reverse_association

Methods included from Persistence

#_create_record, #_update_record, #assign_attributes, #create_or_update, #delete, #destroy, #destroyed?, #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



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

def uuid_generator
  @uuid_generator ||= IdGenerator
end

Class Method Details

.bucketObject



249
250
251
# File 'lib/couchbase-orm/base.rb', line 249

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

.bucket=(bucket) ⇒ Object



245
246
247
# File 'lib/couchbase-orm/base.rb', line 245

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

.clusterObject



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

def cluster
  Connection.cluster
end

.collectionObject



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

def collection
  CollectionProxy.new(bucket.default_collection)
end

.connect(**options) ⇒ Object



241
242
243
# File 'lib/couchbase-orm/base.rb', line 241

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

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

Returns:



296
297
298
299
# File 'lib/couchbase-orm/base.rb', line 296

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

.find(*ids, quiet: false) ⇒ Object



267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# File 'lib/couchbase-orm/base.rb', line 267

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

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

  transcoder = CouchbaseOrm::JsonTranscoder.new(ignored_properties: ignored_properties)
  records = quiet ? collection.get_multi(ids,
                                         transcoder: transcoder) : collection.get_multi!(ids,
                                                                                         transcoder: transcoder)
  return nil if records.nil?

  CouchbaseOrm.logger.debug { "Base.find found(#{records})" }
  records = records.zip(ids).map { |record, id|
    self.new(record, id: id) if record
  }
  records.compact!
  ids.length > 1 ? records : records[0]
end

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



290
291
292
293
# File 'lib/couchbase-orm/base.rb', line 290

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.



341
342
343
# File 'lib/couchbase-orm/base.rb', line 341

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:



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

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.



323
324
325
# File 'lib/couchbase-orm/base.rb', line 323

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

#id=(value) ⇒ Object



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

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.



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

def to_model
  self
end