Class: Textus::Entry::Text

Inherits:
Base
  • Object
show all
Defined in:
lib/textus/entry/text.rb

Overview

Plain-text entry storage. No frontmatter or structured content.

Class Method Summary collapse

Methods inherited from Base

validate_against

Class Method Details

.enforce_name_match!(_path, _meta) ⇒ Object



28
29
30
# File 'lib/textus/entry/text.rb', line 28

def self.enforce_name_match!(_path, _meta)
  # text has no meta home; no-op
end

.extensionsObject



20
# File 'lib/textus/entry/text.rb', line 20

def self.extensions = [".txt"]

.inject_uid(meta, content, _existing_uid) ⇒ Object



24
25
26
# File 'lib/textus/entry/text.rb', line 24

def self.inject_uid(meta, content, _existing_uid)
  [meta, content]
end

.nested_globObject



22
# File 'lib/textus/entry/text.rb', line 22

def self.nested_glob = "**/*.txt"

.parse(raw, path: nil) ⇒ Object

Raises:



5
6
7
8
9
10
# File 'lib/textus/entry/text.rb', line 5

def self.parse(raw, path: nil)
  raw = raw.dup.force_encoding(Encoding::UTF_8)
  raise BadFrontmatter.new(path, "entry is not valid UTF-8") unless raw.valid_encoding?

  { "_meta" => {}, "body" => raw, "content" => nil }
end

.rewrite_name(_path, _basename) ⇒ Object

No-op; text has no meta. Returns false (never writes).



40
41
42
# File 'lib/textus/entry/text.rb', line 40

def self.rewrite_name(_path, _basename) # rubocop:disable Naming/PredicateMethod
  false
end

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



12
13
14
15
16
17
18
# File 'lib/textus/entry/text.rb', line 12

def self.serialize(meta:, body:, content: nil)
  _ = meta
  _ = content
  b = body.to_s
  b += "\n" unless b.empty? || b.end_with?("\n")
  b
end

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



32
33
34
35
36
37
# File 'lib/textus/entry/text.rb', line 32

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_path_extension(path, nested) ⇒ Object

Raises:



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/textus/entry/text.rb', line 44

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

    raise UsageError.new("nested text path must not have an extension")
  end

  return if [".txt", ""].include?(ext)

  raise UsageError.new("text format requires '.txt' or no extension (got #{ext.inspect})")
end