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.



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

def initialize(model, tasks)
  super

  @in_identity_map = false
end

Instance Attribute Details

#in_identity_mapObject

Returns the value of attribute in_identity_map.



17
18
19
# File 'lib/volt/models/persistors/model_store.rb', line 17

def in_identity_map
  @in_identity_map
end

#modelObject (readonly)

Returns the value of attribute model.



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

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.



115
116
117
118
119
120
121
122
123
124
125
# File 'lib/volt/models/persistors/model_store.rb', line 115

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



127
128
129
# File 'lib/volt/models/persistors/model_store.rb', line 127

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



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

def add_to_collection
  @in_collection = true
  ensure_setup
  changed
end

#add_to_identity_mapObject



44
45
46
47
48
49
50
# File 'lib/volt/models/persistors/model_store.rb', line 44

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



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/volt/models/persistors/model_store.rb', line 69

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

  promise = Promise.new

  ensure_setup

  path_size = path.size
  if save_changes? && 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
      if RUBY_PLATFORM == 'opal'
        StoreTasks.save(collection, path, self_attributes).then do |errors|
          if errors.size == 0
            promise.resolve(nil)
          else
            promise.reject(errors)
          end
        end
      else
        errors = save_to_db!(self_attributes)
        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



36
37
38
39
40
41
42
# File 'lib/volt/models/persistors/model_store.rb', line 36

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



107
108
109
110
111
# File 'lib/volt/models/persistors/model_store.rb', line 107

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



53
54
55
56
57
58
# File 'lib/volt/models/persistors/model_store.rb', line 53

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

  id.join
end

#remove_from_collectionObject



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

def remove_from_collection
  @in_collection = false
end

#save_changes?Boolean

Returns:



60
61
62
63
64
65
66
# File 'lib/volt/models/persistors/model_store.rb', line 60

def save_changes?
  if RUBY_PLATFORM == 'opal'
    return !(defined?($loading_models) && $loading_models) && @tasks
  else
    return true
  end
end