Class: Dddr::Sequel::RepositoryBase
- Inherits:
-
Object
- Object
- Dddr::Sequel::RepositoryBase
- Defined in:
- lib/dddr/sequel.rb
Instance Attribute Summary collapse
-
#dataset ⇒ Object
readonly
Returns the value of attribute dataset.
Instance Method Summary collapse
- #add(entity) ⇒ Object (also: #create, #insert)
- #all ⇒ Object
- #count ⇒ Object
- #delete(entity) ⇒ Object
- #get(uid) ⇒ Object (also: #find)
-
#initialize ⇒ RepositoryBase
constructor
A new instance of RepositoryBase.
- #update(entity) ⇒ Object
Constructor Details
#initialize ⇒ RepositoryBase
Returns a new instance of RepositoryBase.
55 56 57 58 |
# File 'lib/dddr/sequel.rb', line 55 def initialize @dataset = Dddr.configuration.db[self.class.entity_class.table] self.class.entity_class.create_properties(@dataset.columns) end |
Instance Attribute Details
#dataset ⇒ Object (readonly)
Returns the value of attribute dataset.
53 54 55 |
# File 'lib/dddr/sequel.rb', line 53 def dataset @dataset end |
Instance Method Details
#add(entity) ⇒ Object Also known as: create, insert
84 85 86 87 88 89 90 91 92 |
# File 'lib/dddr/sequel.rb', line 84 def add(entity) uid = SecureRandom.uuid entity.uid = uid entity.created_at ||= DateTime.now.to_s entity.last_updated_at ||= entity.created_at @dataset.insert(entity.to_hash) uid end |
#all ⇒ Object
74 75 76 |
# File 'lib/dddr/sequel.rb', line 74 def all @dataset.map { |row| self.class.entity_class.from_row(row) } end |
#count ⇒ Object
60 61 62 |
# File 'lib/dddr/sequel.rb', line 60 def count @dataset.count end |
#delete(entity) ⇒ Object
78 79 80 |
# File 'lib/dddr/sequel.rb', line 78 def delete(entity) @dataset.where(uid: entity.uid).delete end |
#get(uid) ⇒ Object Also known as: find
64 65 66 67 |
# File 'lib/dddr/sequel.rb', line 64 def get(uid) row = @dataset.where(uid: uid).first self.class.entity_class.from_row(row) if row end |
#update(entity) ⇒ Object
69 70 71 72 |
# File 'lib/dddr/sequel.rb', line 69 def update(entity) entity.last_updated_at = DateTime.now @dataset.where(uid: entity.uid).update(entity.to_hash) end |