Class: ToughGuy::MappedModel

Inherits:
Model
  • Object
show all
Defined in:
lib/toughguy/mapped_model.rb

Instance Attribute Summary

Attributes inherited from Model

#collection, #values

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Model

#==, #[], #[]=, add_index, #after_create, #after_destroy, #after_load, #before_create, #before_destroy, #bulk, bulk, collection, collection_name, connection, connection=, count, #database, database, database=, #db, db_class_name, db_key, delete, #destroy, #destroyed?, drop, each, ensure_index, first, #id, #initialize, #inspect, limit, map_keys, mapped_db_classes, #method_missing, #model, #new?, paginate, recreate_indexes, #refresh, remove_class_with_db, #save, set_collection, set_collection_name, sort, stats, subclass_with_db, #to_s, to_table_name, #update, update, with_db

Constructor Details

This class inherits a constructor from ToughGuy::Model

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class ToughGuy::Model

Class Method Details

.[](k) ⇒ Object Also known as: find



86
87
88
# File 'lib/toughguy/mapped_model.rb', line 86

def [](k)
  @map[k] || first(@map_key => k)
end

.all(load = false) ⇒ Object



66
67
68
69
# File 'lib/toughguy/mapped_model.rb', line 66

def all(load=false)
  load_map if load
  @map.values
end

.create(values = {}) ⇒ Object



106
107
108
# File 'lib/toughguy/mapped_model.rb', line 106

def create(values = {})
  super.tap {|o| set_map_ref(o)}
end

.delete_allObject



92
93
94
95
# File 'lib/toughguy/mapped_model.rb', line 92

def delete_all
  super
  reset_map
end

.destroy_allObject



97
98
99
100
# File 'lib/toughguy/mapped_model.rb', line 97

def destroy_all
  instances.each {|o| o.destroy}
  reset_map
end

.enumerateObject



43
44
45
# File 'lib/toughguy/mapped_model.rb', line 43

def enumerate
  @map.each
end

.inherited(m) ⇒ Object



29
30
31
32
33
# File 'lib/toughguy/mapped_model.rb', line 29

def inherited(m)
  super
  # the map key is inherited, the map itself is not.
  m.set_map_key(@map_key)
end

.instancesObject



39
40
41
# File 'lib/toughguy/mapped_model.rb', line 39

def instances
  @map.values
end

.load(values) ⇒ Object



102
103
104
# File 'lib/toughguy/mapped_model.rb', line 102

def load(values)
  new(values).tap {|o| set_map_ref(o); o.after_load}
end

.load_map(opts = {}) ⇒ Object



47
48
49
# File 'lib/toughguy/mapped_model.rb', line 47

def load_map(opts = {})
  query({}).sort(@map_key).each {}
end

.load_map_async(&block) ⇒ Object



51
52
53
54
# File 'lib/toughguy/mapped_model.rb', line 51

def load_map_async(&block)
  @async_start = Time.now
  EM.next_tick {load_next_map_chunk(1, &block)}
end

.load_next_map_chunk(page, &block) ⇒ Object



56
57
58
59
60
61
62
63
64
# File 'lib/toughguy/mapped_model.rb', line 56

def load_next_map_chunk(page, &block)
  query.paginate(page: page, per_page: 100).each {}.tap do |q|
    if page < q.total_pages
      EM.timeout(0.1) {load_next_map_chunk(page + 1, &block)}
    else
      block.call if block
    end
  end
end

.mapObject



35
36
37
# File 'lib/toughguy/mapped_model.rb', line 35

def map
  @map
end

.map_keyObject



20
21
22
# File 'lib/toughguy/mapped_model.rb', line 20

def map_key
  @map_key
end

.query(opts = {}) ⇒ Object Also known as: q, where, filter



110
111
112
# File 'lib/toughguy/mapped_model.rb', line 110

def query(opts = {})
  MappedQuery.new(self).where(opts)
end

.remove_map_ref(o) ⇒ Object



75
76
77
# File 'lib/toughguy/mapped_model.rb', line 75

def remove_map_ref(o)
  @map.delete(o[@map_key])
end

.reset_mapObject



71
72
73
# File 'lib/toughguy/mapped_model.rb', line 71

def reset_map
  @map = {}
end

.set_map_key(k) ⇒ Object



24
25
26
27
# File 'lib/toughguy/mapped_model.rb', line 24

def set_map_key(k)
  @map = {}
  @map_key = k
end

.set_map_ref(o) ⇒ Object



79
80
81
82
83
84
# File 'lib/toughguy/mapped_model.rb', line 79

def set_map_ref(o)
  if @map[o[@map_key]]
    raise "Map reference already set for #{self}[#{o[@map_key].inspect}]"
  end
  @map[o[@map_key]] = o
end

Instance Method Details

#deleteObject



5
6
7
8
# File 'lib/toughguy/mapped_model.rb', line 5

def delete
  model.delete(_id: id)
  model.remove_map_ref(self)
end

#exists?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/toughguy/mapped_model.rb', line 15

def exists?
  model.q.filter(_id: id).count == 1
end

#set(hash) ⇒ Object



10
11
12
13
# File 'lib/toughguy/mapped_model.rb', line 10

def set(hash)
  model.update({_id: id}, '$set' => hash)
  @values.merge!(hash.symbolize_keys)
end