Class: Depository::Collection
- Inherits:
-
Object
- Object
- Depository::Collection
- Extended by:
- Forwardable
- Defined in:
- lib/depository/collection.rb
Class Method Summary collapse
- ._model ⇒ Object
- .convert(attrs) ⇒ Object
- .db ⇒ Object
- .delete(key) ⇒ Object
- .find(key) ⇒ Object
- .primary_key ⇒ Object
- .primary_key=(key) ⇒ Object
- .save(model) ⇒ Object
- .use_db(db) ⇒ Object
- .use_model(&block) ⇒ Object
Class Method Details
._model ⇒ Object
22 23 24 |
# File 'lib/depository/collection.rb', line 22 def _model self._model_class ||= _model_block.call end |
.convert(attrs) ⇒ Object
62 63 64 65 66 67 68 69 70 71 |
# File 'lib/depository/collection.rb', line 62 def convert(attrs) case attrs when Array attrs.map(&method(:convert)) when Hash _model.new(attrs) else raise UnknownConversionType, "unable to convert #{attrs.inspect}" end end |
.db ⇒ Object
30 31 32 |
# File 'lib/depository/collection.rb', line 30 def db @_db ||= Result.new(Database[_db_name], self) end |
.delete(key) ⇒ Object
58 59 60 |
# File 'lib/depository/collection.rb', line 58 def delete(key) db.where(primary_key => key).delete end |
.find(key) ⇒ Object
51 52 53 54 55 56 |
# File 'lib/depository/collection.rb', line 51 def find(key) result = where(primary_key => key).first return result if !result.nil? raise RecordNotFound, "no record found in collection `#{_db_name}' with id `#{key}'" end |
.primary_key ⇒ Object
34 35 36 |
# File 'lib/depository/collection.rb', line 34 def primary_key @_primary_key ||= :id end |
.primary_key=(key) ⇒ Object
38 39 40 |
# File 'lib/depository/collection.rb', line 38 def primary_key=(key) @_primary_key = key end |
.save(model) ⇒ Object
42 43 44 45 46 47 48 49 |
# File 'lib/depository/collection.rb', line 42 def save(model) if model.send(primary_key) update(model.to_hash) else model.send(:"#{primary_key}=", insert(model.to_hash)) return model end end |
.use_db(db) ⇒ Object
26 27 28 |
# File 'lib/depository/collection.rb', line 26 def use_db(db) db.is_a?(Symbol) ? self._db_name = db : @_db = Result.new(db, self) end |
.use_model(&block) ⇒ Object
18 19 20 |
# File 'lib/depository/collection.rb', line 18 def use_model(&block) self._model_block = block end |