Class: Harbor::Layouts

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/harbor/layouts.rb

Instance Method Summary collapse

Constructor Details

#initializeLayouts

Returns a new instance of Layouts.



6
7
8
9
# File 'lib/harbor/layouts.rb', line 6

def initialize
  @map = []
  @default = nil
end

Instance Method Details

#clearObject



41
42
43
44
# File 'lib/harbor/layouts.rb', line 41

def clear
  @default = nil
  @map.clear
end

#default(layout) ⇒ Object



27
28
29
# File 'lib/harbor/layouts.rb', line 27

def default(layout)
  @default = layout
end

#eachObject



31
32
33
# File 'lib/harbor/layouts.rb', line 31

def each
  @map.each { |item| yield item }
end

#map(fragment, layout) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/harbor/layouts.rb', line 11

def map(fragment, layout)
  fragment = fragment.squeeze("*").squeeze("/").sub(%r{/$}, "").gsub("*", ".*")
  specificity = fragment_specificity(fragment)

  regexp = Regexp.new("^#{fragment}")

  if previous = @map.assoc(regexp)
    @map[@map.index(previous)] = [regexp, layout, specificity]
  else
    @map << [regexp, layout, specificity]
  end

  sort!
  @map
end

#match(path) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/harbor/layouts.rb', line 46

def match(path)
  @map.each do |fragment, layout|
    return layout if fragment === path
  end

  return @default
end

#sort!Object



35
36
37
38
39
# File 'lib/harbor/layouts.rb', line 35

def sort!
  @map.sort! do |a, b|
    b[2] <=> a[2]
  end
end