Class: Ramaze::Store::Default
- Includes:
- Enumerable
- Defined in:
- lib/ramaze/store/default.rb
Overview
A simple wrapper around YAML::Store
Instance Attribute Summary collapse
-
#db ⇒ Object
Returns the value of attribute db.
Instance Method Summary collapse
-
#clear ⇒ Object
delete all entries.
-
#each ⇒ Object
Iterate over #original and pass the key and value to a block.
-
#empty? ⇒ Boolean
is the Store empty? (no keys).
-
#initialize(filename = 'db.yaml') ⇒ Default
constructor
create a new store with a filename.
-
#keys ⇒ Object
available keys of the store.
-
#merge(hash = {}) ⇒ Object
nondestructive merge on #original.
-
#merge!(hash = {}) ⇒ Object
destructive #merge.
-
#method_missing(meth, *args, &block) ⇒ Object
pass on all methods inside a transaction.
-
#original ⇒ Object
loads the #to_yaml.
-
#size ⇒ Object
number of #keys.
-
#to_yaml ⇒ Object
the actual content of the store in YAML format.
-
#transaction ⇒ Object
yield a block in a transaction, identical to #db.transaction{}.
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
#db ⇒ Object
Returns the value of attribute db.
17 18 19 |
# File 'lib/ramaze/store/default.rb', line 17 def db @db end |
Instance Method Details
#clear ⇒ Object
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 |
#each ⇒ Object
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)
64 65 66 |
# File 'lib/ramaze/store/default.rb', line 64 def empty? keys.empty? end |
#keys ⇒ Object
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 |
#original ⇒ Object
loads the #to_yaml
52 53 54 |
# File 'lib/ramaze/store/default.rb', line 52 def original YAML.load(to_yaml) end |
#size ⇒ Object
number of #keys
96 97 98 |
# File 'lib/ramaze/store/default.rb', line 96 def size keys.size end |
#to_yaml ⇒ Object
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 |
#transaction ⇒ Object
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 |