Class: Utopia::Content::Links::Resolved

Inherits:
Object
  • Object
show all
Defined in:
lib/utopia/content/links.rb

Overview

Represents a list of Utopia::Content::Link instances relating to the structure of the content. They are formed from the links.yaml file and the actual directory structure on disk.

Constant Summary collapse

DEFAULT_OPTIONS =
{
  :directories => true,
  :files => true,
  :virtuals => true,
  :indices => true,
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(links, top = Path.root, options = DEFAULT_OPTIONS) ⇒ Resolved

Returns a new instance of Resolved.

Raises:

  • (ArgumentError)


117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/utopia/content/links.rb', line 117

def initialize(links, top = Path.root, options = DEFAULT_OPTIONS)
  raise ArgumentError.new("top path must be absolute") unless top.absolute?
  
  @links = links
  
  @top = top
  @options = options
  
  # top.components.first == '', but this isn't a problem here.
  @path = File.join(links.root, top.components)
  
  @ordered = []
  @named = Hash.new{|h,k| h[k] = []}
  
  if File.directory? @path
     = links.(@path)
    
    load_links(.dup) do |link|
      @ordered << link
      @named[link.name] << link
    end
  else
     = {}
  end
end

Instance Attribute Details

#namedObject (readonly)

Returns the value of attribute named.



145
146
147
# File 'lib/utopia/content/links.rb', line 145

def named
  @named
end

#orderedObject (readonly)

Returns the value of attribute ordered.



144
145
146
# File 'lib/utopia/content/links.rb', line 144

def ordered
  @ordered
end

#topObject (readonly)

Returns the value of attribute top.



143
144
145
# File 'lib/utopia/content/links.rb', line 143

def top
  @top
end

Instance Method Details

#each(locale) ⇒ Object



147
148
149
150
151
152
153
# File 'lib/utopia/content/links.rb', line 147

def each(locale)
  return to_enum(:each, locale) unless block_given?
  
  ordered.each do |links|
    yield links.find{|link| link.locale == locale}
  end
end

#lookup(name, locale = nil) ⇒ Object



155
156
157
158
159
160
# File 'lib/utopia/content/links.rb', line 155

def lookup(name, locale = nil)
  # This allows generic links to serve any locale requested.
  if links = @named[name]
    links.find{|link| link.locale == locale} || links.find{|link| link.locale == nil}
  end
end