Sha256: 8c3f02bb2116207e96ab8efbc09f6af3c0f53f7432b4ba152aedc8ab376f7892

Contents?: true

Size: 1.33 KB

Versions: 37

Compression:

Stored size: 1.33 KB

Contents

# frozen_string_literal: true

module Nanoc::Int
  # Nanoc::Int::CodeSnippet represent a piece of custom code of a Nanoc site.
  #
  # @api private
  class CodeSnippet
    include Nanoc::Int::ContractsSupport

    # A string containing the actual code in this code snippet.
    #
    # @return [String]
    attr_reader :data

    # The filename corresponding to this code snippet.
    #
    # @return [String]
    attr_reader :filename

    contract String, String => C::Any
    # Creates a new code snippet.
    #
    # @param [String] data The raw source code which will be executed before
    #   compilation
    #
    # @param [String] filename The filename corresponding to this code snippet
    def initialize(data, filename)
      @data     = data
      @filename = filename
    end

    contract C::None => nil
    # Loads the code by executing it.
    #
    # @return [void]
    def load
      eval('def self.use_helper(mod); Nanoc::Int::Context.instance_eval { include mod }; end', TOPLEVEL_BINDING)
      eval(@data, TOPLEVEL_BINDING, @filename)
      nil
    end

    # Returns an object that can be used for uniquely identifying objects.
    #
    # @return [Object] An unique reference to this object
    def reference
      "code_snippet:#{filename}"
    end

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

Version data entries

37 entries across 37 versions & 1 rubygems

Version Path
nanoc-4.11.0 lib/nanoc/base/entities/code_snippet.rb
nanoc-4.10.4 lib/nanoc/base/entities/code_snippet.rb
nanoc-4.10.3 lib/nanoc/base/entities/code_snippet.rb
nanoc-4.10.2 lib/nanoc/base/entities/code_snippet.rb
nanoc-4.10.1 lib/nanoc/base/entities/code_snippet.rb
nanoc-4.10.0 lib/nanoc/base/entities/code_snippet.rb
nanoc-4.9.9 lib/nanoc/base/entities/code_snippet.rb
nanoc-4.9.8 lib/nanoc/base/entities/code_snippet.rb
nanoc-4.9.7 lib/nanoc/base/entities/code_snippet.rb
nanoc-4.9.6 lib/nanoc/base/entities/code_snippet.rb
nanoc-4.9.5 lib/nanoc/base/entities/code_snippet.rb
nanoc-4.9.4 lib/nanoc/base/entities/code_snippet.rb
nanoc-4.9.3 lib/nanoc/base/entities/code_snippet.rb
nanoc-4.9.2 lib/nanoc/base/entities/code_snippet.rb
nanoc-4.9.1 lib/nanoc/base/entities/code_snippet.rb
nanoc-4.9.0 lib/nanoc/base/entities/code_snippet.rb
nanoc-4.8.19 lib/nanoc/base/entities/code_snippet.rb
nanoc-4.8.18 lib/nanoc/base/entities/code_snippet.rb
nanoc-4.8.17 lib/nanoc/base/entities/code_snippet.rb
nanoc-4.8.16 lib/nanoc/base/entities/code_snippet.rb