Class: Dao::Db
- Inherits:
-
Object
show all
- Defined in:
- lib/dao/db.rb
Defined Under Namespace
Classes: Collection
Class Attribute Summary collapse
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
-
#collection(name) ⇒ Object
(also: #[])
-
#data_for(data) ⇒ Object
-
#db ⇒ Object
-
#delete(collection, id = :all) ⇒ Object
(also: #destroy)
-
#find(collection, id = :all, &block) ⇒ Object
-
#id_for(data) ⇒ Object
-
#initialize(*args) ⇒ Db
constructor
-
#method_missing(method, *args, &block) ⇒ Object
-
#next_id_for(collection, data) ⇒ Object
-
#rm_f ⇒ Object
-
#rm_rf ⇒ Object
-
#save(collection, data) ⇒ Object
(also: #create)
-
#to_hash ⇒ Object
-
#to_yaml(*args, &block) ⇒ Object
-
#transaction(*args, &block) ⇒ Object
-
#truncate ⇒ Object
-
#update(collection, id = :all, updates = {}) ⇒ Object
-
#ystore ⇒ Object
Constructor Details
#initialize(*args) ⇒ Db
Returns a new instance of Db.
11
12
13
14
15
|
# File 'lib/dao/db.rb', line 11
def initialize(*args)
options = args..to_options!
@path = ( args.shift || options[:path] || Db.default_path ).to_s
FileUtils.mkdir_p(File.dirname(@path)) rescue nil
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
92
93
94
95
96
97
|
# File 'lib/dao/db.rb', line 92
def method_missing(method, *args, &block)
if args.empty? and block.nil?
return self.collection(method)
end
super
end
|
Class Attribute Details
.instance ⇒ Object
206
207
208
|
# File 'lib/dao/db.rb', line 206
def instance
@instance ||= Db.new(Db.default_path)
end
|
.root ⇒ Object
210
211
212
|
# File 'lib/dao/db.rb', line 210
def root
@root ||= default_root
end
|
Instance Attribute Details
#path ⇒ Object
Returns the value of attribute path.
9
10
11
|
# File 'lib/dao/db.rb', line 9
def path
@path
end
|
Class Method Details
.default_path ⇒ Object
197
198
199
|
# File 'lib/dao/db.rb', line 197
def default_path()
File.join(default_root, 'db.yml')
end
|
.default_root ⇒ Object
193
194
195
|
# File 'lib/dao/db.rb', line 193
def default_root()
defined?(Rails.root) && Rails.root ? File.join(Rails.root.to_s, 'db') : './db'
end
|
.method_missing(method, *args, &block) ⇒ Object
201
202
203
204
|
# File 'lib/dao/db.rb', line 201
def method_missing(method, *args, &block)
super unless instance.respond_to?(method)
instance.send(method, *args, &block)
end
|
.tmp(&block) ⇒ Object
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
|
# File 'lib/dao/db.rb', line 214
def tmp(&block)
require 'tempfile' unless defined?(Tempfile)
tempfile = Tempfile.new("#{ Process.pid }-#{ Process.ppid }-#{ Time.now.to_f }-#{ rand }")
path = tempfile.path
db = new(:path => path)
if block
begin
block.call(db)
ensure
db.rm_rf
end
else
db
end
end
|
Instance Method Details
#collection(name) ⇒ Object
Also known as:
[]
87
88
89
|
# File 'lib/dao/db.rb', line 87
def collection(name)
Collection.new(name, db)
end
|
#data_for(data) ⇒ Object
113
114
115
|
# File 'lib/dao/db.rb', line 113
def data_for(data)
data ? Map.for(data) : nil
end
|
#db ⇒ Object
29
30
31
|
# File 'lib/dao/db.rb', line 29
def db
self
end
|
#delete(collection, id = :all) ⇒ Object
Also known as:
destroy
148
149
150
151
152
153
154
155
156
157
158
|
# File 'lib/dao/db.rb', line 148
def delete(collection, id = :all)
ystore.transaction do |y|
collection = (y[collection.to_s] ||= {})
if id.nil? or id == :all
collection.clear()
else
deleted = collection.delete(String(id))
data_for(deleted) if deleted
end
end
end
|
#find(collection, id = :all, &block) ⇒ Object
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
# File 'lib/dao/db.rb', line 119
def find(collection, id = :all, &block)
ystore.transaction do |y|
collection = (y[collection.to_s] ||= {})
if id.nil? or id == :all
list = collection.values.map{|data| data_for(data)}
if block
collection[:all] = list.map{|record| data_for(block.call(record))}
else
list
end
else
key = String(id)
record = data_for(collection[key])
if block
collection[key] = data_for(block.call(record))
else
record
end
end
end
end
|
#id_for(data) ⇒ Object
173
174
175
176
177
|
# File 'lib/dao/db.rb', line 173
def id_for(data)
data = data_for(data)
%w( id _id ).each{|key| return String(data[key]) if data.has_key?(key)}
raise("no id discoverable for #{ data.inspect }")
end
|
#next_id_for(collection, data) ⇒ Object
161
162
163
164
165
166
167
168
169
170
171
|
# File 'lib/dao/db.rb', line 161
def next_id_for(collection, data)
data = data_for(data)
begin
id = id_for(data)
raise if id.strip.empty?
id
rescue
data['id'] = String(collection.size + 1)
id_for(data)
end
end
|
#rm_f ⇒ Object
17
18
19
|
# File 'lib/dao/db.rb', line 17
def rm_f
FileUtils.rm_f(@path) rescue nil
end
|
#rm_rf ⇒ Object
21
22
23
|
# File 'lib/dao/db.rb', line 21
def rm_rf
FileUtils.rm_rf(@path) rescue nil
end
|
#save(collection, data) ⇒ Object
Also known as:
create
103
104
105
106
107
108
109
110
111
|
# File 'lib/dao/db.rb', line 103
def save(collection, data)
data = data_for(data)
ystore.transaction do |y|
collection = (y[collection.to_s] ||= {})
id = next_id_for(collection, data)
collection[id] = data
id
end
end
|
#to_hash ⇒ Object
179
180
181
182
183
|
# File 'lib/dao/db.rb', line 179
def to_hash
ystore.transaction do |y|
y.roots.inject(Hash.new){|h,k| h.update(k => y[k])}
end
end
|
#to_yaml(*args, &block) ⇒ Object
185
186
187
|
# File 'lib/dao/db.rb', line 185
def to_yaml(*args, &block)
to_hash.to_yaml(*args, &block)
end
|
#transaction(*args, &block) ⇒ Object
99
100
101
|
# File 'lib/dao/db.rb', line 99
def transaction(*args, &block)
ystore.transaction(*args, &block)
end
|
#truncate ⇒ Object
25
26
27
|
# File 'lib/dao/db.rb', line 25
def truncate
rm_f
end
|
#update(collection, id = :all, updates = {}) ⇒ Object
141
142
143
144
145
146
|
# File 'lib/dao/db.rb', line 141
def update(collection, id = :all, updates = {})
data = data_for(data)
find(collection, id) do |record|
record.update(updates)
end
end
|
#ystore ⇒ Object
33
34
35
|
# File 'lib/dao/db.rb', line 33
def ystore
@ystore ||= YAML::Store.new(path)
end
|