Class: Persistors::ModelStore

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

Constant Summary collapse

ID_CHARS =
[('a'..'z'), ('A'..'Z'), ('0'..'9')].map {|v| v.to_a }.flatten

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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.



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

def initialize(model, tasks)
  super

  @in_identity_map = false
end

Instance Attribute Details

#in_identity_mapObject

Returns the value of attribute in_identity_map.



9
10
11
# File 'lib/volt/models/persistors/model_store.rb', line 9

def in_identity_map
  @in_identity_map
end

#modelObject (readonly)

Returns the value of attribute model.



8
9
10
# File 'lib/volt/models/persistors/model_store.rb', line 8

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.



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/volt/models/persistors/model_store.rb', line 76

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



88
89
90
# File 'lib/volt/models/persistors/model_store.rb', line 88

def [](val)
  raise "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



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

def add_to_collection
  @in_collection = true
  ensure_setup
  changed
end

#add_to_identity_mapObject



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

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



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/volt/models/persistors/model_store.rb', line 53

def changed(attribute_name=nil)
  # puts "CHANGED: #{attribute_name.inspect} - #{@model.inspect}"
  ensure_setup

  path_size = @model.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[:"#{@model.path[-4].singularize}_id"] = source._id
    end

    puts "Save: #{collection} - #{self_attributes.inspect} - #{@model.path.inspect}"
    @tasks.call('StoreTasks', 'save', collection, self_attributes)
  end
end

#ensure_setupObject

Called the first time a value is assigned into this model



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

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

    add_to_identity_map
  end
end

#event_added(event, scope_provider, first) ⇒ Object



68
69
70
71
72
# File 'lib/volt/models/persistors/model_store.rb', line 68

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

#generate_idObject

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



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

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

  return id.join
end

#remove_from_collectionObject



23
24
25
# File 'lib/volt/models/persistors/model_store.rb', line 23

def remove_from_collection
  @in_collection = false
end