Class: JekyllAdmin::DataFile

Inherits:
Object
  • Object
show all
Extended by:
PathHelper
Includes:
APIable, PathHelper, URLable
Defined in:
lib/jekyll-admin/data_file.rb

Constant Summary collapse

METHODS_FOR_LIQUID =
%w(name path relative_path slug ext title).freeze
EXTENSIONS =
%w(yaml yml json csv).freeze

Constants included from APIable

APIable::API_SCAFFOLD, APIable::CONTENT_FIELDS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from PathHelper

filename, new?, relative_write_path, renamed?, sanitized_path, sanitized_relative_path, write_path

Methods included from URLable

#api_url, #entries_url, #http_url

Methods included from APIable

#to_api

Constructor Details

#initialize(id) ⇒ DataFile

Initialize a new DataFile object

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



18
19
20
21
22
23
24
25
26
27
# File 'lib/jekyll-admin/data_file.rb', line 18

def initialize(id)
  extname = File.extname(id)
  if extname.empty?
    @id  = "#{id}.yml"
    @ext = ".yml"
  else
    @id  = id
    @ext = extname
  end
end

Instance Attribute Details

#extObject (readonly) Also known as: extension

Returns the value of attribute ext.



13
14
15
# File 'lib/jekyll-admin/data_file.rb', line 13

def ext
  @ext
end

#idObject (readonly) Also known as: relative_path

Returns the value of attribute id.



13
14
15
# File 'lib/jekyll-admin/data_file.rb', line 13

def id
  @id
end

Class Method Details

.allObject



73
74
75
76
77
78
79
# File 'lib/jekyll-admin/data_file.rb', line 73

def self.all
  data_dir = Jekyll.sanitized_path(JekyllAdmin.site.source, DataFile.data_dir)
  data_dir = Pathname.new(data_dir)
  Dir["#{data_dir}/**/*.{#{EXTENSIONS.join(",")}}"].map do |path|
    new(Pathname.new(path).relative_path_from(data_dir).to_s)
  end
end

.data_dirObject

Relative path to data directory within site source



82
83
84
# File 'lib/jekyll-admin/data_file.rb', line 82

def self.data_dir
  JekyllAdmin.site.config["data_dir"]
end

Instance Method Details

#absolute_pathObject Also known as: file_path



62
63
64
# File 'lib/jekyll-admin/data_file.rb', line 62

def absolute_path
  sanitized_path(path)
end

#contentObject

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



43
44
45
# File 'lib/jekyll-admin/data_file.rb', line 43

def content
  @content ||= data_reader.read_data_file(absolute_path)
end

#exists?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/jekyll-admin/data_file.rb', line 33

def exists?
  @exists ||= File.exist?(absolute_path)
end

#pathObject

Returns path relative to site source



58
59
60
# File 'lib/jekyll-admin/data_file.rb', line 58

def path
  @path ||= File.join(DataFile.data_dir, relative_path)
end

#raw_contentObject

Returns unparsed content as it exists on disk



38
39
40
# File 'lib/jekyll-admin/data_file.rb', line 38

def raw_content
  @raw_content ||= File.open(absolute_path, "r:UTF-8", &:read)
end

#slugObject

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



48
49
50
# File 'lib/jekyll-admin/data_file.rb', line 48

def slug
  @slug ||= data_reader.sanitize_filename(basename)
end

#titleObject

Returns the human-readable title of the data file



53
54
55
# File 'lib/jekyll-admin/data_file.rb', line 53

def title
  @title ||= Jekyll::Utils.titleize_slug(slug.tr("_", "-"))
end

#to_liquidObject

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



69
70
71
# File 'lib/jekyll-admin/data_file.rb', line 69

def to_liquid
  @to_liquid ||= METHODS_FOR_LIQUID.map { |key| [key, public_send(key)] }.to_h
end