Module: Rakit::Data

Defined in:
lib/rakit/data.rb,
lib/generated/data_pb.rb

Defined Under Namespace

Modules: DataService

Constant Summary collapse

Index =
::Google::Protobuf::DescriptorPool.generated_pool.lookup("rakit.data.Index").msgclass
ExportFormat =
::Google::Protobuf::DescriptorPool.generated_pool.lookup("rakit.data.ExportFormat").enummodule

Class Method Summary collapse

Class Method Details

._export_binary_files(pb_paths, base, export_dir) ⇒ Object



45
46
47
48
49
50
51
52
53
# File 'lib/rakit/data.rb', line 45

def self._export_binary_files(pb_paths, base, export_dir)
  pb_paths.each do |path|
    type_name, _unique_name, rel = _rel_parts(base, path)
    message = DataService.load(type_name, File.basename(path, ".pb"))
    out_path = File.join(export_dir, rel)
    FileUtils.mkdir_p(File.dirname(out_path))
    File.binwrite(out_path, message.class.encode(message))
  end
end

._export_binary_zipped(pb_paths, base, export_dir) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/rakit/data.rb', line 66

def self._export_binary_zipped(pb_paths, base, export_dir)
  require "zip"
  zip_path = File.join(export_dir, "data.zip")
  FileUtils.rm_f(zip_path)
  Zip::File.open(zip_path, Zip::File::CREATE) do |zip|
    pb_paths.each do |path|
      type_name, unique_name, rel = _rel_parts(base, path)
      message = DataService.load(type_name, unique_name)
      zip.get_output_stream(rel) { |io| io.write(message.class.encode(message)) }
    end
  end
end

._export_json_files(pb_paths, base, export_dir) ⇒ Object



55
56
57
58
59
60
61
62
63
64
# File 'lib/rakit/data.rb', line 55

def self._export_json_files(pb_paths, base, export_dir)
  pb_paths.each do |path|
    type_name, unique_name, rel = _rel_parts(base, path)
    message = DataService.load(type_name, unique_name)
    out_rel = rel.sub(/\.pb\z/, ".json")
    out_path = File.join(export_dir, out_rel)
    FileUtils.mkdir_p(File.dirname(out_path))
    File.write(out_path, message.class.encode_json(message))
  end
end

._rel_parts(base, path) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/rakit/data.rb', line 37

def self._rel_parts(base, path)
  rel = path[(base.end_with?(File::SEPARATOR) ? base.length : base.length + 1)..]
  parts = rel.split(File::SEPARATOR)
  type_name = parts[0..-2].join("::")
  unique_name = File.basename(parts[-1], ".pb")
  [type_name, unique_name, rel]
end

.export(export_dir, export_format) ⇒ void

This method returns an undefined value.

Export all stored messages from DataService.data_dir to export_dir in the given format. If there are no .pb files under data_dir, no files are created.

Parameters:

  • export_dir (String)

    target directory (created if needed)

  • export_format (Rakit::Data::ExportFormat)

    PROTOBUF_BINARY_FILES (mirror .pb layout), PROTOBUF_JSON_FILES (same layout with .json), or PROTOBUF_BINARY_ZIPPED (single data.zip)

Raises:

  • (ArgumentError)

    if export_format is not a supported ExportFormat value



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/rakit/data.rb', line 17

def self.export(export_dir, export_format)
  base = DataService.data_dir
  export_dir = File.expand_path(export_dir)
  FileUtils.mkdir_p(export_dir)

  pb_paths = Dir.glob(File.join(base, "**", "*.pb"))
  return if pb_paths.empty?

  case export_format
  when ExportFormat::PROTOBUF_BINARY_FILES
    _export_binary_files(pb_paths, base, export_dir)
  when ExportFormat::PROTOBUF_JSON_FILES
    _export_json_files(pb_paths, base, export_dir)
  when ExportFormat::PROTOBUF_BINARY_ZIPPED
    _export_binary_zipped(pb_paths, base, export_dir)
  else
    raise ArgumentError, "unsupported export_format: #{export_format.inspect}"
  end
end