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.
- #buffer? ⇒ Boolean
-
#promise_for_errors(errors) ⇒ Object
When errors come in, we mark all fields and return a rejected promise.
-
#save!(&block) ⇒ Object
Save saves the contents of a buffer to the save_to location.
- #save_to ⇒ Object
Instance Method Details
#buffer ⇒ Object
Returns a buffered version of the model
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/volt/models/buffer.rb', line 86 def buffer model_path = [:path] model_klass = self.class = .merge(path: model_path, save_to: self, buffer: true).reject { |k, _| k.to_sym == :persistor } model = nil Volt::Model.no_validate do model = model_klass.new(attributes, , :loaded) model.instance_variable_set('@new', @new) end model end |
#promise_for_errors(errors) ⇒ Object
When errors come in, we mark all fields and return a rejected promise.
68 69 70 71 72 73 74 75 |
# File 'lib/volt/models/buffer.rb', line 68 def promise_for_errors(errors) mark_all_fields! # Wrap in an Errors class unless it already is one errors = errors.is_a?(Errors) ? errors : Errors.new(errors) Promise.new.reject(errors) end |
#save!(&block) ⇒ Object
Save saves the contents of a buffer to the save_to location. If the buffer is new, it will create a new model to save to. Otherwise it will be an existing model. Save returns a promise that may fail with validation errors from the server (or the client). You can also pass a block as a shortcut to calling “‘.save!.then do“`
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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/volt/models/buffer.rb', line 7 def save!(&block) # TODO: this shouldn't need to be run, but if no attributes are assigned, then # if needs to be run. Maybe there's a better way to handle it. return validate!.then do # Get errors from validate errors = self.errors.to_h result = nil 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 result = promise.then do |new_model| # The main model saved, so mark the buffer as not new @new = false if new_model # Mark the model as loaded new_model.change_state_to(:loaded_state, :loaded) # 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) # Merge the server errors into the main errors self.errors.merge!(server_errors.to_h) 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 result = promise_for_errors(errors) end # If passed a block, call then on it with the block. result = result.then(&block) if block result end end |
#save_to ⇒ Object
81 82 83 |
# File 'lib/volt/models/buffer.rb', line 81 def save_to [:save_to] end |