Class: Decant::File

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

Direct Known Subclasses

Content

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ File

Returns a new instance of File.

Parameters:

  • path (Pathname)


10
11
12
# File 'lib/decant/file.rb', line 10

def initialize(path)
  @path = path
end

Instance Attribute Details

#pathPathname (readonly)

Returns:

  • (Pathname)


7
8
9
# File 'lib/decant/file.rb', line 7

def path
  @path
end

Instance Method Details

#contentString

The “content” part of the file at #path - everything after the end of the frontmatter definition (see Decant::Frontmatter.load for more about frontmatter).

Returns:

  • (String)


19
20
21
# File 'lib/decant/file.rb', line 19

def content
  frontmatter_content[1]
end

#frontmatterHash<Symbol, anything>?

The frontmatter data contained in the file at #path or nil if there’s none (see Decant::Frontmatter.load for more about frontmatter).

Returns:

  • (Hash<Symbol, anything>, nil)


27
28
29
# File 'lib/decant/file.rb', line 27

def frontmatter
  frontmatter_content[0]
end

#frontmatter?(key = nil) ⇒ Boolean

When passing a key the return value indicates whether #frontmatter has the key, when no key is passed it indicates whether the file has any frontmatter at all.

Parameters:

  • key (Symbol, nil) (defaults to: nil)

Returns:

  • (Boolean)


38
39
40
41
# File 'lib/decant/file.rb', line 38

def frontmatter?(key = nil)
  return false if frontmatter.nil?
  key ? frontmatter.key?(key) : true
end

#readString

The full untouched contents of the file at #path.

Returns:

  • (String)


46
47
48
# File 'lib/decant/file.rb', line 46

def read
  path.read
end