Class: Tabster::TabBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/tabster/tab_builder.rb

Instance Method Summary collapse

Instance Method Details

#build(options, &block) ⇒ Object

Construct and return a tab with the given path and options.



7
8
9
10
11
12
# File 'lib/tabster/tab_builder.rb', line 7

def build(options, &block)
  segments = extract_tab_segments(options)
  puts "EXTRACTED FRAGMENTS: #{segments.inspect}"
  tab = Tab.new(segments, &block)
  tab.freeze
end

#extract_path_segment(path_to) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/tabster/tab_builder.rb', line 30

def extract_path_segment(path_to)
  case path_to.class.to_s
    when 'String'
      normalize_path(wrap_path_with_slashes(path_to.to_s))
    when 'Symbol'
      normalize_path(wrap_path_with_slashes(path_to.to_s))
    when 'Hash'
      # if controller key given, make action an explicit param
      action = path_to[:action].nil? ? 'index' : path_to[:action]
      if path_to[:controller] 
        normalize_path("#{path_to[:controller]}/#{action}")
      else
        raise ArgumentError, ":path_to is invalid in tab parameters. Need to specify at least the controller, i.e. :controller => :admin"
      end
  end
end

#extract_tab_segments(options) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/tabster/tab_builder.rb', line 14

def extract_tab_segments(options)
  segments = {}
  options.each do |key, value|
    if [:path_to].include?(key)
      segments[key] = extract_path_segment(value)
    elsif [:priority].include?(key)
      segments[key] = value.to_i
    elsif [:highlights_on].include?(key)
      if value.is_a? Proc
        segments[key] = value
      end
    end
  end
  segments
end

#normalize_path(path) ⇒ Object

Returns a path cleaned of double-shlashes and relative path references.



53
54
55
56
57
58
59
60
61
62
# File 'lib/tabster/tab_builder.rb', line 53

def normalize_path(path)
  path = path.
    gsub("//", "/"). 
    gsub("\\\\", "\\").
    gsub(%r{(.)[\\/]$}, '\1')

  re = %r{[^/\\]+[/\\]\.\.[/\\]}
  path.gsub!(re, "") while path.match(re)
  path
end

#wrap_path_with_slashes(path) ⇒ Object



47
48
49
50
# File 'lib/tabster/tab_builder.rb', line 47

def wrap_path_with_slashes(path)
  path = "/#{path}" unless path[0] == ?/
  path = "#{path}/" unless path[-1] == ?/
end