Class: Volt::Persistors::ModelStore

Inherits:
Store show all
Includes:
StoreState
Defined in:
lib/volt/models/persistors/model_store.rb

Constant Summary collapse

ID_CHARS =
[('a'..'f'), ('0'..'9')].map(&:to_a).flatten

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from StoreState

#change_state_to, #loaded, #state

Methods inherited from Store

#read_new_model, #saved?

Methods inherited from Base

#added, #event_removed, #loaded, #removed

Constructor Details

#initialize(model, tasks) ⇒ ModelStore

Returns a new instance of ModelStore.



14
15
16
17
18
# File 'lib/volt/models/persistors/model_store.rb', line 14

def initialize(model, tasks)
  super

  @in_identity_map = false
end

Instance Attribute Details

#in_identity_mapObject

Returns the value of attribute in_identity_map.



12
13
14
# File 'lib/volt/models/persistors/model_store.rb', line 12

def in_identity_map
  @in_identity_map
end

#modelObject (readonly)

Returns the value of attribute model.



11
12
13
# File 'lib/volt/models/persistors/model_store.rb', line 11

def model
  @model
end

Class Method Details

.changed(model_id, data) ⇒ Object

Update the models based on the id/identity map. Usually these requests will come from the backend.



93
94
95
96
97
98
99
100
101
102
103
# File 'lib/volt/models/persistors/model_store.rb', line 93

def self.changed(model_id, data)
  model = @@identity_map.lookup(model_id)

  if model
    data.each_pair do |key, value|
      if key != :_id
        model.send(:"_#{key}=", value)
      end
    end
  end
end

Instance Method Details

#[](val) ⇒ Object



105
106
107
# File 'lib/volt/models/persistors/model_store.rb', line 105

def [](val)
  fail 'Models do not support hash style lookup.  Hashes inserted into other models are converted to models, see https://github.com/voltrb/volt#automatic-model-conversion'
end

#add_to_collectionObject



20
21
22
23
24
# File 'lib/volt/models/persistors/model_store.rb', line 20

def add_to_collection
  @in_collection = true
  ensure_setup
  changed
end

#add_to_identity_mapObject



39
40
41
42
43
44
45
# File 'lib/volt/models/persistors/model_store.rb', line 39

def add_to_identity_map
  unless @in_identity_map
    @@identity_map.add(@model._id, @model)

    @in_identity_map = true
  end
end

#changed(attribute_name = nil) ⇒ Object

Called when the model changes



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/volt/models/persistors/model_store.rb', line 56

def changed(attribute_name = nil)
  path = @model.path

  promise = Promise.new

  ensure_setup

  path_size = path.size
  if !(defined?($loading_models) && $loading_models) && @tasks && path_size > 0 && !@model.nil?
    if path_size > 3 && (parent = @model.parent) && (source = parent.parent)
      @model.attributes[:"#{path[-4].singularize}_id"] = source._id
    end

    if !collection
      puts 'Attempting to save model directly on store.'
      fail 'Attempting to save model directly on store.'
    else
      StoreTasks.save(collection, self_attributes).then do |errors|
        if errors.size == 0
          promise.resolve(nil)
        else
          promise.reject(errors)
        end
      end
    end
  end
  promise
end

#ensure_setupObject

Called the first time a value is assigned into this model



31
32
33
34
35
36
37
# File 'lib/volt/models/persistors/model_store.rb', line 31

def ensure_setup
  if @model.attributes
    @model.attributes[:_id] ||= generate_id

    add_to_identity_map
  end
end

#event_added(event, first, first_for_event) ⇒ Object



85
86
87
88
89
# File 'lib/volt/models/persistors/model_store.rb', line 85

def event_added(event, first, first_for_event)
  if first_for_event && event == :changed
    ensure_setup
  end
end

#generate_idObject

Create a random unique id that can be used as the mongo id as well



48
49
50
51
52
53
# File 'lib/volt/models/persistors/model_store.rb', line 48

def generate_id
  id = []
  24.times { id << ID_CHARS.sample }

  id.join
end

#remove_from_collectionObject



26
27
28
# File 'lib/volt/models/persistors/model_store.rb', line 26

def remove_from_collection
  @in_collection = false
end