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
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
41 42 43 |
# File 'lib/ar_doc_store/embeddable_model.rb', line 41 def true end |
#ensure_id ⇒ Object
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 |
#inspect ⇒ Object
69 70 71 |
# File 'lib/ar_doc_store/embeddable_model.rb', line 69 def inspect "#{self.class}: #{attributes.inspect}" end |
#mark_embeds_as_persisted ⇒ Object
53 54 55 56 57 58 59 |
# File 'lib/ar_doc_store/embeddable_model.rb', line 53 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
61 62 63 |
# File 'lib/ar_doc_store/embeddable_model.rb', line 61 def new_record? id.blank? end |
#persist ⇒ Object
45 46 47 |
# File 'lib/ar_doc_store/embeddable_model.rb', line 45 def persist run_callbacks :persist end |
#persisted? ⇒ 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_param ⇒ Object
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, , as_json end end value end |