Module: MemModel::Base::ClassMethods

Includes:
Enumerable
Defined in:
lib/mem_model/base.rb

Instance Method Summary collapse

Instance Method Details

#abortObject



89
90
91
# File 'lib/mem_model/base.rb', line 89

def abort
  MemModel.abort
end

#allObject



50
51
52
# File 'lib/mem_model/base.rb', line 50

def all
  store.to_a
end

#commitObject



93
94
95
# File 'lib/mem_model/base.rb', line 93

def commit
  MemModel.commit
end

#committed?Boolean



104
# File 'lib/mem_model/base.rb', line 104

def committed?; false; end

#container_keyObject



20
21
22
# File 'lib/mem_model/base.rb', line 20

def container_key
  name.to_sym
end

#create(atts = {}) ⇒ Object

Create a new record. Example:

create(:name => "foo", :id => 1)


80
81
82
83
# File 'lib/mem_model/base.rb', line 80

def create(atts = {})
  rec = self.new(atts)
  rec.save && rec
end

#delete(record) ⇒ Object



99
100
101
# File 'lib/mem_model/base.rb', line 99

def delete(record)
  store.delete(record)
end

#delete_allObject



73
74
75
# File 'lib/mem_model/base.rb', line 73

def delete_all
  store.clear
end

#each(&block) ⇒ Object



34
35
36
# File 'lib/mem_model/base.rb', line 34

def each(&block)
  store.each(&block)
end

#exists?(id) ⇒ Boolean



69
70
71
# File 'lib/mem_model/base.rb', line 69

def exists?(id)
  store.any?{ |record| record.id == id }
end

#find(id) ⇒ Object Also known as: []

Find or nil



59
60
61
# File 'lib/mem_model/base.rb', line 59

def find(id)
  store.detect{ |r| r.id == id }
end

#find_by_id(id) ⇒ Object

Find or raise



65
66
67
# File 'lib/mem_model/base.rb', line 65

def find_by_id(id)
  find(id) || raise(UnknownRecord, "Couldn't find #{self.name} with ID=#{id}")
end

#lastObject



54
55
56
# File 'lib/mem_model/base.rb', line 54

def last
  all[-1]
end

#maglev?Boolean



85
86
87
# File 'lib/mem_model/base.rb', line 85

def maglev?
  MemModel.maglev?
end

#model_nameObject



38
39
40
# File 'lib/mem_model/base.rb', line 38

def model_name
  @model_name ||= ModelName.new(self.name)
end

#persistent(&block) ⇒ Object



107
108
109
110
111
# File 'lib/mem_model/base.rb', line 107

def persistent(&block)
  committed? ?
    MemModel.persistent(&block) :
    block.call
end

#rootObject



29
30
31
# File 'lib/mem_model/base.rb', line 29

def root
  @root ||= root_container::PERSISTENT_ROOT
end

#root_containerObject



16
17
18
# File 'lib/mem_model/base.rb', line 16

def root_container
  MemModel.maglev? ? Maglev : MemModel
end

#sizeObject



46
47
48
# File 'lib/mem_model/base.rb', line 46

def size
  store.size
end

#storeObject



24
25
26
27
# File 'lib/mem_model/base.rb', line 24

def store
  root[:MemModel] ||= {}
  root[:MemModel][container_key] ||= store_class.new
end

#store_classObject



42
43
44
# File 'lib/mem_model/base.rb', line 42

def store_class
  maglev? ? IdentitySet : Set
end