Class: Audrey
- Inherits:
-
Object
- Object
- Audrey
- Extended by:
- Forwardable
- Defined in:
- lib/audrey.rb,
lib/audrey.rb
Overview
build aclasses for collections This code needs to go at the bottom of this file because the classes being referenced aren’t defined until after the COLLECTIONS hash is used.
Defined Under Namespace
Modules: Util Classes: Engine, Exception, Mode, Node, Object, Query, Transaction
Constant Summary collapse
- VERSION =
version
'0.3.2'
- RUBY_OBJECT_TO_AUDREY =
RUBY_OBJECT_TO_AUDREY
{}
- AUDREY_SCALAR_TO_RUBY =
AUDREY_SCALAR_TO_RUBY
{}
- AUDREY_COLLECTION_TO_RUBY =
AUDREY_COLLECTION_TO_RUBY
{}
- @@current_db =
nil
Instance Attribute Summary collapse
-
#autopurge ⇒ Object
Returns the value of attribute autopurge.
-
#closers ⇒ Object
readonly
Returns the value of attribute closers.
-
#engine ⇒ Object
readonly
Returns the value of attribute engine.
-
#partition ⇒ Object
Returns the value of attribute partition.
Class Method Summary collapse
-
.connect(*opts) ⇒ Object
————————————————————————— connect.
- .current_db(opts = {}) ⇒ Object
- .current_db=(db) ⇒ Object
- .explicit_or_current_db(accessor) ⇒ Object
-
.finalize(id) ⇒ Object
————————————————————————— finalize.
Instance Method Summary collapse
-
#autocommit(opts = {}, &block) ⇒ Object
————————————————————————— autocommit.
-
#build_array(row) ⇒ Object
————————————————————————— build_array.
-
#changed_objects ⇒ Object
————————————————————————— changed_objects.
-
#checks_outside_transaction(aclass) ⇒ Object
————————————————————————— checks_outside_transaction.
-
#close ⇒ Object
————————————————————————— close.
-
#connect_engine ⇒ Object
————————————————————————— connect_engine.
-
#current ⇒ Object
————————————————————————— current.
-
#ensure_object_record(object) ⇒ Object
————————————————————————— ensure_object_record.
-
#exit ⇒ Object
————————————————————————— exit.
-
#import_csv(aclass, path, opts = {}) ⇒ Object
————————————————————————— import_csv.
-
#initialize(*params) ⇒ Audrey
constructor
————————————————————————— initialize.
-
#insert_object(object) ⇒ Object
————————————————————————— insert_object.
-
#object_from_pk(pk) ⇒ Object
————————————————————————— object_from_pk.
-
#object_from_row(row) ⇒ Object
————————————————————————— object_from_row.
-
#purge ⇒ Object
————————————————————————— purge.
-
#q0 ⇒ Object
————————————————————————— q0.
-
#read_check ⇒ Object
————————————————————————— read_check, write_check.
-
#reset ⇒ Object
————————————————————————— reset.
-
#run_checks ⇒ Object
————————————————————————— run_checks.
-
#session_pk ⇒ Object
————————————————————————— session_pk.
-
#transaction(opts = {}) ⇒ Object
————————————————————————— transaction.
- #write_check ⇒ Object
Constructor Details
#initialize(*params) ⇒ Audrey
initialize
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/audrey.rb', line 43 def initialize(*params) @org_opts = self.class.params_to_opts(*params) # mode # KLUDGE: For now, just always using the mode that was sent with the # "new" command. Eventually we'll need to recognize that different # partitions will have different modes @mode = Audrey::Mode.new(@org_opts['mode']) # finalizer ObjectSpace.define_finalizer(self, proc { self.class.finalize(self.object_id) }) # engine opts @engine_opts = {} # immediate_commit if @org_opts.has_key?('immediate_commit') @engine_opts['immediate_commit'] = @org_opts['immediate_commit'] end # default autopurge @autopurge = true # connect engine connect_engine() end |
Instance Attribute Details
#autopurge ⇒ Object
Returns the value of attribute autopurge.
12 13 14 |
# File 'lib/audrey.rb', line 12 def autopurge @autopurge end |
#closers ⇒ Object (readonly)
Returns the value of attribute closers.
14 15 16 |
# File 'lib/audrey.rb', line 14 def closers @closers end |
#engine ⇒ Object (readonly)
Returns the value of attribute engine.
11 12 13 |
# File 'lib/audrey.rb', line 11 def engine @engine end |
#partition ⇒ Object
Returns the value of attribute partition.
13 14 15 |
# File 'lib/audrey.rb', line 13 def partition @partition end |
Class Method Details
.connect(*opts) ⇒ Object
connect
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/audrey.rb', line 131 def self.connect(*opts) if block_given? begin db = self.new(*opts) db.current do yield db end # implement db.exit by catching Audrey::Exception::DatabaseExit rescue Audrey::Exception::DatabaseExit => e # NOTE: for whatever reason, e.db == db doesn't return true, # even though they're the same object. Using object_id to # bypass that problem. unless e.db.object_id == db.object_id raise e end # return out of this method return nil # ensure database is closed ensure db and db.close end else return self.new(*opts) end end |
.current_db(opts = {}) ⇒ Object
188 189 190 |
# File 'lib/audrey.rb', line 188 def self.current_db(opts={}) return @@current_db end |
.current_db=(db) ⇒ Object
205 206 207 |
# File 'lib/audrey.rb', line 205 def self.current_db=(db) @@current_db = db end |
.explicit_or_current_db(accessor) ⇒ Object
192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/audrey.rb', line 192 def self.explicit_or_current_db(accessor) # $tm.hrm # explicit accessor was sent if accessor return accessor # else return current database else return @@current_db end end |
.finalize(id) ⇒ Object
finalize
231 232 233 234 235 |
# File 'lib/audrey.rb', line 231 def self.finalize(id) # $tm.hrm obj = ObjectSpace._id2ref(id) obj.close() end |
Instance Method Details
#autocommit(opts = {}, &block) ⇒ Object
autocommit
466 467 468 469 470 |
# File 'lib/audrey.rb', line 466 def autocommit(opts={}, &block) # $tm.hrm opts = {'autocommit'=>true}.merge(opts) transaction opts, &block end |
#build_array(row) ⇒ Object
build_array
264 265 266 267 268 269 |
# File 'lib/audrey.rb', line 264 def build_array(row) node = Audrey::Node::Array.new(self, 'pk'=>row['pk']) rv = [] node.attach_to rv return rv end |
#changed_objects ⇒ Object
changed_objects
523 524 525 526 527 |
# File 'lib/audrey.rb', line 523 def changed_objects() @engine.changed_objects do |row| yield self.object_from_row(row) end end |
#checks_outside_transaction(aclass) ⇒ Object
checks_outside_transaction
423 424 425 426 427 428 429 |
# File 'lib/audrey.rb', line 423 def checks_outside_transaction(aclass) if aclass.respond_to?('has_checks?') if (not in_transaction?) and aclass.has_checks? raise 'cannot-create-or-modify-object-that-has-checks-when-not-in-a-transaction' end end end |
#close ⇒ Object
close
244 245 246 247 248 249 250 251 252 253 254 255 |
# File 'lib/audrey.rb', line 244 def close # closers @closers.keys.each do |k| @closers[k].close @closers.delete(k) end # purge if @engine @engine.close 'purge'=>@autopurge end end |
#connect_engine ⇒ Object
connect_engine
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/audrey.rb', line 77 def connect_engine # objects that should be closed before the database is closed @closers = {} # sqlite3 if @org_opts['engine'] == 'sqlite3' require 'audrey/engine/sqlite3' @engine = Audrey::Engine::SQLite3.new(@org_opts['connect'], @mode, @engine_opts) else # @engine = self.class.initialize_engine(opts) raise 'not-yet-implemented-non-sqlite-engine' end # default partition to m self.partition = 'm' end |
#current ⇒ Object
current
180 181 182 183 184 185 186 |
# File 'lib/audrey.rb', line 180 def current hold_current = Audrey.current_db Audrey.current_db = self yield ensure Audrey.current_db = hold_current end |
#ensure_object_record(object) ⇒ Object
ensure_object_record
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 |
# File 'lib/audrey.rb', line 306 def ensure_object_record(object) # $tm.hrm write_check() # early exit: object already has a audrey object if object.respond_to?('audrey') return object.audrey.pk end # scalar if dfn = Audrey::RUBY_OBJECT_TO_AUDREY[object.class] if dfn['nclass'] pk = @engine.insert_object(object) node = dfn['nclass'].new(self, 'pk'=>pk) node.attach_to object return pk else return @engine.insert_object(object) end end end |
#exit ⇒ Object
exit
168 169 170 171 |
# File 'lib/audrey.rb', line 168 def exit() e = Audrey::Exception::DatabaseExit.new(self) raise e end |
#import_csv(aclass, path, opts = {}) ⇒ Object
import_csv
411 412 413 414 |
# File 'lib/audrey.rb', line 411 def import_csv(aclass, path, opts={}) # $tm.hrm return @engine.import_csv(aclass, path, opts) end |
#insert_object(object) ⇒ Object
insert_object
369 370 371 372 373 |
# File 'lib/audrey.rb', line 369 def insert_object(object) write_check() checks_outside_transaction object.class return @engine.insert_object(object) end |
#object_from_pk(pk) ⇒ Object
object_from_pk
382 383 384 385 386 387 388 389 390 |
# File 'lib/audrey.rb', line 382 def object_from_pk(pk) row = @engine.row_by_pk(pk) if row return object_from_row(row) else return nil end end |
#object_from_row(row) ⇒ Object
object_from_row
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 |
# File 'lib/audrey.rb', line 335 def object_from_row(row) # $tm.hrm # scalar if dfn = Audrey::AUDREY_SCALAR_TO_RUBY[row['aclass']] return dfn.call(row['scalar']) end # collection if dfn = Audrey::AUDREY_COLLECTION_TO_RUBY[row['aclass']] return dfn['nclass'].create_object(self, 'row'=>row) end # custom class if Module.const_defined?(row['aclass']) clss = Module.const_get(row['aclass']) nclass = Audrey::AUDREY_COLLECTION_TO_RUBY[clss.hsa]['nclass'] node = nclass.new(self, 'row'=>row) rv = clss.new('db'=>self, 'node'=>node) return rv end # if we get this far then we don't know how to turn the row into an # Audrey object raise 'unknown-aclass: ' + row['aclass'].to_s end |
#purge ⇒ Object
purge
278 279 280 281 |
# File 'lib/audrey.rb', line 278 def purge # $tm.hrm @engine.purge end |
#q0 ⇒ Object
q0
399 400 401 402 |
# File 'lib/audrey.rb', line 399 def q0 require 'audrey/query/q0' return Audrey::Query::Q0.new(self) end |
#read_check ⇒ Object
read_check, write_check
290 291 292 293 |
# File 'lib/audrey.rb', line 290 def read_check # $tm.hrm @mode.read or raise Audrey::Exception::Permission::Read.new() end |
#reset ⇒ Object
reset
101 102 103 104 |
# File 'lib/audrey.rb', line 101 def reset close() connect_engine() end |
#run_checks ⇒ Object
run_checks
479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 |
# File 'lib/audrey.rb', line 479 def run_checks() # $tm.hrm xeme = Xeme.new() # loop through records that have been changed changed_objects() do |obj| if obj.class.has_checks? obj.class.fields.each do |fieldname, dfn| val = obj.audrey[fieldname] # require if dfn.required and val.nil? xeme.error('required') do |msg| msg['field'] = fieldname end # wrong class elsif dfn.aclass and (not val.is_a?(dfn.aclass)) xeme.error('wrong-aclass') do |msg| msg['field'] = fieldname msg['aclass'] = dfn.aclass.to_s end # run checks else dfn.base_checks xeme, fieldname, val end end else obj.audrey.changed = false end end # return return xeme end |
#session_pk ⇒ Object
session_pk
216 217 218 219 220 221 222 |
# File 'lib/audrey.rb', line 216 def session_pk if not instance_variable_defined?(:@session_pk) @session_pk = Audrey::Util.uuid end return @session_pk end |
#transaction(opts = {}) ⇒ Object
transaction
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 |
# File 'lib/audrey.rb', line 438 def transaction(opts={}) # $tm.hrm # yield to block and return nil if block_given? @engine.transaction(opts) do |tr| # yield yield tr # autocommit if opts['autocommit'] tr.commit end end # else just return transaction else raise 'not-yet-implemented-non-block-transaction' end end |
#write_check ⇒ Object
295 296 297 |
# File 'lib/audrey.rb', line 295 def write_check @mode.write or raise Audrey::Exception::Permission::Write.new() end |