Class: Textus::Protocol::Format::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/textus/protocol/format/base.rb

Direct Known Subclasses

Json, Markdown, Script, Text, Yaml

Class Method Summary collapse

Class Method Details

.enforce_name_match!(path, meta) ⇒ Object

Raises:



42
43
44
45
46
47
48
49
50
# File 'lib/textus/protocol/format/base.rb', line 42

def self.enforce_name_match!(path, meta)
  return unless meta.is_a?(Hash) && meta["name"]

  ext = extensions.first
  basename = File.basename(path, ext)
  return if meta["name"] == basename

  raise BadFrontmatter.new(path, "name '#{meta["name"]}' does not match basename '#{basename}'")
end

.extensionsObject

Raises:

  • (NotImplementedError)


17
18
19
# File 'lib/textus/protocol/format/base.rb', line 17

def self.extensions
  raise NotImplementedError.new("#{name}.extensions not implemented")
end

.format_nameObject



63
64
65
# File 'lib/textus/protocol/format/base.rb', line 63

def self.format_name
  name.split("::").last.downcase
end

.nested_ext(_segment = nil) ⇒ Object



69
# File 'lib/textus/protocol/format/base.rb', line 69

def self.nested_ext(_segment = nil) = extensions.first

.nested_globObject

Raises:

  • (NotImplementedError)


25
26
27
# File 'lib/textus/protocol/format/base.rb', line 25

def self.nested_glob
  raise NotImplementedError.new("#{name}.nested_glob not implemented")
end

.parse(_raw, path: nil) ⇒ Object

Raises:

  • (NotImplementedError)


5
6
7
8
# File 'lib/textus/protocol/format/base.rb', line 5

def self.parse(_raw, path: nil)
  _ = path
  raise NotImplementedError.new("#{name}.parse not implemented")
end

.rewrite_name(path, basename) ⇒ Object

rubocop:disable Naming/PredicateMethod



52
53
54
55
56
57
58
59
60
61
# File 'lib/textus/protocol/format/base.rb', line 52

def self.rewrite_name(path, basename) # rubocop:disable Naming/PredicateMethod
  raw = File.binread(path)
  parsed = parse(raw, path: path)
  meta = parsed["_meta"] || {}
  return false unless meta.is_a?(Hash) && meta["name"].is_a?(String) && meta["name"] != basename

  new_meta = meta.merge("name" => basename)
  File.binwrite(path, serialize(meta: new_meta, body: parsed["body"] || "", content: parsed["content"]))
  true
end

.serialize(meta: {}, body: "", content: nil) ⇒ Object

Raises:

  • (NotImplementedError)


10
11
12
13
14
15
# File 'lib/textus/protocol/format/base.rb', line 10

def self.serialize(meta: {}, body: "", content: nil)
  _ = meta
  _ = body
  _ = content
  raise NotImplementedError.new("#{name}.serialize not implemented")
end

.serialize_for_put(meta:, body:, content:, path:) ⇒ Object



71
72
73
74
75
76
# File 'lib/textus/protocol/format/base.rb', line 71

def self.serialize_for_put(meta:, body:, content:, path:)
  _ = path
  _ = content
  bytes = serialize(meta: meta || {}, body: body.to_s)
  [bytes, meta, body.to_s, nil]
end

.validate_against(schema, parsed) ⇒ Object



21
22
23
# File 'lib/textus/protocol/format/base.rb', line 21

def self.validate_against(schema, parsed)
  schema.validate!(parsed["_meta"] || {})
end

.validate_path_extension(path, nested) ⇒ Object

Raises:



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/textus/protocol/format/base.rb', line 29

def self.validate_path_extension(path, nested)
  ext = File.extname(path)
  if nested
    return if ext == ""

    raise UsageError.new("#{format_name} nested path must not have an extension")
  end

  return if extensions.include?(ext)

  raise UsageError.new("#{format_name} format requires '#{extensions.join("' or '")}' path (got #{ext.inspect})")
end

.validate_raw_entry!(_parsed, _lane) ⇒ Object



67
# File 'lib/textus/protocol/format/base.rb', line 67

def self.validate_raw_entry!(_parsed, _lane); end