Class: Nanoc2::Code

Inherits:
Object
  • Object
show all
Defined in:
lib/nanoc2/base/code.rb

Overview

Nanoc2::Code represent the custom code of a nanoc site. It contains the textual source code as well as a mtime, which is used to speed up site compilation.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, mtime = nil) ⇒ Code

Creates a new code object. data is the raw source code, which will be executed before compilation. mtime is the time when the code was last modified (optional).



20
21
22
23
# File 'lib/nanoc2/base/code.rb', line 20

def initialize(data, mtime=nil)
  @data  = data
  @mtime = mtime
end

Instance Attribute Details

#dataObject (readonly)

The textual source code representation.



12
13
14
# File 'lib/nanoc2/base/code.rb', line 12

def data
  @data
end

#mtimeObject (readonly)

The time where the code was last modified.



15
16
17
# File 'lib/nanoc2/base/code.rb', line 15

def mtime
  @mtime
end

#siteObject

The Nanoc2::Site this code belongs to.



9
10
11
# File 'lib/nanoc2/base/code.rb', line 9

def site
  @site
end

Instance Method Details

#loadObject

Loads the code by executing it.



26
27
28
# File 'lib/nanoc2/base/code.rb', line 26

def load
  eval(@data, TOPLEVEL_BINDING)
end

#saveObject

Saves the code in the database, creating it if it doesn’t exist yet or updating it if it already exists. Tells the site’s data source to save the code.



33
34
35
36
37
# File 'lib/nanoc2/base/code.rb', line 33

def save
  @site.data_source.loading do
    @site.data_source.save_code(self)
  end
end