Class: CouchbaseOrm::Base
- 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
Class Attribute Summary collapse
Class Method Summary collapse
- .bucket ⇒ Object
- .bucket=(bucket) ⇒ Object
- .cluster ⇒ Object
- .collection ⇒ Object
- .connect(**options) ⇒ Object
- .exists?(id) ⇒ Boolean (also: has_key?)
- .find(*ids, **options) ⇒ Object
- .find_by_id(*ids, **options) ⇒ Object (also: [])
Instance Method Summary collapse
-
#==(other) ⇒ Object
Public: Overrides == to compare via class and entity id.
-
#eql?(other) ⇒ Boolean
Public: Overrides eql? to use == in the comparison.
-
#hash ⇒ Object
Public: Hashes identifying properties of the instance.
- #id=(value) ⇒ Object
-
#to_model ⇒ Object
Public: Allows for access to ActiveModel functionality.
Methods included from HasMany
build_index, build_index_n1ql, build_index_view, has_many
Methods included from IgnoredProperties
Methods included from EmbedsOne
Methods included from EmbedsMany
Methods included from N1ql
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_generator ⇒ Object
320 321 322 |
# File 'lib/couchbase-orm/base.rb', line 320 def uuid_generator @uuid_generator ||= IdGenerator end |
Class Method Details
.bucket ⇒ Object
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 |
.cluster ⇒ Object
312 313 314 |
# File 'lib/couchbase-orm/base.rb', line 312 def cluster Connection.cluster end |
.collection ⇒ Object
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(**) @bucket = BucketProxy.new(::MTLibcouchbase::Bucket.new(**)) end |
.exists?(id) ⇒ Boolean Also known as: has_key?
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, **) CouchbaseOrm.logger.debug { "Base.find(l##{ids.length}) #{ids}" } chunck = (.delete(:chunck) || 25).to_i quiet = .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, **) [:quiet] = true find(*ids, **) 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.
404 405 406 |
# File 'lib/couchbase-orm/base.rb', line 404 def eql?(other) self == other end |
#hash ⇒ Object
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_model ⇒ Object
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 |