Module: NoBrainer::Document::Persistance
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/no_brainer/document/persistance.rb
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
- #_create(options = {}) ⇒ Object
- #_initialize(attrs = {}, options = {}) ⇒ Object
- #_reload(options = {}) ⇒ Object
- #_save?(options = {}) ⇒ Boolean
- #_update(attrs) ⇒ Object
- #_update_only_changed_attrs(options = {}) ⇒ Object
- #delete ⇒ Object
- #destroy ⇒ Object
- #destroyed? ⇒ Boolean
- #new_record? ⇒ Boolean
- #persisted? ⇒ Boolean
- #reload(options = {}) ⇒ Object
- #save(*args) ⇒ Object
- #save!(*args) ⇒ Object
- #save?(options = {}) ⇒ Boolean
- #update(*args) ⇒ Object (also: #update_attributes)
- #update!(*args) ⇒ Object (also: #update_attributes!)
- #update?(attrs, options = {}) ⇒ Boolean
Instance Method Details
#_create(options = {}) ⇒ Object
57 58 59 60 61 62 63 64 |
# File 'lib/no_brainer/document/persistance.rb', line 57 def _create(={}) attrs = self.class.persistable_attributes(@_attributes) result = NoBrainer.run(self.class.rql_table.insert(attrs)) self.pk_value ||= result['generated_keys'].to_a.first @new_record = false unlock_unique_fields # just an optimization for the uniquness validation true end |
#_initialize(attrs = {}, options = {}) ⇒ Object
4 5 6 7 |
# File 'lib/no_brainer/document/persistance.rb', line 4 def _initialize(attrs={}, ={}) @new_record = ![:from_db] super end |
#_reload(options = {}) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/no_brainer/document/persistance.rb', line 21 def _reload(={}) criteria = root_class.raw if opt = [:missing_attributes] criteria = criteria.pluck(opt[:pluck]) if opt[:pluck] criteria = criteria.without(opt[:without]) if opt[:without] end attrs = criteria.find(pk_value) = .merge(:pristine => true, :from_db => true) if [:keep_ivars] assign_attributes(attrs, ) else instance_variables.each { |ivar| remove_instance_variable(ivar) } initialize(attrs, ) end self end |
#_save?(options = {}) ⇒ Boolean
86 87 88 |
# File 'lib/no_brainer/document/persistance.rb', line 86 def _save?(={}) new_record? ? _create() : _update_only_changed_attrs() end |
#_update(attrs) ⇒ Object
66 67 68 69 |
# File 'lib/no_brainer/document/persistance.rb', line 66 def _update(attrs) rql = ->(doc){ self.class.persistable_attributes(attrs, :rql_doc => doc) } NoBrainer.run { selector.update(&rql) } end |
#_update_only_changed_attrs(options = {}) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/no_brainer/document/persistance.rb', line 71 def _update_only_changed_attrs(={}) # We won't be using the `changes` values, because they went through # read_attribute(), and we want the raw values. attrs = Hash[self.changed.map do |k| attr = @_attributes[k] # If we have a hash to save, we need to specify r.literal(), # otherwise, the hash would just get merged with the existing one. attr = RethinkDB::RQL.new.literal(attr) if attr.is_a?(Hash) [k, attr] end] _update(attrs) if attrs.present? unlock_unique_fields # just an optimization for the uniquness validation true end |
#delete ⇒ Object
117 118 119 120 121 122 123 124 |
# File 'lib/no_brainer/document/persistance.rb', line 117 def delete unless @destroyed NoBrainer.run { selector.delete } @destroyed = true end @_attributes.freeze true end |
#destroy ⇒ Object
126 127 128 |
# File 'lib/no_brainer/document/persistance.rb', line 126 def destroy delete end |
#destroyed? ⇒ Boolean
13 14 15 |
# File 'lib/no_brainer/document/persistance.rb', line 13 def destroyed? !!@destroyed end |
#new_record? ⇒ Boolean
9 10 11 |
# File 'lib/no_brainer/document/persistance.rb', line 9 def new_record? !!@new_record end |
#persisted? ⇒ Boolean
17 18 19 |
# File 'lib/no_brainer/document/persistance.rb', line 17 def persisted? !new_record? && !destroyed? end |
#reload(options = {}) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/no_brainer/document/persistance.rb', line 41 def reload(={}) [:without, :pluck].each do |type| next unless v = .delete(type) v = Hash[v.flatten.map { |k| [k, true] }] if v.is_a?(Array) v = {v => true} unless v.is_a?(Hash) v = v.select { |k,_v| _v } v = v.with_indifferent_access next unless v.present? [:missing_attributes] ||= {} [:missing_attributes][type] = v end _reload() end |
#save(*args) ⇒ Object
98 99 100 |
# File 'lib/no_brainer/document/persistance.rb', line 98 def save(*args) save?(*args) end |
#save!(*args) ⇒ Object
94 95 96 |
# File 'lib/no_brainer/document/persistance.rb', line 94 def save!(*args) save?(*args) or raise NoBrainer::Error::DocumentInvalid, self end |
#save?(options = {}) ⇒ Boolean
90 91 92 |
# File 'lib/no_brainer/document/persistance.rb', line 90 def save?(={}) _save?() end |
#update(*args) ⇒ Object Also known as: update_attributes
112 113 114 |
# File 'lib/no_brainer/document/persistance.rb', line 112 def update(*args) update?(*args) end |
#update!(*args) ⇒ Object Also known as: update_attributes!
107 108 109 |
# File 'lib/no_brainer/document/persistance.rb', line 107 def update!(*args) update?(*args) or raise NoBrainer::Error::DocumentInvalid, self end |
#update?(attrs, options = {}) ⇒ Boolean
102 103 104 105 |
# File 'lib/no_brainer/document/persistance.rb', line 102 def update?(attrs, ={}) assign_attributes(attrs, ) save?() end |