Module: Decant

Defined in:
lib/decant.rb,
lib/decant/file.rb,
lib/decant/errors.rb,
lib/decant/content.rb,
lib/decant/version.rb,
lib/decant/collection.rb,
lib/decant/path_utils.rb,
lib/decant/frontmatter.rb

Defined Under Namespace

Modules: Frontmatter, PathUtils Classes: Collection, Content, File

Constant Summary collapse

Error =
Class.new(StandardError)
FileNotFound =
Class.new(Error)
VERSION =
'0.3.0'

Class Method Summary collapse

Class Method Details

.define(dir:, ext: nil) { ... } ⇒ Class<Content>

Defines a new Content subclass and assigns it a new Collection with dir/ext. Passing a block lets you to declare convenience frontmatter readers and add your own methods.

Examples:

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

  def shouty
    "#{title.upcase}!!!"
  end
end

# Given a file `content/about.md` with the following contents:
#
# ---
# title: About
# ---
# About Decant

about = Page.find('about')
about.content     # => "About Decant"
about.frontmatter # => {:title=>"About"}
about.title       # => "About"
about.shouty      # => "ABOUT!!!"

Parameters:

  • dir (Pathname, String)
  • ext (String, nil) (defaults to: nil)

Yields:

  • pass an optional block to declare convenience frontmatter readers with Decant::Content.frontmatter and add your own methods to the class.

Returns:



40
41
42
43
44
45
# File 'lib/decant.rb', line 40

def self.define(dir:, ext: nil, &block)
  Class.new(Content) do
    @collection = Collection.new(dir: dir, ext: ext)
    class_eval(&block) if block_given?
  end
end