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(path relative_path slug ext title).freeze
EXTENSIONS =
%w(yaml yml json csv).freeze

Constants included from APIable

APIable::CONTENT_FIELDS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from PathHelper

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



16
17
18
# File 'lib/jekyll-admin/data_file.rb', line 16

def initialize(id)
  @id ||= File.extname(id).empty? ? "#{id}.yml" : id
end

Instance Attribute Details

#idObject (readonly) Also known as: path

Returns the value of attribute id.



11
12
13
# File 'lib/jekyll-admin/data_file.rb', line 11

def id
  @id
end

Class Method Details

.allObject



71
72
73
74
75
76
77
78
79
80
# File 'lib/jekyll-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_dirObject

Relative path to data directory within site source



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

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

Instance Method Details

#absolute_pathObject

Return the absolute path to given data file



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

def absolute_path
  sanitized_path id
end

#contentObject

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



31
32
33
# File 'lib/jekyll-admin/data_file.rb', line 31

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

#exists?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/jekyll-admin/data_file.rb', line 21

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

#extObject Also known as: extension

Returns the file’s extension with preceeding ‘.`



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

def ext
  @ext ||= if File.extname(id).to_s.empty?
             ".yml"
           else
             File.extname(id)
           end
end

#raw_contentObject

Returns unparsed content as it exists on disk



26
27
28
# File 'lib/jekyll-admin/data_file.rb', line 26

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

#relative_pathObject

Return path relative to configured ‘data_dir`



56
57
58
# File 'lib/jekyll-admin/data_file.rb', line 56

def relative_path
  id.sub("/#{DataFile.data_dir}/", "")
end

#slugObject

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



46
47
48
# File 'lib/jekyll-admin/data_file.rb', line 46

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

#titleObject

Returns the human-readable title of the data file



51
52
53
# File 'lib/jekyll-admin/data_file.rb', line 51

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



67
68
69
# File 'lib/jekyll-admin/data_file.rb', line 67

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