Class: Dddr::Sdbm::RepositoryBase
- Inherits:
-
Object
- Object
- Dddr::Sdbm::RepositoryBase
- Defined in:
- lib/dddr/sdbm.rb
Instance Method Summary collapse
- #add(entity) ⇒ Object (also: #create, #insert, #put, #append, #store)
- #all ⇒ Object
- #count ⇒ Object
- #delete(entity) ⇒ Object (also: #remove, #erase, #discard, #destroy, #wipe)
- #get(uid) ⇒ Object (also: #find)
-
#initialize(entity_class) ⇒ RepositoryBase
constructor
A new instance of RepositoryBase.
- #setup_data_directory(entity_class) ⇒ Object
- #to_csv ⇒ Object
- #update(entity) ⇒ Object (also: #modify, #change, #edit, #revise, #alter)
Constructor Details
#initialize(entity_class) ⇒ RepositoryBase
Returns a new instance of RepositoryBase.
33 34 35 36 37 38 |
# File 'lib/dddr/sdbm.rb', line 33 def initialize(entity_class) @mutex = Mutex.new @entity_class = entity_class setup_data_directory(entity_class) @serializer = Dddr::Serializer.new(Dddr.configuration.serializer) end |
Instance Method Details
#add(entity) ⇒ Object Also known as: create, insert, put, append, store
79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/dddr/sdbm.rb', line 79 def add(entity) @mutex.synchronize do uid = SecureRandom.uuid entity.uid = uid entity.created_at ||= DateTime.now.to_s entity.last_updated_at ||= entity.created_at SDBM.open(@data_dir) do |db| db[uid] = @serializer.dump(entity.to_hash) end uid end end |
#all ⇒ Object
146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/dddr/sdbm.rb', line 146 def all @mutex.synchronize do result = [] SDBM.open(@data_dir) do |db| db.each do |key, value| entity = @entity_class.new entity.from_hash(key, @serializer.load(value)) result << entity unless entity.deleted end end result end end |
#count ⇒ Object
63 64 65 |
# File 'lib/dddr/sdbm.rb', line 63 def count all.count end |
#delete(entity) ⇒ Object Also known as: remove, erase, discard, destroy, wipe
116 117 118 119 120 121 122 123 |
# File 'lib/dddr/sdbm.rb', line 116 def delete(entity) return unless entity.uid @mutex.synchronize do SDBM.open(@data_dir) do |db| db.delete(entity.uid) end end end |
#get(uid) ⇒ Object Also known as: find
131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/dddr/sdbm.rb', line 131 def get(uid) @mutex.synchronize do SDBM.open(@data_dir) do |db| data = db[uid] return unless data entity = @entity_class.new entity.from_hash(uid, @serializer.load(data)) entity end end end |
#setup_data_directory(entity_class) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/dddr/sdbm.rb', line 40 def setup_data_directory(entity_class) env = Dddr.configuration.env data_dir = Dddr.configuration.data_dir container = Dddr.configuration.container raise Dddr::Error, "Container name is required" unless container # Construct the target directory path @data_dir = "#{data_dir}/#{container}/#{env}/#{entity_class.name.downcase}" begin # Ensure all directories in the path are created FileUtils.mkdir_p(@data_dir) # Change ownership only if the path starts with "/var/" if @data_dir.start_with?("/var/") FileUtils.chown_R(ENV["USER"], nil, @data_dir) end rescue Errno::ENOENT => e # Log or handle the error raise "Failed to create or access the directory: #{e.}" end end |
#to_csv ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/dddr/sdbm.rb', line 67 def to_csv env = Dddr.configuration.env data_dir = Dddr.configuration.data_dir container = Dddr.configuration.container CSV.open("#{data_dir}/#{container}/#{env}/#{self.class.name}.csv", "w") do |csv| csv = all.first&.to_hash&.keys all.each do |entity| csv << entity.to_hash.values end end end |
#update(entity) ⇒ Object Also known as: modify, change, edit, revise, alter
99 100 101 102 103 104 105 106 107 108 |
# File 'lib/dddr/sdbm.rb', line 99 def update(entity) return unless entity.uid @mutex.synchronize do entity.last_updated_at = DateTime.now.to_s SDBM.open(@data_dir) do |db| db[entity.uid] = @serializer.dump(entity.to_hash) end entity end end |