Class: Utopia::Content::Links::Resolver

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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(links, top = Path.root) ⇒ Resolver

Returns a new instance of Resolver.

Raises:

  • (ArgumentError)


139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/utopia/content/links.rb', line 139

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

Instance Attribute Details

#namedObject (readonly)

Returns the value of attribute named.



166
167
168
# File 'lib/utopia/content/links.rb', line 166

def named
  @named
end

#orderedObject (readonly)

Returns the value of attribute ordered.



165
166
167
# File 'lib/utopia/content/links.rb', line 165

def ordered
  @ordered
end

#topObject (readonly)

Returns the value of attribute top.



164
165
166
# File 'lib/utopia/content/links.rb', line 164

def top
  @top
end

Instance Method Details

#each(locale) ⇒ Object



172
173
174
175
176
177
178
# File 'lib/utopia/content/links.rb', line 172

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

#indicesObject



168
169
170
# File 'lib/utopia/content/links.rb', line 168

def indices
  return @ordered.select{|link| link.index?}
end

#lookup(name, locale = nil) ⇒ Object



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/utopia/content/links.rb', line 180

def lookup(name, locale = nil)
  # This allows generic links to serve any locale requested.
  if links = @named[name]
    generic_link = nil
    
    links.each do |link|
      if link.locale == locale
        return link
      elsif link.locale.nil?
        generic_link = link
      end
    end
    
    return generic_link
  end
end