Module: Nanoc::Helpers::ChildParent
- Defined in:
- lib/nanoc/helpers/child_parent.rb
Overview
Provides functionality for fetching the children and the parent of a given item. This works for both full identifiers and legacy identifiers.
Instance Method Summary collapse
-
#children_of(item) ⇒ Object
Returns the children of the given item.
-
#parent_of(item) ⇒ Object
Returns the parent of the given item.
Instance Method Details
#children_of(item) ⇒ Object
Returns the children of the given item.
For items with legacy identifiers, the children are the items where the identifier contains one more component than the identifier of the given item. For example, the children of the “/projects/” item could be “/projects/nanoc/” and “/projects/cri/”, but not “/about/” nor “/projects/nanoc/history/”.
For items with full identifiers, the children are the item where the identifier starts with the identifier of the given item, minus the extension, followed by a slash. For example, the children of the “/projects.md” item could be the “/projects/nanoc.md” and “/projects/cri.adoc” items , but not “/about.md” nor “/projects/nanoc/history.md”.
42 43 44 45 46 47 48 49 |
# File 'lib/nanoc/helpers/child_parent.rb', line 42 def children_of(item) if item.identifier.legacy? item.children else pattern = item.identifier.without_ext + '/*' @items.find_all(pattern) end end |
#parent_of(item) ⇒ Object
Returns the parent of the given item.
For items with legacy identifiers, the parent is the item where the identifier contains one less component than the identifier of the given item. For # example, the parent of the “/projects/nanoc/” item is the “/projects/” item.
For items with full identifiers, the parent is the item where the identifier contains one less component than the identifier of the given item, and ends with any extension. For example, the parent of the “/projects/nanoc.md” item could be the “/projects.md” item, or the “/projects.html” item, etc. Note that the parent is ambiguous for items that have a full identifier; only the first candidate parent item will be returned.
19 20 21 22 23 24 25 26 |
# File 'lib/nanoc/helpers/child_parent.rb', line 19 def parent_of(item) if item.identifier.legacy? item.parent else path_without_last_component = item.identifier.to_s.sub(/[^\/]+$/, '').chop @items[path_without_last_component + '.*'] end end |