Class: UsefulDB::ImportExport
- Inherits:
-
Object
- Object
- UsefulDB::ImportExport
- Defined in:
- lib/usefuldb/import_export.rb
Constant Summary collapse
- FORMATS =
%i[yaml json].freeze
Class Method Summary collapse
- .detect_format(path, explicit_format = nil) ⇒ Object
- .export_content(data, format: :yaml) ⇒ Object
- .import!(data, mode:, log:) ⇒ Object
- .normalize_imported_data(data) ⇒ Object
- .parse_content(content, format) ⇒ Object
- .parse_file(path, format: nil) ⇒ Object
- .read_source(path) ⇒ Object
- .write_export(path, content) ⇒ Object
Class Method Details
.detect_format(path, explicit_format = nil) ⇒ Object
15 16 17 18 19 20 21 22 23 24 |
# File 'lib/usefuldb/import_export.rb', line 15 def detect_format(path, explicit_format = nil) return explicit_format.to_sym if explicit_format case File.extname(path.to_s).downcase when ".json" then :json when ".yaml", ".yml" then :yaml else raise ImportError, "Could not detect format from #{path}. Use --format yaml or --format json." end end |
.export_content(data, format: :yaml) ⇒ Object
50 51 52 53 54 55 56 57 58 59 |
# File 'lib/usefuldb/import_export.rb', line 50 def export_content(data, format: :yaml) case format when :yaml data.to_yaml when :json JSONEncoder.generate(data) else raise ImportError, "Unsupported format: #{format}" end end |
.import!(data, mode:, log:) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/usefuldb/import_export.rb', line 88 def import!(data, mode:, log:) normalized = normalize_imported_data(data) current = UsefulDB::Utils.data case mode when :merge import_merge(current, normalized) when :replace import_replace(normalized) else raise ImportError, "Unsupported import mode: #{mode}" end end |
.normalize_imported_data(data) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/usefuldb/import_export.rb', line 70 def normalize_imported_data(data) if data.is_a?(Array) data = { "version" => UsefulDB::Version.to_s, "db" => data } end unless data.is_a?(Hash) && data["db"].is_a?(Array) raise ImportError, "Import file must contain a db array" end { "version" => UsefulDB::Version.to_s, "db" => data["db"].map { |entry| normalize_entry(entry) } } end |
.parse_content(content, format) ⇒ Object
34 35 36 37 38 39 40 41 42 43 |
# File 'lib/usefuldb/import_export.rb', line 34 def parse_content(content, format) case format when :yaml parse_yaml(content) when :json parse_json(content) else raise ImportError, "Unsupported format: #{format}" end end |
.parse_file(path, format: nil) ⇒ Object
45 46 47 48 |
# File 'lib/usefuldb/import_export.rb', line 45 def parse_file(path, format: nil) detected_format = path == "-" && format.nil? ? :yaml : detect_format(path, format) parse_content(read_source(path), detected_format) end |
.read_source(path) ⇒ Object
26 27 28 29 30 31 32 |
# File 'lib/usefuldb/import_export.rb', line 26 def read_source(path) if path == "-" $stdin.read else File.read(path) end end |
.write_export(path, content) ⇒ Object
61 62 63 64 65 66 67 68 |
# File 'lib/usefuldb/import_export.rb', line 61 def write_export(path, content) if path == "-" print content else FileUtils.mkdir_p(File.dirname(File.(path))) File.write(path, content) end end |