Module: ArDocStore::EmbeddableModel::InstanceMethods

Defined in:
lib/ar_doc_store/embeddable_model.rb

Instance Method Summary collapse

Instance Method Details

#as_json(_ = nil) ⇒ Object



92
93
94
95
96
97
# File 'lib/ar_doc_store/embeddable_model.rb', line 92

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)


41
42
43
# File 'lib/ar_doc_store/embeddable_model.rb', line 41

def embedded?
  true
end

#ensure_idObject



49
50
51
# File 'lib/ar_doc_store/embeddable_model.rb', line 49

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

#initialize(values = {}) ⇒ Object



34
35
36
37
38
39
# File 'lib/ar_doc_store/embeddable_model.rb', line 34

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

#inspectObject



69
70
71
# File 'lib/ar_doc_store/embeddable_model.rb', line 69

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

#mark_embeds_as_persistedObject



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

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)


61
62
63
# File 'lib/ar_doc_store/embeddable_model.rb', line 61

def new_record?
  id.blank?
end

#persistObject



45
46
47
# File 'lib/ar_doc_store/embeddable_model.rb', line 45

def persist
  run_callbacks :persist
end

#persisted?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/ar_doc_store/embeddable_model.rb', line 65

def persisted?
  id.present?
end

#read_store_attribute(store, attr) ⇒ Object



73
74
75
# File 'lib/ar_doc_store/embeddable_model.rb', line 73

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

#to_paramObject



88
89
90
# File 'lib/ar_doc_store/embeddable_model.rb', line 88

def to_param
  id
end

#write_store_attribute(store, attribute, value) ⇒ Object



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

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