class BuntoAdmin::DataFile

Constants

EXTENSIONS
METHODS_FOR_LIQUID

Attributes

id[R]
path[R]

Public Class Methods

all() click to toggle source
# File lib/bunto-admin/data_file.rb, line 71
def self.all
  data_dir = sanitized_path DataFile.data_dir
  files = Dir["#{data_dir}/**/*.{#{EXTENSIONS.join(",")}}"].reject do |d|
    File.directory?(d)
  end

  files.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/bunto-admin/data_file.rb, line 83
def self.data_dir
  BuntoAdmin.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/bunto-admin/data_file.rb, line 16
def initialize(id)
  @id ||= File.extname(id).empty? ? "#{id}.yml" : id
end

Public Instance Methods

absolute_path() click to toggle source

Return the absolute path to given data file

# File lib/bunto-admin/data_file.rb, line 61
def absolute_path
  sanitized_path id
end
content() click to toggle source

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

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

Returns the file's extension with preceeding `.`

# File lib/bunto-admin/data_file.rb, line 36
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/bunto-admin/data_file.rb, line 26
def raw_content
  @raw_content ||= File.open(absolute_path, "r:UTF-8", &:read)
end
relative_path() click to toggle source

Return path relative to configured `data_dir`

# File lib/bunto-admin/data_file.rb, line 56
def relative_path
  id.sub("/#{DataFile.data_dir}/", "")
end
slug() click to toggle source

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

# File lib/bunto-admin/data_file.rb, line 46
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/bunto-admin/data_file.rb, line 51
def title
  @title ||= Bunto::Utils.titleize_slug(slug.tr("_", "-"))
end
to_liquid() click to toggle source

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

# File lib/bunto-admin/data_file.rb, line 67
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/bunto-admin/data_file.rb, line 93
def basename
  @basename ||= File.basename(id, ".*")
end
basename_with_extension() click to toggle source
# File lib/bunto-admin/data_file.rb, line 97
def basename_with_extension
  [basename, extension].join
end
Also aliased as: filename
data_reader() click to toggle source
# File lib/bunto-admin/data_file.rb, line 89
def data_reader
  @data_reader = Bunto::DataReader.new(BuntoAdmin.site)
end
filename()
namespace() click to toggle source
# File lib/bunto-admin/data_file.rb, line 102
def namespace
  "data"
end