Module: ArDocStore::EmbeddableModel::InstanceMethods

Defined in:
lib/ar_doc_store/embeddable_model.rb

Instance Method Summary collapse

Instance Method Details

#as_json(_ = nil) ⇒ Object



94
95
96
97
98
99
# File 'lib/ar_doc_store/embeddable_model.rb', line 94

def as_json(_=nil)
  attributes.inject({}) do |attrs, attr|
    attrs[attr[0]] = attr[1] unless attr[1].nil?
    attrs
  end
end

#embedded?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/ar_doc_store/embeddable_model.rb', line 43

def embedded?
  true
end

#ensure_idObject



51
52
53
# File 'lib/ar_doc_store/embeddable_model.rb', line 51

def ensure_id
  self.id ||= SecureRandom.uuid
end

#initialize(values = {}) ⇒ Object



36
37
38
39
40
41
# File 'lib/ar_doc_store/embeddable_model.rb', line 36

def initialize(values={})
  super(values)
  values && persisted? && values.keys.each do |key|
    mutations_from_database.forget_change(key)
  end
end

#inspectObject



71
72
73
# File 'lib/ar_doc_store/embeddable_model.rb', line 71

def inspect
  "#{self.class}: #{attributes.inspect}"
end

#mark_embeds_as_persistedObject



55
56
57
58
59
60
61
# File 'lib/ar_doc_store/embeddable_model.rb', line 55

def mark_embeds_as_persisted
  json_attributes.values.each do |value|
    if value.respond_to?(:embedded?) && value.embedded? && respond_to?(value.attribute) && !public_send(value.attribute).nil?
      public_send(value.attribute).persist
    end
  end
end

#new_record?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/ar_doc_store/embeddable_model.rb', line 63

def new_record?
  id.blank?
end

#persistObject



47
48
49
# File 'lib/ar_doc_store/embeddable_model.rb', line 47

def persist
  run_callbacks :persist
end

#persisted?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/ar_doc_store/embeddable_model.rb', line 67

def persisted?
  id.present?
end

#read_store_attribute(store, attr) ⇒ Object



75
76
77
# File 'lib/ar_doc_store/embeddable_model.rb', line 75

def read_store_attribute(store, attr)
  attributes[attr.to_s]
end

#to_paramObject



90
91
92
# File 'lib/ar_doc_store/embeddable_model.rb', line 90

def to_param
  id
end

#write_store_attribute(store, attribute, value) ⇒ Object



79
80
81
82
83
84
85
86
87
88
# File 'lib/ar_doc_store/embeddable_model.rb', line 79

def write_store_attribute(store, attribute, value)
  if parent
    if parent.is_a?(EmbeddedCollection)
      parent.save
    else
      parent.send :write_store_attribute, store, embedded_as, as_json
    end
  end
  value
end