Module: Volt::Buffer
- Included in:
- Model
- Defined in:
- lib/volt/models/buffer.rb
Instance Method Summary collapse
-
#buffer ⇒ Object
Returns a buffered version of the model.
-
#promise_for_errors(errors) ⇒ Object
When errors come in, we mark all fields and return a rejected promise.
- #save! ⇒ Object
Instance Method Details
#buffer ⇒ Object
Returns a buffered version of the model
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/volt/models/buffer.rb', line 51 def buffer model_path = [:path] # When we grab a buffer off of a plual class (subcollection), we get it as a model. if model_path.last.plural? && model_path[-1] != :[] model_klass = class_at_path(model_path + [:[]]) else model_klass = class_at_path(model_path) end = .merge(path: model_path, save_to: self).reject { |k, _| k.to_sym == :persistor } model = model_klass.new({}, , :loading) if state == :loaded setup_buffer(model) else parent.then do setup_buffer(model) end end model end |
#promise_for_errors(errors) ⇒ Object
When errors come in, we mark all fields and return a rejected promise.
44 45 46 47 48 |
# File 'lib/volt/models/buffer.rb', line 44 def promise_for_errors(errors) mark_all_fields! Promise.new.reject(errors) end |
#save! ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/volt/models/buffer.rb', line 4 def save! # Compute the erros once errors = self.errors if errors.size == 0 save_to = [:save_to] if save_to if save_to.is_a?(ArrayModel) # Add to the collection promise = save_to.append(attributes) else # We have a saved model promise = save_to.assign_attributes(attributes) end return promise.then do |new_model| if new_model # Set the buffer's id to track the main model's id attributes[:_id] = new_model._id [:save_to] = new_model end nil end.fail do |errors| if errors.is_a?(Hash) server_errors.replace(errors) end promise_for_errors(errors) end else fail 'Model is not a buffer, can not be saved, modifications should be persisted as they are made.' end else # Some errors, mark all fields promise_for_errors(errors) end end |