Class: Utopia::Content::Links

Inherits:
Object
  • Object
show all
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"
DEFAULT_OPTIONS =
{
  :directories => true,
  :files => true,
  :virtuals => true,
  :indices => true,
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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, options = DEFAULT_OPTIONS)
  @top = top
  @options = options
  
  @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

#namedObject (readonly)

Returns the value of attribute named.



113
114
115
# File 'lib/utopia/content/links.rb', line 113

def named
  @named
end

#orderedObject (readonly)

Returns the value of attribute ordered.



112
113
114
# File 'lib/utopia/content/links.rb', line 112

def ordered
  @ordered
end

#topObject (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, options = {})
  options = DEFAULT_INDEX_OPTIONS.merge(options)
  
  ordered = self.new(root, path, options).ordered
  
  # This option filters a link based on the display parameter.
  if display_key = options[:display]
    ordered.reject!{|link| link.info[display_key] == false}
  end
  
  # Named:
  if name = options[:name]
    ordered.select!{|link| link.name[options[:name]]}
  end
  
  if variant = options[: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 = options[:sort]
    # Sort by sort_key, otherwise by title.
    ordered.sort_by!{|link| [link[sort_key] || options[: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