Class: Utopia::Content::Links
- Inherits:
-
Object
- Object
- Utopia::Content::Links
- Defined in:
- lib/utopia/content/links.rb
Overview
Links are essentially a static list of information relating to the structure of the content. They are formed from the ‘links.yaml` file and the actual files on disk.
Constant Summary collapse
- DEFAULT_INDEX_OPTIONS =
{ :directories => true, :files => true, :virtuals => true, :indices => false, :sort => :order, :display => :display, }
- XNODE_FILTER =
/^(.+)#{Regexp.escape XNODE_EXTENSION}$/- INDEX_XNODE_FILTER =
/^(index(\..+)*)#{Regexp.escape XNODE_EXTENSION}$/- LINKS_YAML =
"links.yaml"- DEFAULT_OPTIONS =
{ :directories => true, :files => true, :virtuals => true, :indices => true, }
Instance Attribute Summary collapse
-
#named ⇒ Object
readonly
Returns the value of attribute named.
-
#ordered ⇒ Object
readonly
Returns the value of attribute ordered.
-
#top ⇒ Object
readonly
Returns the value of attribute top.
Class Method Summary collapse
Instance Method Summary collapse
- #each(variant) ⇒ Object
-
#initialize(root, top = Path.new, options = DEFAULT_OPTIONS) ⇒ Links
constructor
A new instance of Links.
- #lookup(name, variant = nil) ⇒ Object
Constructor Details
#initialize(root, top = Path.new, options = DEFAULT_OPTIONS) ⇒ Links
Returns a new instance of Links.
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/utopia/content/links.rb', line 93 def initialize(root, top = Path.new, = DEFAULT_OPTIONS) @top = top = @path = File.join(root, top.components) = self.class.(@path) @ordered = [] @named = Hash.new{|h,k| h[k] = []} if File.directory? @path load_links(.dup) do |link| @ordered << link @named[link.name] << link end end end |
Instance Attribute Details
#named ⇒ Object (readonly)
Returns the value of attribute named.
113 114 115 |
# File 'lib/utopia/content/links.rb', line 113 def named @named end |
#ordered ⇒ Object (readonly)
Returns the value of attribute ordered.
112 113 114 |
# File 'lib/utopia/content/links.rb', line 112 def ordered @ordered end |
#top ⇒ Object (readonly)
Returns the value of attribute top.
111 112 113 |
# File 'lib/utopia/content/links.rb', line 111 def top @top end |
Class Method Details
.for(root, path, variant = nil) ⇒ Object
29 30 31 32 33 |
# File 'lib/utopia/content/links.rb', line 29 def self.for(root, path, variant = nil) links = self.new(root, path.dirname) links.lookup(path.last, variant) end |
.index(root, path, options = {}) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/utopia/content/links.rb', line 44 def self.index(root, path, = {}) = DEFAULT_INDEX_OPTIONS.merge() ordered = self.new(root, path, ).ordered # This option filters a link based on the display parameter. if display_key = [:display] ordered.reject!{|link| link.info[display_key] == false} end # Named: if name = [:name] ordered.select!{|link| link.name[[:name]]} end if variant = [:variant] variants = {} ordered.each do |link| if link.variant == variant variants[link.name] = link elsif link.variant == nil variants[link.name] ||= link end end ordered = variants.values end # Sort: if sort_key = [:sort] # Sort by sort_key, otherwise by title. ordered.sort_by!{|link| [link[sort_key] || [:sort_default] || 0, link.title]} end return ordered end |
Instance Method Details
#each(variant) ⇒ Object
115 116 117 118 119 120 121 |
# File 'lib/utopia/content/links.rb', line 115 def each(variant) return to_enum(:each, variant) unless block_given? ordered.each do |links| yield links.find{|link| link.variant == variant} end end |
#lookup(name, variant = nil) ⇒ Object
123 124 125 126 127 128 |
# File 'lib/utopia/content/links.rb', line 123 def lookup(name, variant = nil) # This allows generic links to serve any variant requested. if links = @named[name] links.find{|link| link.variant == variant} || links.find{|link| link.variant == nil} end end |