Class: CouchbaseOrm::Document

Inherits:
Object
  • Object
show all
Extended by:
Enum
Includes:
ActiveModel::Attributes, ActiveModel::Dirty, ActiveModel::Model, ActiveModel::Serializers::JSON, ActiveModel::Validations, ActiveModel::Validations::Callbacks, ActiveRecord::Core, ActiveRecordCompat, Encrypt
Defined in:
lib/couchbase-orm/base.rb

Direct Known Subclasses

Base, NestedDocument

Defined Under Namespace

Classes: Metadata, MismatchTypeError

Instance Method Summary collapse

Methods included from Encrypt

#as_json, #decode_encrypted_attributes, #encode_encrypted_attributes, #to_json

Methods included from ActiveRecordCompat

#_has_attribute?, #_write_attribute, #attribute_for_inspect, #attribute_names, #attribute_present?, #format_for_inspect, #has_attribute?, #read_attribute

Constructor Details

#initialize(model = nil, ignore_doc_type: false, **attributes) {|_self| ... } ⇒ Document

Returns a new instance of Document.

Yields:

  • (_self)

Yield Parameters:



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/couchbase-orm/base.rb', line 150

def initialize(model = nil, ignore_doc_type: false, **attributes)
  CouchbaseOrm.logger.debug { "Initialize model #{model} with #{attributes.to_s.truncate(200)}" }
  @__metadata__ = Metadata.new

  super()

  if model
    case model
    when Couchbase::Collection::GetResult
      doc = model.content || raise('empty response provided')
      type = doc.delete('type')
      doc.delete('id')

      if type && !ignore_doc_type && type.to_s != self.class.design_document
        raise CouchbaseOrm::Error::TypeMismatchError.new(
          "document type mismatch, #{type} != #{self.class.design_document}", self
        )
      end

      self.id = attributes[:id] if attributes[:id].present?
      @__metadata__.cas = model.cas

      assign_attributes(decode_encrypted_attributes(doc))
      clear_changes_information
    when CouchbaseOrm::Base
      clear_changes_information
      super(model.attributes.except(:id, 'type'))
    else
      clear_changes_information
      super(decode_encrypted_attributes(**attributes.merge(Hash(model))))
    end
  else
    clear_changes_information
    super(attributes)
  end

  yield self if block_given?

  run_callbacks :initialize
end

Instance Method Details

#[](key) ⇒ Object



195
196
197
# File 'lib/couchbase-orm/base.rb', line 195

def [](key)
  send(key)
end

#[]=(key, value) ⇒ Object



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

def []=(key, value)
  send(:"#{key}=", value)
end

#attributesObject



191
192
193
# File 'lib/couchbase-orm/base.rb', line 191

def attributes
  super.with_indifferent_access
end