Class: Store

Inherits:
Model show all
Defined in:
lib/volt/models/store.rb

Constant Summary collapse

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

Instance Attribute Summary

Attributes inherited from Model

#attributes, #parent, #path

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Model

#!, #<<, #==, #assign_attribute, #delete, #expand!, #false?, #inspect, #nil?, #read_attribute, #return_undefined_method, #trigger_by_attribute!, #true?

Methods included from ObjectTracking

#__setup_tracking

Methods included from ModelWrapper

#wrap_value, #wrap_values

Methods included from ReactiveTags

included, #reactive_method_tag

Constructor Details

#initialize(tasks = nil, *args) ⇒ Store

Returns a new instance of Store.



8
9
10
11
12
13
14
15
16
# File 'lib/volt/models/store.rb', line 8

def initialize(tasks=nil, *args)
  @tasks = tasks

  super(*args)
  
  track_in_identity_map if attributes && attributes['_id']
  
  value_updated
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



65
66
67
68
69
70
71
72
73
74
# File 'lib/volt/models/store.rb', line 65

def method_missing(method_name, *args, &block)
  result = super
  
  if method_name[0] == '_' && method_name[-1] == '='
    # Trigger value updated after an assignment
    self.value_updated
  end
  
  return result
end

Class Method Details

.update(model_id, data) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/volt/models/store.rb', line 45

def self.update(model_id, data)
  model = @@identity_map[model_id]
  
  if model
    data.each_pair do |key, value|
      if key != '_id'
        puts "update #{key} with #{value.inspect}"
        model.send(:"#{key}=", value)
      end
    end
  end
end

Instance Method Details

#collection(path = nil) ⇒ Object



107
108
109
110
111
112
113
114
# File 'lib/volt/models/store.rb', line 107

def collection(path=nil)
  path ||= self.path
  
  collection_name = path.last
  collection_name = path[-2] if collection_name == :[]
  
  return collection_name
end

#ensure_idObject

When called, will setup an id if there is not one



81
82
83
84
85
86
87
# File 'lib/volt/models/store.rb', line 81

def ensure_id
  # No id yet, lets create one
  if attributes && !attributes['_id']
    self.attributes['_id'] = generate_id
    track_in_identity_map
  end
end

#event_added(event, scope_provider, first) ⇒ Object

def _id

return attributes && attributes['_id']

end



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/volt/models/store.rb', line 22

def event_added(event, scope_provider, first)
  if first && event == :changed
    # Start listening
    ensure_id
    if self.attributes && self.path.size > 1
      channel_name = "#{self.path[-2]}##{self.attributes['_id']}"
      puts "LISTENER ON: #{channel_name.inspect} -- #{self.path.inspect}"
      $page.tasks.call('ChannelTasks', 'add_listener', channel_name)
    end
  end
end

#event_removed(event, no_more_events) ⇒ Object



34
35
36
37
38
39
40
41
42
43
# File 'lib/volt/models/store.rb', line 34

def event_removed(event, no_more_events)
  if no_more_events && event == :changed
    # Stop listening
    if self.attributes && self.path.size > 1
      channel_name = "#{self.path[-2]}##{self.attributes['_id']}"
      puts "REMOVE LISTENER ON: #{channel_name}"
      $page.tasks.call('ChannelTasks', 'remove_listener', channel_name)
    end
  end
end

#generate_idObject



58
59
60
61
62
63
# File 'lib/volt/models/store.rb', line 58

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

#new_array_model(*args) ⇒ Object



161
162
163
# File 'lib/volt/models/store.rb', line 161

def new_array_model(*args)
  StoreArray.new(@tasks, *args)
end

#new_model(attributes = {}, parent = nil, path = nil, class_paths = nil) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/volt/models/store.rb', line 129

def new_model(attributes={}, parent=nil, path=nil, class_paths=nil)
  model = Store.new(@tasks, attributes, parent, path, class_paths)

  if @tasks && path.last[-1] == 's'
    # puts "FIND NEW MODEL: #{path.inspect} - #{attributes.inspect}"
    
    # Check to see the parents scope so we can only lookup associated
    # models.
    scope = {}
    
    if parent.attributes && parent.attributes['_id'].true?
      scope[path[-1].singularize + '_id'] = parent._id
    end
    
    puts "FIND: #{collection(path).inspect} at #{scope.inspect}"
    @tasks.call('StoreTasks', 'find', collection(path), scope) do |results|
      # TODO: Globals evil, replace
      $loading_models = true
      results.each do |result|
        # Get model again, we need to fetch it each time so it gets the
        # updated model when it switches from nil.
        # TODO: Strange that this is needed
        model = self.send(path.last)
        model << Store.new(@tasks, result, model, path + [:[]], class_paths)
      end
      $loading_models = false
    end
  end
  
  return model
end

#read_new_model(method_name) ⇒ Object

On stores, we store the model so we don’t have to look it up every time we do a read.



118
119
120
121
122
123
124
125
# File 'lib/volt/models/store.rb', line 118

def read_new_model(method_name)
  model = new_model(nil, self, path + [method_name])
  
  self.attributes ||= {}
  attributes[method_name] = model
  
  return model
end

#track_in_identity_mapObject



76
77
78
# File 'lib/volt/models/store.rb', line 76

def track_in_identity_map
  @@identity_map[attributes['_id']] = self
end

#value_updatedObject



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/volt/models/store.rb', line 89

def value_updated
  # puts "VU: #{@tasks.inspect} = #{path.inspect} - #{attributes.inspect}"
  if (!defined?($loading_models) || !$loading_models) && @tasks && path.size > 0 && !self.nil?
    
    ensure_id
    
    if path.size > 2 && parent && source = parent.parent
      self.attributes[path[-2].to_s.singularize+'_id'] = source._id
    end
    
    # Don't store any sub-stores, those will do their own saving.
    attrs = attributes.reject {|k,v| v.is_a?(Model) || v.is_a?(ArrayModel) }
    
    puts "Save: #{collection} - #{attrs.inspect}"
    @tasks.call('StoreTasks', 'save', collection, attrs)
  end
end