Class: YDB

Inherits:
Object
  • Object
show all
Defined in:
lib/ydb.rb

Defined Under Namespace

Classes: Collection

Constant Summary collapse

Version =
'0.0.1'

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ YDB

Returns a new instance of YDB.



105
106
107
108
109
# File 'lib/ydb.rb', line 105

def initialize(*args)
  options = Map.options_for!(args)
  @path = ( args.shift || options[:path] || YDB.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



192
193
194
195
196
197
# File 'lib/ydb.rb', line 192

def method_missing(method, *args, &block)
  if args.empty? and block.nil?
    return self.collection(method)
  end
  super
end

Class Attribute Details

.instanceObject



307
308
309
# File 'lib/ydb.rb', line 307

def instance
  @instance ||= YDB.new(YDB.default_path)
end

.rootObject



311
312
313
# File 'lib/ydb.rb', line 311

def root
  @root ||= default_root
end

Instance Attribute Details

#pathObject

Returns the value of attribute path.



103
104
105
# File 'lib/ydb.rb', line 103

def path
  @path
end

Class Method Details

.default_pathObject



298
299
300
# File 'lib/ydb.rb', line 298

def default_path()
  File.join(default_root, 'ydb.yml')
end

.default_rootObject



294
295
296
# File 'lib/ydb.rb', line 294

def default_root()
  defined?(Rails.root) && Rails.root ? File.join(Rails.root.to_s, 'db') : '.'
end

.dependenciesObject



63
64
65
66
67
# File 'lib/ydb.rb', line 63

def YDB.dependencies
  {
    'map'         =>  [ 'map'         , '~> 4.4.0' ],
  }
end

.libdir(*args, &block) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/ydb.rb', line 69

def YDB.libdir(*args, &block)
  @libdir ||= File.expand_path(__FILE__).sub(/\.rb$/,'')
  args.empty? ? @libdir : File.join(@libdir, *args)
ensure
  if block
    begin
      $LOAD_PATH.unshift(@libdir)
      block.call()
    ensure
      $LOAD_PATH.shift()
    end
  end
end

.load(*libs) ⇒ Object



83
84
85
86
# File 'lib/ydb.rb', line 83

def YDB.load(*libs)
  libs = libs.join(' ').scan(/[^\s+]+/)
  YDB.libdir{ libs.each{|lib| Kernel.load(lib) } }
end

.method_missing(method, *args, &block) ⇒ Object



302
303
304
305
# File 'lib/ydb.rb', line 302

def method_missing(method, *args, &block)
  super unless instance.respond_to?(method)
  instance.send(method, *args, &block)
end

.tmp(&block) ⇒ Object



315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
# File 'lib/ydb.rb', line 315

def tmp(&block)
  require 'tempfile' unless defined?(Tempfile)
  tempfile = Tempfile.new("#{ Process.pid }-#{ Process.ppid }-#{ Time.now.to_f }-#{ rand }")
  path = tempfile.path
  ydb = new(:path => path)
  if block
    begin
      block.call(ydb)
    ensure
      ydb.rm_rf
    end
  else
    ydb
  end
end

.versionObject



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

def YDB.version
  YDB::Version
end

Instance Method Details

#collection(name) ⇒ Object Also known as: []



187
188
189
# File 'lib/ydb.rb', line 187

def collection(name)
  Collection.new(name, ydb)
end

#data_for(data) ⇒ Object



214
215
216
# File 'lib/ydb.rb', line 214

def data_for(data)
  data ? Map.for(data) : nil
end

#delete(collection, id = :all) ⇒ Object Also known as: destroy



249
250
251
252
253
254
255
256
257
258
259
# File 'lib/ydb.rb', line 249

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



220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/ydb.rb', line 220

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



274
275
276
277
278
# File 'lib/ydb.rb', line 274

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



262
263
264
265
266
267
268
269
270
271
272
# File 'lib/ydb.rb', line 262

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_fObject



111
112
113
# File 'lib/ydb.rb', line 111

def rm_f
  FileUtils.rm_f(@path) rescue nil
end

#rm_rfObject



115
116
117
# File 'lib/ydb.rb', line 115

def rm_rf
  FileUtils.rm_rf(@path) rescue nil
end

#save(collection, data) ⇒ Object Also known as: create



203
204
205
206
207
208
209
210
211
212
# File 'lib/ydb.rb', line 203

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
    record = collection[id]
    id
  end
end

#to_hashObject



280
281
282
283
284
# File 'lib/ydb.rb', line 280

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



286
287
288
# File 'lib/ydb.rb', line 286

def to_yaml(*args, &block)
  to_hash.to_yaml(*args, &block)
end

#transaction(*args, &block) ⇒ Object



199
200
201
# File 'lib/ydb.rb', line 199

def transaction(*args, &block)
  ystore.transaction(*args, &block)
end

#truncateObject



119
120
121
# File 'lib/ydb.rb', line 119

def truncate
  rm_f
end

#update(collection, id = :all, updates = {}) ⇒ Object



242
243
244
245
246
247
# File 'lib/ydb.rb', line 242

def update(collection, id = :all, updates = {})
  data = data_for(data)
  find(collection, id) do |record|
    record.update(updates)
  end
end

#ydbObject



123
124
125
# File 'lib/ydb.rb', line 123

def ydb
  self
end

#ystoreObject



127
128
129
# File 'lib/ydb.rb', line 127

def ystore
  @ystore ||= YAML::Store.new(path)
end