Class: FlatKit::Format

Inherits:
Object
  • Object
show all
Extended by:
DescendantTracker
Defined in:
lib/flat_kit/format.rb

Overview

Internal: The base class of all data file format classes

Direct Known Subclasses

Jsonl::Format, Xsv::Format

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DescendantTracker

children, find_child, find_children, inherited

Class Method Details

.for(path) ⇒ Object



17
18
19
# File 'lib/flat_kit/format.rb', line 17

def self.for(path)
  find_child(:handles?, path.to_s)
end

.for_with_fallback(path:, fallback: "auto") ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/flat_kit/format.rb', line 21

def self.for_with_fallback(path:, fallback: "auto")
  # test by path
  format = ::FlatKit::Format.for(path)
  return format unless format.nil?

  # now try the fallback
  ::FlatKit::Format.for(fallback)
end

.for_with_fallback!(path:, fallback: "auto") ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/flat_kit/format.rb', line 30

def self.for_with_fallback!(path:, fallback: "auto")
  format = for_with_fallback(path: path, fallback: fallback)
  if format.nil?
    raise ::FlatKit::Error::UnknownFormat,
          "Unable to figure out format for '#{path}' with fallback '#{fallback}'"
  end

  format
end

.format_nameObject

Raises:

  • (NotImplementedError)


9
10
11
# File 'lib/flat_kit/format.rb', line 9

def self.format_name
  raise NotImplementedError, "#{self.class} must implement #{self.class}.format_name"
end

Instance Method Details

#format_nameObject



13
14
15
# File 'lib/flat_kit/format.rb', line 13

def format_name
  self.class.format_name
end