class JekyllAdmin::DataFile

Constants

EXTENSIONS
METHODS_FOR_LIQUID

Public Class Methods

all() click to toggle source
# File lib/jekyll-admin/data_file.rb, line 60
def self.all
  data_dir = sanitized_path DataFile.data_dir
  Dir["#{data_dir}/*.{#{EXTENSIONS.join(",")}}"].map do |path|
    new path_without_site_source(path)
  end
end
data_dir() click to toggle source

Relative path to data directory within site source

# File lib/jekyll-admin/data_file.rb, line 68
def self.data_dir
  JekyllAdmin.site.config["data_dir"]
end
new(id) click to toggle source

Initialize a new DataFile object

id - the file ID as passed from the API. This may or may not have an extension

# File lib/jekyll-admin/data_file.rb, line 16
def initialize(id)
  @id ||= id
end

Public Instance Methods

content() click to toggle source

Returnes (re)parsed content using Jekyll's native parsing mechanism

# File lib/jekyll-admin/data_file.rb, line 30
def content
  @content ||= data_reader.read_data_file(absolute_path)
end
exists?() click to toggle source
# File lib/jekyll-admin/data_file.rb, line 20
def exists?
  @exists ||= File.exist?(absolute_path)
end
ext() click to toggle source

Returns the file's extension with preceeding `.`

# File lib/jekyll-admin/data_file.rb, line 35
def ext
  @ext ||= if File.extname(@id).to_s.empty?
             ".yml"
           else
             File.extname(@id)
           end
end
Also aliased as: extension
extension()
Alias for: ext
raw_content() click to toggle source

Returns unparsed content as it exists on disk

# File lib/jekyll-admin/data_file.rb, line 25
def raw_content
  @raw_content ||= File.open(absolute_path, "r:UTF-8", &:read)
end
slug() click to toggle source

Returns the file's sanitized slug (as used in `site.data`)

# File lib/jekyll-admin/data_file.rb, line 45
def slug
  @slug ||= data_reader.sanitize_filename(basename)
end
title() click to toggle source

Returns the human-readable title of the data file

# File lib/jekyll-admin/data_file.rb, line 50
def title
  @title ||= Jekyll::Utils.titleize_slug(slug.tr("_", "-"))
end
to_liquid() click to toggle source

Mimics Jekyll's native to_liquid functionality by returning a hash of data file metadata

# File lib/jekyll-admin/data_file.rb, line 56
def to_liquid
  @to_liquid ||= METHODS_FOR_LIQUID.map { |key| [key, public_send(key)] }.to_h
end

Private Instance Methods

basename() click to toggle source
# File lib/jekyll-admin/data_file.rb, line 78
def basename
  @basename ||= File.basename(@id, ".*")
end
basename_with_extension() click to toggle source
# File lib/jekyll-admin/data_file.rb, line 82
def basename_with_extension
  [basename, extension].join
end
Also aliased as: filename
data_reader() click to toggle source
# File lib/jekyll-admin/data_file.rb, line 74
def data_reader
  @data_reader = Jekyll::DataReader.new(JekyllAdmin.site)
end
filename()
namespace() click to toggle source
# File lib/jekyll-admin/data_file.rb, line 87
def namespace
  "data"
end