Class: Nanoc::DataSource Abstract
- Inherits:
-
Object
- Object
- Nanoc::DataSource
- 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).
Instance Attribute Summary (collapse)
-
- (Hash) config
readonly
The configuration for this data source.
-
- (String) items_root
readonly
The root path where items returned by this data source should be mounted.
-
- (String) layouts_root
readonly
The root path where layouts returned by this data source should be mounted.
Instance Method Summary (collapse)
-
- (void) down
Brings down the connection to the data.
-
- (DataSource) initialize(site_config, items_root, layouts_root, config)
constructor
A new instance of DataSource.
-
- (Enumerable) items
Returns the collection of items (represented by Int::Item) in this site.
-
- (Enumerable) layouts
Returns the collection of layouts (represented by Int::Layout) in this site.
- - (void) loading
-
- (Object) new_item(content, attributes, identifier, binary: false)
Creates a new in-memory item instance.
-
- (Object) new_layout(raw_content, attributes, identifier)
Creates a new in-memory layout instance.
-
- (void) unuse
Marks the data source as unused by the caller.
-
- (void) up
Brings up the connection to the data.
-
- (void) use
Marks the data source as used by the caller.
Constructor Details
- (DataSource) initialize(site_config, items_root, layouts_root, config)
Returns a new instance of DataSource
32 33 34 35 36 37 38 39 |
# File 'lib/nanoc/base/repos/data_source.rb', line 32 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
- (Hash) config (readonly)
Returns The configuration for this data source. For example, online data sources could contain authentication details.
28 29 30 |
# File 'lib/nanoc/base/repos/data_source.rb', line 28 def config @config end |
- (String) items_root (readonly)
Returns The root path where items returned by this data source should be mounted.
20 21 22 |
# File 'lib/nanoc/base/repos/data_source.rb', line 20 def items_root @items_root end |
- (String) layouts_root (readonly)
Returns The root path where layouts returned by this data source should be mounted.
24 25 26 |
# File 'lib/nanoc/base/repos/data_source.rb', line 24 def layouts_root @layouts_root end |
Instance Method Details
- (void) down
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.
99 100 |
# File 'lib/nanoc/base/repos/data_source.rb', line 99 def down end |
- (Enumerable) items
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.
112 113 114 |
# File 'lib/nanoc/base/repos/data_source.rb', line 112 def items [] end |
- (Enumerable) layouts
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.
126 127 128 |
# File 'lib/nanoc/base/repos/data_source.rb', line 126 def layouts [] end |
- (void) loading
This method returns an undefined value.
Loads the data source when necessary (calling #up), yields, and unloads (using #down) the data source when it is not being used elsewhere. All data source queries and data manipulations should be wrapped in a #loading block; it ensures that the data source is loaded when necessary and makes sure the data source does not get unloaded while it is still being used elsewhere.
49 50 51 52 53 54 |
# File 'lib/nanoc/base/repos/data_source.rb', line 49 def loading use yield ensure unuse end |
- (Object) new_item(content, attributes, identifier, binary: false)
Creates a new in-memory item instance. This is intended for use within the #items method.
142 143 144 145 |
# File 'lib/nanoc/base/repos/data_source.rb', line 142 def new_item(content, attributes, identifier, binary: false) content = Nanoc::Int::Content.create(content, binary: binary) Nanoc::Int::Item.new(content, attributes, identifier) end |
- (Object) new_layout(raw_content, attributes, identifier)
Creates a new in-memory layout instance. This is intended for use within the #layouts method.
155 156 157 |
# File 'lib/nanoc/base/repos/data_source.rb', line 155 def new_layout(raw_content, attributes, identifier) Nanoc::Int::Layout.new(raw_content, attributes, identifier) end |
- (void) unuse
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).
75 76 77 78 |
# File 'lib/nanoc/base/repos/data_source.rb', line 75 def unuse @references -= 1 down if @references == 0 end |
- (void) up
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.
88 89 |
# File 'lib/nanoc/base/repos/data_source.rb', line 88 def up end |
- (void) use
63 64 65 66 |
# File 'lib/nanoc/base/repos/data_source.rb', line 63 def use up if @references == 0 @references += 1 end |