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
#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_generator ⇒ Object
274 275 276 |
# File 'lib/couchbase-orm/base.rb', line 274 def uuid_generator @uuid_generator ||= IdGenerator end |
Class Method Details
.bucket ⇒ Object
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 |
.cluster ⇒ Object
266 267 268 |
# File 'lib/couchbase-orm/base.rb', line 266 def cluster Connection.cluster end |
.collection ⇒ Object
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(**) @bucket = BucketProxy.new(::MTLibcouchbase::Bucket.new(**)) end |
.exists?(id) ⇒ Boolean Also known as: has_key?
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, **) 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: []
299 300 301 302 |
# File 'lib/couchbase-orm/base.rb', line 299 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.
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.
358 359 360 |
# File 'lib/couchbase-orm/base.rb', line 358 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.
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_model ⇒ Object
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 |