Class: JekyllAdmin::Directory

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

Constant Summary collapse

TYPE =
:directory

Constants included from APIable

APIable::CONTENT_FIELDS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from APIable

#to_api

Methods included from URLable

#api_url, #entries_url

Constructor Details

#initialize(path, base: nil, content_type: nil, splat: nil) ⇒ Directory

Arguments:

path - full path of the directory which its entries will be listed

base - passes site.source to generate ‘relative_path` needed for `to_api`

content_type - type of the requested directory entries, this is used to generate API endpoint of the directory along with ‘splat`

splat - the requested directory path relative to content namespace



24
25
26
27
28
29
# File 'lib/jekyll-admin/directory.rb', line 24

def initialize(path, base: nil, content_type: nil, splat: nil)
  @base = Pathname.new base
  @content_type = content_type
  @splat = Pathname.new splat
  @path = Pathname.new path
end

Instance Attribute Details

#baseObject (readonly)

Returns the value of attribute base.



6
7
8
# File 'lib/jekyll-admin/directory.rb', line 6

def base
  @base
end

#content_typeObject (readonly)

Returns the value of attribute content_type.



6
7
8
# File 'lib/jekyll-admin/directory.rb', line 6

def content_type
  @content_type
end

#pathObject (readonly)

Returns the value of attribute path.



6
7
8
# File 'lib/jekyll-admin/directory.rb', line 6

def path
  @path
end

#splatObject (readonly)

Returns the value of attribute splat.



6
7
8
# File 'lib/jekyll-admin/directory.rb', line 6

def splat
  @splat
end

Instance Method Details

#directoriesObject



62
63
64
65
66
67
68
69
70
71
# File 'lib/jekyll-admin/directory.rb', line 62

def directories
  path.entries.map do |entry|
    next if [".", ".."].include? entry.to_s
    next unless path.join(entry).directory?
    self.class.new(
      path.join(entry),
      :base => base, :content_type => content_type, :splat => splat
    )
  end.compact!
end

#http_urlObject



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

def http_url
  nil
end

#relative_pathObject



40
41
42
43
44
45
46
# File 'lib/jekyll-admin/directory.rb', line 40

def relative_path
  if content_type == "drafts"
    path.relative_path_from(base).to_s.sub("_drafts/", "")
  else
    path.relative_path_from(base).to_s
  end
end

#resource_pathObject Also known as: url



48
49
50
51
52
53
54
55
# File 'lib/jekyll-admin/directory.rb', line 48

def resource_path
  types = %w(pages data drafts static_files templates theme)
  if types.include?(content_type)
    "/#{content_type}/#{splat}/#{name}"
  else
    "/collections/#{content_type}/entries/#{splat}/#{name}"
  end
end

#to_liquidObject



31
32
33
34
35
36
37
38
# File 'lib/jekyll-admin/directory.rb', line 31

def to_liquid
  {
    :name          => name,
    :modified_time => modified_time,
    :path          => relative_path,
    :type          => TYPE,
  }
end