Class: Decant::Content

Inherits:
File
  • Object
show all
Defined in:
lib/decant/content.rb

Class Attribute Summary collapse

Attributes inherited from File

#path

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from File

#content, #frontmatter, #frontmatter?, #initialize, #read

Constructor Details

This class inherits a constructor from Decant::File

Class Attribute Details

.collectionCollection (readonly)

When using Decant.define the returned Decant::Content subclass comes with its own Decant::Collection instance.

Returns:



12
13
14
# File 'lib/decant/content.rb', line 12

def collection
  @collection
end

Class Method Details

.allArray<Content>

Return all matching files within collection.

Returns:



17
18
19
# File 'lib/decant/content.rb', line 17

def all
  collection.entries.map { |path| new(path) }
end

.find(pattern) ⇒ Content

Find a file within the collection by passing its relative path.

Examples:

Find a specific nested file within a content directory

Page = Decant.define(dir: 'content', ext: '.md')

# Return an instance for the file `content/features/nesting.md`.
Page.find('features/nesting')

Parameters:

  • pattern (String)

Returns:

Raises:



34
35
36
37
38
# File 'lib/decant/content.rb', line 34

def find(pattern)
  path = collection.find(pattern)
  raise FileNotFound, %(Couldn't find "#{pattern}" in "#{collection.dir}") unless path
  new(path)
end

.frontmatter(*attrs) ⇒ Object

Define convenience frontmatter readers - see Decant.define.

Parameters:

  • attrs (Array<Symbol>)

    a list of convenience frontmatter readers.



43
44
45
46
47
48
49
# File 'lib/decant/content.rb', line 43

def frontmatter(*attrs)
  attrs.each do |name|
    define_method name do
      frontmatter&.[](name.to_sym)
    end
  end
end

Instance Method Details

#relative_pathString

The relative path of the file within its collection.

Examples:

Page = Decant.define(dir: 'content', ext: 'md')

page = Page.find('features/slugs')
page.path.expand_path # => "/Users/dave/my-website/content/features/slugs.md"
page.relative_path    # => "features/slugs.md"

Returns:

  • (String)


62
63
64
# File 'lib/decant/content.rb', line 62

def relative_path
  self.class.collection.relative_path_for(path)
end

#slugString

The extension-less relative path of the file within its collection.

Examples:

Page = Decant.define(dir: 'content', ext: 'md')

page = Page.find('features/slugs')
page.path.expand_path # => "/Users/dave/my-website/content/features/slugs.md"
page.slug             # => "features/slugs"

Returns:

  • (String)


76
77
78
# File 'lib/decant/content.rb', line 76

def slug
  self.class.collection.slug_for(path)
end