Module: Volt::Buffer

Included in:
Model
Defined in:
lib/volt/models/buffer.rb

Instance Method Summary collapse

Instance Method Details

#bufferObject

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 = options[:path]

  model_klass = self.class

  new_options = options.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, new_options, :loaded)

    model.instance_variable_set('@new', @new)
  end

  model
end

#buffer?Boolean

Returns:



77
78
79
# File 'lib/volt/models/buffer.rb', line 77

def buffer?
  options[:buffer]
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 = options[: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
            options[: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_toObject



81
82
83
# File 'lib/volt/models/buffer.rb', line 81

def save_to
  options[:save_to]
end