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, ActiveRecordCompat, Changeable, Encrypt, Inspectable, StrictLoading
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 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

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

Returns a new instance of Document.

Yields:

  • (_self)

Yield Parameters:



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/couchbase-orm/base.rb', line 56

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 = HashWithIndifferentAccess.new(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))
        when CouchbaseOrm::Base
            clear_changes_information
            super(model.attributes.except(:id, 'type'))
        else
            clear_changes_information
            assign_attributes(decode_encrypted_attributes(**attributes.merge(Hash(model)).symbolize_keys))
        end
    else
        clear_changes_information
        super(attributes)
    end

    yield self if block_given?

    init_strict_loading
    run_callbacks :initialize
end

Instance Method Details

#[](key) ⇒ Object



95
96
97
# File 'lib/couchbase-orm/base.rb', line 95

def [](key)
    send(key)
end

#[]=(key, value) ⇒ Object



99
100
101
# File 'lib/couchbase-orm/base.rb', line 99

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