Class: CouchbaseOrm::Base

Inherits:
Document show all
Extended by:
EnsureUnique, Enum, HasMany, Index, Join, JsonSchema::Validation, PropertiesAlwaysExistsInDocument
Includes:
Associations, N1ql, Persistence, QueryHelper, Relation, Timestamps, Views
Defined in:
lib/couchbase-orm/base.rb

Constant Summary

Constants included from PropertiesAlwaysExistsInDocument

PropertiesAlwaysExistsInDocument::DEFAULT_VALUE

Constants included from N1ql

N1ql::DEFAULT_SCAN_CONSISTENCY, N1ql::NO_VALUE

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 JsonSchema::Validation

json_validation_config, validate_json_schema

Methods included from PropertiesAlwaysExistsInDocument

properties_always_exists_in_document, properties_always_exists_in_document=

Methods included from N1ql

config, 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, #delete, #destroy, #destroyed?, #new_record?, #perform_validations, #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

#[], #[]=, #initialize

Methods included from IgnoredProperties

#ignored_properties, #ignored_properties=

Methods included from StrictLoading

#init_strict_loading, #strict_loading!, #strict_loading?

Methods included from ActiveRecordCompat

#_has_attribute?, #_write_attribute, #attribute_for_inspect, #attribute_names, #attribute_present?, #has_attribute?, #slice, #values_at

Methods included from Changeable

#_children, #attribute_before_last_save, #changed, #changed?, #changed_attributes, #changes, #changes_applied, #children_changed?, #move_changes, #previous_changes, #reset_object!, #saved_change_to_attribute, #saved_change_to_attribute?, #setters, #will_save_change_to_attribute?

Methods included from Inspectable

#inspect

Constructor Details

This class inherits a constructor from CouchbaseOrm::Document

Class Method Details

.attribute(name) ⇒ Object



143
144
145
146
147
# File 'lib/couchbase-orm/base.rb', line 143

def attribute(name, ...)
    super
    create_dirty_methods(name, name)
    create_setters(name)
end

.bucketObject



157
158
159
# File 'lib/couchbase-orm/base.rb', line 157

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

.bucket=(bucket) ⇒ Object



153
154
155
# File 'lib/couchbase-orm/base.rb', line 153

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

.clusterObject



161
162
163
# File 'lib/couchbase-orm/base.rb', line 161

def cluster
    Connection.cluster
end

.collectionObject



165
166
167
# File 'lib/couchbase-orm/base.rb', line 165

def collection
    CollectionProxy.new(bucket.default_collection)
end

.connect(**options) ⇒ Object



149
150
151
# File 'lib/couchbase-orm/base.rb', line 149

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

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

Returns:



206
207
208
209
# File 'lib/couchbase-orm/base.rb', line 206

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

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



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/couchbase-orm/base.rb', line 177

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

    ids = ids.flatten.select { |id| id.present? }
    if ids.empty?
        raise CouchbaseOrm::Error::EmptyNotAllowed, 'no id(s) provided'
    end

    transcoder = CouchbaseOrm::JsonTranscoder.new(ignored_properties: ignored_properties)
    records = quiet ? collection.get_multi(ids, transcoder: transcoder) : collection.get_multi!(ids, transcoder: transcoder)
    CouchbaseOrm.logger.debug { "Base.find found(#{records})" }
    records = records.zip(ids).map { |record, id|
        next unless record
        next if record.error
        new(record, id: id).tap do |instance|
            if with_strict_loading
                instance.strict_loading!
            end
        end.tap(&:reset_object!)
    }.compact
    ids.length > 1 ? records : records[0]
end

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



200
201
202
203
# File 'lib/couchbase-orm/base.rb', line 200

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

.uuid_generatorObject



169
170
171
# File 'lib/couchbase-orm/base.rb', line 169

def uuid_generator
    @uuid_generator ||= IdGenerator
end

.uuid_generator=(generator) ⇒ Object



173
174
175
# File 'lib/couchbase-orm/base.rb', line 173

def uuid_generator=(generator)
    @uuid_generator = generator
end

Instance Method Details

#==(other) ⇒ Object

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

other - Another object to compare to

Returns a boolean.



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

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:



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

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.



232
233
234
# File 'lib/couchbase-orm/base.rb', line 232

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

#id=(value) ⇒ Object

Raises:

  • (RuntimeError)


213
214
215
216
217
# File 'lib/couchbase-orm/base.rb', line 213

def id=(value)
    raise RuntimeError, 'ID cannot be changed' if .cas && value
    attribute_will_change!(:id)
    _write_attribute("id", value)
end

#to_modelObject

Public: Allows for access to ActiveModel functionality.

Returns self.



222
223
224
# File 'lib/couchbase-orm/base.rb', line 222

def to_model
    self
end