Class: Newman::Recorder
- Inherits:
-
Object
- Object
- Newman::Recorder
- Includes:
- Enumerable
- Defined in:
- lib/newman/recorder.rb
Instance Method Summary collapse
-
#create(contents) ⇒ Object
Newman::Recorder#createstore an arbitrary Ruby object in the data store and returns aNewman::Recordobject which has fields for thecolumnkey, recordid, and recordcontents. -
#destroy(id) ⇒ Object
Newman::Recorder#destroylooks up a record byidand then removes it from the data store. -
#each ⇒ Object
Newman::Recorder#eachiterates over all records stored in the column, yielding aNewman::Recordobject for each one. -
#initialize(column, store) ⇒ Recorder
constructor
To initialize a
Newman::Recorderobject, acolumnkey andstoreobject must be provided, i.e. -
#read(id) ⇒ Object
Newman::Recorder#readlooks up a record byidand returns aNewman::Recordobject. -
#update(id) ⇒ Object
Newman::Recorder#updatelooks up a record byidand yields its contents.
Constructor Details
#initialize(column, store) ⇒ Recorder
To initialize a Newman::Recorder object, a column key
and store object must be provided, i.e.
store = Newman::Store.new("sample.store")
recorder = Newman::Recorder.new(:subscribers, store)
However, in most cases you should not instantiate a
Newman::Recorder directly, and instead should make use of
Newman::Store#[] which is syntactic sugar for the same operation.
The first time a particular column key is referenced, two mapping
is created for the column in the underlying data store: one which
keeps track of the autoincrementing ids, and one that keeps track
of the data stored within the column. It's fine to treat these
mappings as implementation details, but we treat them as part of Newman's
external interface because backwards-incompatible changes to them will
result in possible data store corruption.
37 38 39 40 41 42 43 44 45 |
# File 'lib/newman/recorder.rb', line 37 def initialize(column, store) self.column = column self.store = store store.write do |data| data[:identifiers][column] ||= 0 data[:columns][column] ||= {} end end |
Instance Method Details
#create(contents) ⇒ Object
Newman::Recorder#create store an arbitrary Ruby object in the data
store and returns a Newman::Record object which has fields for the
column key, record id, and record contents. This method
automatically generates new ids, starting with id=1 for the
first record and then incrementing sequentially.
69 70 71 72 73 74 75 76 77 |
# File 'lib/newman/recorder.rb', line 69 def create(contents) store.write do |data| id = (data[:identifiers][column] += 1) data[:columns][column][id] = contents Record.new(column, id, contents) end end |
#destroy(id) ⇒ Object
Newman::Recorder#destroy looks up a record by id and then removes it
from the data store. This method returns true whether or not a record
was actually destroyed, which is a somewhat useless behavior and may
need to be fixed in a future version of Newman. Patches welcome!
111 112 113 114 115 116 117 |
# File 'lib/newman/recorder.rb', line 111 def destroy(id) store.write do |data| data[:columns][column].delete(id) end true end |
#each ⇒ Object
Newman::Recorder#each iterates over all records stored in the column,
yielding a Newman::Record object for each one. Because Enumerable is
mixed into Newman::Recorder, all enumerable methods that get called on a
recorder object end up making calls to this method.
53 54 55 56 57 58 59 |
# File 'lib/newman/recorder.rb', line 53 def each store.read do |data| data[:columns][column].each do |id, contents| yield(Record.new(column, id, contents)) end end end |
#read(id) ⇒ Object
Newman::Recorder#read looks up a record by id and returns a
Newman::Record object.
84 85 86 87 88 |
# File 'lib/newman/recorder.rb', line 84 def read(id) store.read do |data| Record.new(column, id, data[:columns][column][id]) end end |
#update(id) ⇒ Object
Newman::Recorder#update looks up a record by id and yields its
contents. The record contents are then replaced with the
return value of the provided block.
96 97 98 99 100 101 102 |
# File 'lib/newman/recorder.rb', line 96 def update(id) store.write do |data| data[:columns][column][id] = yield(data[:columns][column][id]) Record.new(column, id, data[:columns][column][id]) end end |