Class: Ramaze::Store::Default

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/ramaze/store/default.rb

Overview

A simple wrapper around YAML::Store

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename = 'db.yaml') ⇒ Default

create a new store with a filename



21
22
23
24
# File 'lib/ramaze/store/default.rb', line 21

def initialize filename = 'db.yaml'
  FileUtils.touch(filename)
  @db = YAML::Store.new(filename)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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

pass on all methods inside a transaction



36
37
38
39
40
41
42
# File 'lib/ramaze/store/default.rb', line 36

def method_missing(meth, *args, &block)
  read_only = (meth == :[])

  @db.transaction(read_only) do
    @db.send(meth, *args, &block)
  end
end

Instance Attribute Details

#dbObject

Returns the value of attribute db.



17
18
19
# File 'lib/ramaze/store/default.rb', line 17

def db
  @db
end

Instance Method Details

#clearObject

delete all entries



88
89
90
91
92
# File 'lib/ramaze/store/default.rb', line 88

def clear
  keys.each do |key|
    delete key
  end
end

#eachObject

Iterate over #original and pass the key and value to a block.



102
103
104
105
106
# File 'lib/ramaze/store/default.rb', line 102

def each
  original.each do |key, value|
    yield key, value
  end
end

#empty?Boolean

is the Store empty? (no keys)

Returns:

  • (Boolean)


64
65
66
# File 'lib/ramaze/store/default.rb', line 64

def empty?
  keys.empty?
end

#keysObject

available keys of the store



58
59
60
# File 'lib/ramaze/store/default.rb', line 58

def keys
  (original || {}).keys
end

#merge(hash = {}) ⇒ Object

nondestructive merge on #original



70
71
72
# File 'lib/ramaze/store/default.rb', line 70

def merge hash = {}
  original.merge(hash)
end

#merge!(hash = {}) ⇒ Object

destructive #merge



76
77
78
79
80
81
82
83
84
# File 'lib/ramaze/store/default.rb', line 76

def merge! hash = {}
  hash.each do |key, value|
    transaction do
      @db[key] = value
    end
  end

  original
end

#originalObject

loads the #to_yaml



52
53
54
# File 'lib/ramaze/store/default.rb', line 52

def original
  YAML.load(to_yaml)
end

#sizeObject

number of #keys



96
97
98
# File 'lib/ramaze/store/default.rb', line 96

def size
  keys.size
end

#to_yamlObject

the actual content of the store in YAML format



46
47
48
# File 'lib/ramaze/store/default.rb', line 46

def to_yaml
  dump(:x)
end

#transactionObject

yield a block in a transaction, identical to #db.transaction{}



28
29
30
31
32
# File 'lib/ramaze/store/default.rb', line 28

def transaction
  @db.transaction do
    yield
  end
end