Module: Rakit::Data::DataService
- Defined in:
- lib/rakit/data.rb
Overview
Persist and retrieve protobuf message instances under a configurable data root.
Storage layout: root is DataService.data_dir (default ~/.rakit/data). Path for a message is data_dir/TYPE_PATH/unique_name.pb. TYPE_PATH is the Ruby class name with :: replaced by File::SEPARATOR (e.g. Rakit::Shell::Command → Rakit/Shell/Command). File content is binary protobuf; no character encoding.
Class Method Summary collapse
-
._dir_for_type(type_name) ⇒ Object
PACKAGE_PATH/MESSAGE_NAME: e.g.
- ._path(type_name, unique_name) ⇒ Object
- ._validate_unique_name!(unique_name) ⇒ Object
-
.data_dir ⇒ String
Current data root (default: expanded ~/.rakit/data).
- .data_dir=(path) ⇒ void
-
.get_names(type_name) ⇒ Array<String>
Return unique names (without
.pb) for the given type. -
.load(type_name, unique_name) ⇒ Object
Load a stored message.
-
.remove(type_name, unique_name) ⇒ void
Remove a stored message by type and unique name (no-op if file absent).
-
.store(message, unique_name) ⇒ void
Store a proto message under a unique name.
Class Method Details
._dir_for_type(type_name) ⇒ Object
PACKAGE_PATH/MESSAGE_NAME: e.g. Rakit::Shell::Command -> Rakit/Shell/Command
164 165 166 167 168 169 170 |
# File 'lib/rakit/data.rb', line 164 def self._dir_for_type(type_name) parts = type_name.split("::") raise ArgumentError, "type_name must be a qualified constant path" if parts.empty? relative = parts.join(File::SEPARATOR) File.join(data_dir, relative) end |
._path(type_name, unique_name) ⇒ Object
158 159 160 161 |
# File 'lib/rakit/data.rb', line 158 def self._path(type_name, unique_name) dir = _dir_for_type(type_name) File.join(dir, "#{unique_name}.pb") end |
._validate_unique_name!(unique_name) ⇒ Object
150 151 152 153 154 155 156 |
# File 'lib/rakit/data.rb', line 150 def self._validate_unique_name!(unique_name) u = unique_name.to_s raise ArgumentError, "unique_name must be a non-empty string" if u.strip.empty? if u.include?("/") || u.include?("\\") || u.include?("..") raise ArgumentError, "unique_name must not contain path separators or '..'" end end |
.data_dir ⇒ String
Returns current data root (default: expanded ~/.rakit/data).
87 88 89 |
# File 'lib/rakit/data.rb', line 87 def self.data_dir @data_dir ||= File.("~/.rakit/data") end |
.data_dir=(path) ⇒ void
This method returns an undefined value.
93 94 95 |
# File 'lib/rakit/data.rb', line 93 def self.data_dir=(path) @data_dir = path end |
.get_names(type_name) ⇒ Array<String>
Return unique names (without .pb) for the given type.
143 144 145 146 147 148 |
# File 'lib/rakit/data.rb', line 143 def self.get_names(type_name) dir = _dir_for_type(type_name.to_s) return [] unless File.directory?(dir) Dir.children(dir).select { |f| File.file?(File.join(dir, f)) && f.end_with?(".pb") }.map { |f| f.chomp(".pb") } end |
.load(type_name, unique_name) ⇒ Object
Load a stored message.
118 119 120 121 122 123 124 125 |
# File 'lib/rakit/data.rb', line 118 def self.load(type_name, unique_name) _validate_unique_name!(unique_name) klass = Object.const_get(type_name.to_s) path = _path(klass.name, unique_name.to_s) raise Errno::ENOENT, path unless File.file?(path) klass.decode(File.binread(path)) end |
.remove(type_name, unique_name) ⇒ void
This method returns an undefined value.
Remove a stored message by type and unique name (no-op if file absent).
132 133 134 135 136 |
# File 'lib/rakit/data.rb', line 132 def self.remove(type_name, unique_name) _validate_unique_name!(unique_name) path = _path(type_name.to_s, unique_name.to_s) File.delete(path) if File.file?(path) end |
.store(message, unique_name) ⇒ void
This method returns an undefined value.
Store a proto message under a unique name.
103 104 105 106 107 108 109 |
# File 'lib/rakit/data.rb', line 103 def self.store(, unique_name) _validate_unique_name!(unique_name) klass = .class path = _path(klass.name, unique_name) FileUtils.mkdir_p(File.dirname(path)) File.binwrite(path, klass.encode()) end |