Class: CouchbaseOrm::Base

Inherits:
Document show all
Extended by:
EnsureUnique, Enum, HasMany, IgnoredProperties, 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 IgnoredProperties

ignored_properties, ignored_properties=

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 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



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

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

.bucketObject



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

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

.bucket=(bucket) ⇒ Object



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

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

.clusterObject



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

def cluster
    Connection.cluster
end

.collectionObject



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

def collection
    CollectionProxy.new(bucket.default_collection)
end

.connect(**options) ⇒ Object



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

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

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

Returns:



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

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

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



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

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



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

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

.uuid_generatorObject



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

def uuid_generator
    @uuid_generator ||= IdGenerator
end

.uuid_generator=(generator) ⇒ Object



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

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.



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

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:



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

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.



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

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

#id=(value) ⇒ Object

Raises:

  • (RuntimeError)


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

def id=(value)
    raise RuntimeError, '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.



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

def to_model
    self
end