Module: Stic::Readable

Included in:
File, Layout
Defined in:
lib/stic/readable.rb

Overview

Stic::Readable

Include this module for blobs that have a source file. It provides methods for reading and storing the source file.

Attributes collapse

Construction collapse

Accessors collapse

Actions collapse

Instance Attribute Details

#nameString (readonly)

The blob file name. Will be used as file name in #url_template and therefor in #relative_target_path etc. The #name will be taken from #path‘s file name if not specified in the constructor.

Returns:

  • (String)

    File blob name.



33
34
35
# File 'lib/stic/readable.rb', line 33

def name
  @name
end

#pathPath (readonly)

The virtual path to this file including file name. This path represents the target in the generated output.

The path will be used to derive the #url_template.

Returns:

  • (Path)

    File blob path.



25
26
27
# File 'lib/stic/readable.rb', line 25

def path
  @path
end

#sourcePath (readonly)

The full path to source file.

Returns:

  • (Path)

    Base path.



16
17
18
# File 'lib/stic/readable.rb', line 16

def source
  @source
end

Instance Method Details

#contentString

Return file content.

This method caches file content and should be favored over #read.

Returns:

  • (String)

    Blob content.



83
84
85
# File 'lib/stic/readable.rb', line 83

def content
  @content ||= read
end

#initialize(opts = {}) ⇒ Object

Initialize new file blob object.

Parameters:

  • opts (Hash) (defaults to: {})

    Initialization options.

Options Hash (opts):

  • :source (#to_s)

    Required Full source path. See #source.

  • :path (#to_s)

    Output path. Will use source path if not given. See #path.

  • :name (String)

    Optional blob file name. See #name.



45
46
47
48
49
50
51
52
53
54
# File 'lib/stic/readable.rb', line 45

def initialize(opts = {})
  super

  source = opts.delete(:source) { raise ::ArgumentError.new 'Argument `:source` required.' }
  path   = opts.delete(:path)   { source }

  @source = Path(source).expand
  @path   = Path(path).as_relative
  @name   = opts.delete(:name) || @path.name
end

#readString

Read and return raw content.

It directly read the content from file every time called and should be avoided in favor of #content.

It can be overridden by subclasses implementing special input reading behavior.

Returns:

  • (String)

    Red content.



99
100
101
# File 'lib/stic/readable.rb', line 99

def read
  source_path.read
end

#renderString

Return final output as string.

On file blob this will just returned the unchanged #content.

Returns:

  • (String)

    Blob output.



72
73
74
# File 'lib/stic/readable.rb', line 72

def render
  content
end

#source_pathPath

Return full source path.

Returns:

  • (Path)

    (Absolute) source path.



62
63
64
# File 'lib/stic/readable.rb', line 62

def source_path
  source
end