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
- @@identity_map =
{}
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from Store
#change_channel_connection, #initialize, #read_new_model
Methods inherited from Base
#added, #loaded, #removed
Instance Attribute Details
Returns the value of attribute model.
9
10
11
|
# File 'lib/volt/models/persistors/model_store.rb', line 9
def model
@model
end
|
Class Method Details
.from_id(id) ⇒ Object
124
125
126
|
# File 'lib/volt/models/persistors/model_store.rb', line 124
def self.from_id(id)
@@identity_map[id]
end
|
.update(model_id, data) ⇒ Object
Update the models based on the id/identity map. Usually these requests will come from the backend.
112
113
114
115
116
117
118
119
120
121
122
|
# File 'lib/volt/models/persistors/model_store.rb', line 112
def self.update(model_id, data)
persistor = @@identity_map[model_id]
if persistor
data.each_pair do |key, value|
if key != '_id'
persistor.model.send(:"#{key}=", value)
end
end
end
end
|
Instance Method Details
#add_to_collection ⇒ Object
11
12
13
14
15
|
# File 'lib/volt/models/persistors/model_store.rb', line 11
def add_to_collection
@in_collection = true
ensure_setup
changed
end
|
#changed(attribute_name = nil) ⇒ Object
Called when the model changes
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
# File 'lib/volt/models/persistors/model_store.rb', line 51
def changed(attribute_name=nil)
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
|
#channel_name ⇒ Object
97
98
99
|
# File 'lib/volt/models/persistors/model_store.rb', line 97
def channel_name
@channel_name ||= "#{@model.path[-2]}##{@model.attributes[:_id]}"
end
|
Finds the model in its parent collection and deletes it.
102
103
104
105
106
107
108
|
# File 'lib/volt/models/persistors/model_store.rb', line 102
def delete!
if @model.path.size == 0
raise "Not in a collection"
end
@model.parent.delete(@model)
end
|
#ensure_setup ⇒ Object
Called the first time a value is assigned into this model
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
# File 'lib/volt/models/persistors/model_store.rb', line 23
def ensure_setup
if @model.attributes
@model.attributes[:_id] ||= generate_id
if !model_in_identity_map?
@@identity_map[@model.attributes[:_id]] ||= self
end
if @model.listeners[:changed]
listen_for_changes
end
end
end
|
#event_added(event, scope_provider, first) ⇒ Object
82
83
84
85
86
87
88
|
# File 'lib/volt/models/persistors/model_store.rb', line 82
def event_added(event, scope_provider, first)
if first && event == :changed
ensure_setup
listen_for_changes
end
end
|
#event_removed(event, no_more_events) ⇒ Object
90
91
92
93
94
95
|
# File 'lib/volt/models/persistors/model_store.rb', line 90
def event_removed(event, no_more_events)
if no_more_events && event == :changed
stop_listening_for_changes
end
end
|
#generate_id ⇒ Object
Create a random unique id that can be used as the mongo id as well
43
44
45
46
47
48
|
# File 'lib/volt/models/persistors/model_store.rb', line 43
def generate_id
id = []
12.times { id << ID_CHARS.sample }
return id.join
end
|
#listen_for_changes ⇒ Object
66
67
68
69
70
71
72
73
|
# File 'lib/volt/models/persistors/model_store.rb', line 66
def listen_for_changes
unless @change_listening
if @in_collection
@change_listening = true
change_channel_connection("add")
end
end
end
|
#model_in_identity_map? ⇒ Boolean
38
39
40
|
# File 'lib/volt/models/persistors/model_store.rb', line 38
def model_in_identity_map?
@@identity_map[@model.attributes[:_id]]
end
|
#remove_from_collection ⇒ Object
17
18
19
20
|
# File 'lib/volt/models/persistors/model_store.rb', line 17
def remove_from_collection
@in_collection = false
stop_listening_for_changes
end
|
#stop_listening_for_changes ⇒ Object
75
76
77
78
79
80
|
# File 'lib/volt/models/persistors/model_store.rb', line 75
def stop_listening_for_changes
if @change_listening
@change_listening = false
change_channel_connection("remove")
end
end
|