Class: Dao::Db
- Inherits:
-
Object
- Object
- Dao::Db
- Defined in:
- lib/dao/db.rb
Defined Under Namespace
Classes: Collection
Class Attribute Summary collapse
Instance Attribute Summary collapse
-
#path ⇒ Object
Returns the value of attribute path.
Class Method Summary collapse
Instance Method Summary collapse
- #[](name) ⇒ Object
- #data_for(data) ⇒ Object
- #db ⇒ Object
- #delete(collection, id = :all) ⇒ Object
- #find(collection, id = :all, &block) ⇒ Object
- #id_for(data) ⇒ Object
-
#initialize(*args) ⇒ Db
constructor
A new instance of Db.
- #next_id_for(collection, data) ⇒ Object
- #save(collection, data = {}) ⇒ Object (also: #create)
- #to_hash ⇒ Object
- #to_yaml(*args, &block) ⇒ Object
- #transaction(*args, &block) ⇒ Object
- #update(collection, id = :all, updates = {}) ⇒ Object
- #ystore ⇒ Object
Constructor Details
Class Attribute Details
.instance ⇒ Object
174 175 176 |
# File 'lib/dao/db.rb', line 174 def instance @instance ||= Db.new(Db.default_path) end |
.root ⇒ Object
178 179 180 |
# File 'lib/dao/db.rb', line 178 def root @root ||= default_root end |
Instance Attribute Details
#path ⇒ Object
Returns the value of attribute path.
7 8 9 |
# File 'lib/dao/db.rb', line 7 def path @path end |
Class Method Details
.default_path ⇒ Object
165 166 167 |
# File 'lib/dao/db.rb', line 165 def default_path() File.join(default_root, 'dao.yml') end |
.default_root ⇒ Object
161 162 163 |
# File 'lib/dao/db.rb', line 161 def default_root() defined?(Rails.root) ? File.join(Rails.root.to_s, 'db') : './db' end |
.method_missing(method, *args, &block) ⇒ Object
169 170 171 172 |
# File 'lib/dao/db.rb', line 169 def method_missing(method, *args, &block) super unless instance.respond_to?(method) instance.send(method, *args, &block) end |
Instance Method Details
#[](name) ⇒ Object
64 65 66 |
# File 'lib/dao/db.rb', line 64 def [](name) Collection.new(name, db) end |
#data_for(data) ⇒ Object
82 83 84 |
# File 'lib/dao/db.rb', line 82 def data_for(data) data ? Map.for(data) : nil end |
#db ⇒ Object
15 16 17 |
# File 'lib/dao/db.rb', line 15 def db self end |
#delete(collection, id = :all) ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/dao/db.rb', line 117 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
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/dao/db.rb', line 88 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
141 142 143 144 145 |
# File 'lib/dao/db.rb', line 141 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
129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/dao/db.rb', line 129 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 |
#save(collection, data = {}) ⇒ Object Also known as: create
72 73 74 75 76 77 78 79 80 |
# File 'lib/dao/db.rb', line 72 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] end end |
#to_hash ⇒ Object
147 148 149 150 151 |
# File 'lib/dao/db.rb', line 147 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
153 154 155 |
# File 'lib/dao/db.rb', line 153 def to_yaml(*args, &block) to_hash.to_yaml(*args, &block) end |
#transaction(*args, &block) ⇒ Object
68 69 70 |
# File 'lib/dao/db.rb', line 68 def transaction(*args, &block) ystore.transaction(*args, &block) end |
#update(collection, id = :all, updates = {}) ⇒ Object
110 111 112 113 114 115 |
# File 'lib/dao/db.rb', line 110 def update(collection, id = :all, updates = {}) data = data_for(data) find(collection, id) do |record| record.update(updates) end end |
#ystore ⇒ Object
19 20 21 |
# File 'lib/dao/db.rb', line 19 def ystore @ystore ||= YAML::Store.new(path) end |