Module: ArDocStore::EmbeddableModel::InstanceMethods
- Defined in:
- lib/ar_doc_store/embeddable_model.rb
Instance Method Summary collapse
- #as_json(_ = nil) ⇒ Object
- #embedded? ⇒ Boolean
- #ensure_id ⇒ Object
- #initialize(values = {}) ⇒ Object
- #inspect ⇒ Object
- #mark_embeds_as_persisted ⇒ Object
- #new_record? ⇒ Boolean
- #persist ⇒ Object
- #persisted? ⇒ Boolean
- #read_store_attribute(store, attr) ⇒ Object
- #to_param ⇒ Object
- #write_store_attribute(store, attribute, value) ⇒ Object
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
43 44 45 |
# File 'lib/ar_doc_store/embeddable_model.rb', line 43 def true end |
#ensure_id ⇒ Object
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 |
#inspect ⇒ Object
71 72 73 |
# File 'lib/ar_doc_store/embeddable_model.rb', line 71 def inspect "#{self.class}: #{attributes.inspect}" end |
#mark_embeds_as_persisted ⇒ Object
55 56 57 58 59 60 61 |
# File 'lib/ar_doc_store/embeddable_model.rb', line 55 def json_attributes.values.each do |value| if value.respond_to?(:embedded?) && value. && respond_to?(value.attribute) && !public_send(value.attribute).nil? public_send(value.attribute).persist end end end |
#new_record? ⇒ Boolean
63 64 65 |
# File 'lib/ar_doc_store/embeddable_model.rb', line 63 def new_record? id.blank? end |
#persist ⇒ Object
47 48 49 |
# File 'lib/ar_doc_store/embeddable_model.rb', line 47 def persist run_callbacks :persist end |
#persisted? ⇒ 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_param ⇒ Object
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, , as_json end end value end |