Class: Nanoc::Core::CodeSnippet Private

Inherits:
Object
  • Object
show all
Includes:
ContractsSupport
Defined in:
lib/nanoc/core/code_snippet.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Nanoc::Core::CodeSnippet represent a piece of custom code of a Nanoc site.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ContractsSupport

enabled?, included, setup_once, warn_about_performance

Constructor Details

#initialize(data, filename) ⇒ CodeSnippet

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Creates a new code snippet.

Parameters:

  • data (String)

    The raw source code which will be executed before compilation

  • filename (String)

    The filename corresponding to this code snippet



28
29
30
31
# File 'lib/nanoc/core/code_snippet.rb', line 28

def initialize(data, filename)
  @data     = data
  @filename = filename
end

Instance Attribute Details

#dataString (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

A string containing the actual code in this code snippet.

Returns:



14
15
16
# File 'lib/nanoc/core/code_snippet.rb', line 14

def data
  @data
end

#filenameString (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The filename corresponding to this code snippet.

Returns:



19
20
21
# File 'lib/nanoc/core/code_snippet.rb', line 19

def filename
  @filename
end

Instance Method Details

#inspectObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



58
59
60
# File 'lib/nanoc/core/code_snippet.rb', line 58

def inspect
  "<#{self.class} filename=\"#{filename}\">"
end

#loadvoid

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Loads the code by executing it.



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/nanoc/core/code_snippet.rb', line 37

def load
  # rubocop:disable Security/Eval
  eval(<<~CODE, TOPLEVEL_BINDING)
    unless respond_to?(:use_helper)
      def self.use_helper(mod)
        Nanoc::Core::Context.instance_eval { include mod }
      end
    end
  CODE
  eval(@data, TOPLEVEL_BINDING, @filename)
  # rubocop:enable Security/Eval
  nil
end

#referenceObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns an object that can be used for uniquely identifying objects.

Returns:

  • (Object)

    An unique reference to this object



54
55
56
# File 'lib/nanoc/core/code_snippet.rb', line 54

def reference
  "code_snippet:#{filename}"
end