Class: JekyllAdmin::DataFile
- Inherits:
-
Object
- Object
- JekyllAdmin::DataFile
- Defined in:
- lib/jekyll-admin/data_file.rb
Constant Summary collapse
- METHODS_FOR_LIQUID =
%w(path relative_path slug ext title).freeze
- EXTENSIONS =
%w(yaml yml json csv).freeze
Constants included from APIable
Class Method Summary collapse
- .all ⇒ Object
-
.data_dir ⇒ Object
Relative path to data directory within site source.
Instance Method Summary collapse
- #absolute_path ⇒ Object (also: #file_path)
-
#content ⇒ Object
Returnes (re)parsed content using Jekyll’s native parsing mechanism.
- #exists? ⇒ Boolean
-
#ext ⇒ Object
(also: #extension)
Returns the file’s extension with preceeding
.. -
#initialize(id) ⇒ DataFile
constructor
Initialize a new DataFile object.
-
#raw_content ⇒ Object
Returns unparsed content as it exists on disk.
-
#relative_path ⇒ Object
(also: #path)
Returns the relative path relative to site source.
-
#slug ⇒ Object
Returns the file’s sanitized slug (as used in
site.data[slug]). -
#title ⇒ Object
Returns the human-readable title of the data file.
-
#to_liquid ⇒ Object
Mimics Jekyll’s native to_liquid functionality by returning a hash of data file metadata.
Methods included from URLable
Methods included from APIable
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
12 13 14 |
# File 'lib/jekyll-admin/data_file.rb', line 12 def initialize(id) @id ||= id end |
Class Method Details
.all ⇒ Object
67 68 69 70 71 72 73 |
# File 'lib/jekyll-admin/data_file.rb', line 67 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_dir ⇒ Object
Relative path to data directory within site source
76 77 78 |
# File 'lib/jekyll-admin/data_file.rb', line 76 def self.data_dir JekyllAdmin.site.config["data_dir"] end |
Instance Method Details
#absolute_path ⇒ Object Also known as: file_path
20 21 22 |
# File 'lib/jekyll-admin/data_file.rb', line 20 def absolute_path @absolute_path ||= Jekyll.sanitized_path(JekyllAdmin.site.source, relative_path) end |
#content ⇒ Object
Returnes (re)parsed content using Jekyll’s native parsing mechanism
37 38 39 |
# File 'lib/jekyll-admin/data_file.rb', line 37 def content @content ||= data_reader.read_data_file(absolute_path) end |
#exists? ⇒ Boolean
16 17 18 |
# File 'lib/jekyll-admin/data_file.rb', line 16 def exists? @exists ||= File.exist?(absolute_path) end |
#ext ⇒ Object Also known as: extension
Returns the file’s extension with preceeding .
42 43 44 45 46 47 48 |
# File 'lib/jekyll-admin/data_file.rb', line 42 def ext @ext ||= if File.extname(@id).to_s.empty? ".yml" else File.extname(@id) end end |
#raw_content ⇒ Object
Returns unparsed content as it exists on disk
32 33 34 |
# File 'lib/jekyll-admin/data_file.rb', line 32 def raw_content @raw_content ||= File.open(absolute_path, "r:UTF-8", &:read) end |
#relative_path ⇒ Object Also known as: path
Returns the relative path relative to site source
26 27 28 |
# File 'lib/jekyll-admin/data_file.rb', line 26 def relative_path @relative_path = File.join(DataFile.data_dir, basename_with_extension) end |
#slug ⇒ Object
Returns the file’s sanitized slug (as used in site.data[slug])
52 53 54 |
# File 'lib/jekyll-admin/data_file.rb', line 52 def slug @slug ||= data_reader.sanitize_filename(basename) end |
#title ⇒ Object
Returns the human-readable title of the data file
57 58 59 |
# File 'lib/jekyll-admin/data_file.rb', line 57 def title @title ||= Jekyll::Utils.titleize_slug(slug.tr("_", "-")) end |
#to_liquid ⇒ Object
Mimics Jekyll’s native to_liquid functionality by returning a hash of data file metadata
63 64 65 |
# File 'lib/jekyll-admin/data_file.rb', line 63 def to_liquid @to_liquid ||= METHODS_FOR_LIQUID.map { |key| [key, public_send(key)] }.to_h end |