Class: Nanoc::DataSource Abstract
- Inherits:
-
Object
- Object
- Nanoc::DataSource
- Extended by:
- DDPlugin::Plugin
- Defined in:
- lib/nanoc/base/repos/data_source.rb
Overview
Responsible for loading site data. It is the (abstract) superclass for all data sources. Subclasses must at least implement the data reading methods (#items and #layouts).
Apart from the methods for loading and storing data, there are the #up and #down methods for bringing up and tearing down the connection to the data source. These should be overridden in subclasses. The #loading method wraps #up and #down. #loading is a convenience method for the more low-level methods #use and #unuse, which respectively increment and decrement the reference count; when the reference count goes from 0 to 1, the data source will be loaded (#up will be called) and when the reference count goes from 1 to 0, the data source will be unloaded (#down will be called).
Direct Known Subclasses
Int::AggregateDataSource, Int::InMemDataSource, Int::PrefixedDataSource
Instance Attribute Summary collapse
-
#config ⇒ Hash
readonly
The configuration for this data source.
-
#items_root ⇒ String
readonly
The root path where items returned by this data source should be mounted.
-
#layouts_root ⇒ String
readonly
The root path where layouts returned by this data source should be mounted.
Instance Method Summary collapse
-
#down ⇒ void
Brings down the connection to the data.
-
#initialize(site_config, items_root, layouts_root, config) ⇒ DataSource
constructor
A new instance of DataSource.
-
#items ⇒ Enumerable
Returns the collection of items (represented by Int::Item) in this site.
-
#layouts ⇒ Enumerable
Returns the collection of layouts (represented by Int::Layout) in this site.
-
#new_item(content, attributes, identifier, binary: false, checksum_data: nil, content_checksum_data: nil, attributes_checksum_data: nil) ⇒ Object
Creates a new in-memory item instance.
-
#new_layout(raw_content, attributes, identifier, checksum_data: nil, content_checksum_data: nil, attributes_checksum_data: nil) ⇒ Object
Creates a new in-memory layout instance.
-
#unuse ⇒ void
Marks the data source as unused by the caller.
-
#up ⇒ void
Brings up the connection to the data.
-
#use ⇒ void
Marks the data source as used by the caller.
Constructor Details
#initialize(site_config, items_root, layouts_root, config) ⇒ DataSource
Returns a new instance of DataSource
34 35 36 37 38 39 40 41 |
# File 'lib/nanoc/base/repos/data_source.rb', line 34 def initialize(site_config, items_root, layouts_root, config) @site_config = site_config @items_root = items_root @layouts_root = layouts_root @config = config || {} @references = 0 end |
Instance Attribute Details
#config ⇒ Hash (readonly)
Returns The configuration for this data source. For example, online data sources could contain authentication details.
30 31 32 |
# File 'lib/nanoc/base/repos/data_source.rb', line 30 def config @config end |
#items_root ⇒ String (readonly)
Returns The root path where items returned by this data source should be mounted.
22 23 24 |
# File 'lib/nanoc/base/repos/data_source.rb', line 22 def items_root @items_root end |
#layouts_root ⇒ String (readonly)
Returns The root path where layouts returned by this data source should be mounted.
26 27 28 |
# File 'lib/nanoc/base/repos/data_source.rb', line 26 def layouts_root @layouts_root end |
Instance Method Details
#down ⇒ void
This method returns an undefined value.
Brings down the connection to the data. This method should undo the effects of the #up method. For example, a database connection established in #up should be closed in this method.
Subclasses may override this method, but are not required to do so; the default implementation simply does nothing.
85 |
# File 'lib/nanoc/base/repos/data_source.rb', line 85 def down; end |
#items ⇒ Enumerable
Returns the collection of items (represented by Int::Item) in this site. The default implementation simply returns an empty array.
Subclasses should not prepend items_root
to the item’s identifiers, as
this will be done automatically.
Subclasses may override this method, but are not required to do so; the default implementation simply does nothing.
97 98 99 |
# File 'lib/nanoc/base/repos/data_source.rb', line 97 def items [] end |
#layouts ⇒ Enumerable
Returns the collection of layouts (represented by Int::Layout) in this site. The default implementation simply returns an empty array.
Subclasses should prepend layout_root
to the layout’s identifiers,
since this is not done automatically.
Subclasses may override this method, but are not required to do so; the default implementation simply does nothing.
111 112 113 |
# File 'lib/nanoc/base/repos/data_source.rb', line 111 def layouts [] end |
#new_item(content, attributes, identifier, binary: false, checksum_data: nil, content_checksum_data: nil, attributes_checksum_data: nil) ⇒ Object
Creates a new in-memory item instance. This is intended for use within the #items method.
133 134 135 136 |
# File 'lib/nanoc/base/repos/data_source.rb', line 133 def new_item(content, attributes, identifier, binary: false, checksum_data: nil, content_checksum_data: nil, attributes_checksum_data: nil) content = Nanoc::Int::Content.create(content, binary: binary) Nanoc::Int::Item.new(content, attributes, identifier, checksum_data: checksum_data, content_checksum_data: content_checksum_data, attributes_checksum_data: attributes_checksum_data) end |
#new_layout(raw_content, attributes, identifier, checksum_data: nil, content_checksum_data: nil, attributes_checksum_data: nil) ⇒ Object
Creates a new in-memory layout instance. This is intended for use within the #layouts method.
152 153 154 |
# File 'lib/nanoc/base/repos/data_source.rb', line 152 def new_layout(raw_content, attributes, identifier, checksum_data: nil, content_checksum_data: nil, attributes_checksum_data: nil) Nanoc::Int::Layout.new(raw_content, attributes, identifier, checksum_data: checksum_data, content_checksum_data: content_checksum_data, attributes_checksum_data: attributes_checksum_data) end |
#unuse ⇒ void
This method returns an undefined value.
Marks the data source as unused by the caller.
Calling this method decreases the internal reference count. When the reference count reaches zero, i.e. when the data source is not used any more, the data source will be unloaded (#down will be called).
62 63 64 65 |
# File 'lib/nanoc/base/repos/data_source.rb', line 62 def unuse @references -= 1 down if @references.zero? end |
#up ⇒ void
This method returns an undefined value.
Brings up the connection to the data. Depending on the way data is stored, this may not be necessary. This is the ideal place to connect to the database, for example.
Subclasses may override this method, but are not required to do so; the default implementation simply does nothing.
75 |
# File 'lib/nanoc/base/repos/data_source.rb', line 75 def up; end |
#use ⇒ void
50 51 52 53 |
# File 'lib/nanoc/base/repos/data_source.rb', line 50 def use up if @references.zero? @references += 1 end |