class SwedbankPay::SidebarPath

Represents a page in the Sidebar

Attributes

level[R]
name[R]
parent[R]

Public Class Methods

new(path) click to toggle source
# File lib/sidebar_path.rb, line 11
def initialize(path)
  @segments = segment(path)
  @path = normalized(path)
  @name = construct_name
  @parent = find_parent
end

Public Instance Methods

inspect() click to toggle source
# File lib/sidebar_path.rb, line 22
def inspect
  to_s
end
to_s() click to toggle source
# File lib/sidebar_path.rb, line 18
def to_s
  @path
end

Private Instance Methods

construct_name() click to toggle source
# File lib/sidebar_path.rb, line 47
def construct_name
  return '/' if @path == '/'

  @segments.last
end
directory?(path) click to toggle source
# File lib/sidebar_path.rb, line 43
def directory?(path)
  path.end_with?('/') || path.end_with?('/index.html')
end
find_parent() click to toggle source
# File lib/sidebar_path.rb, line 53
def find_parent
  # If there's no or only one path segment, this is a root page
  return nil if @segments.empty? || (@segments.length == 1)

  # Return the path minus the last segment as the parent path
  last = @path.sub(%r{(/#{@segments.last}/?)$}, '')
  "#{last}/"
end
normalized(path) click to toggle source
# File lib/sidebar_path.rb, line 34
def normalized(path)
  return '/' if @segments.empty?

  joined = "/#{@segments.join('/')}"

  # Directory paths should end with '/'.
  directory?(path) ? "#{joined}/" : joined
end
segment(path) click to toggle source
# File lib/sidebar_path.rb, line 28
def segment(path)
  segments = path.sanitized.split('/').reject(&:empty?)
  @level = segments.length.zero? ? 0 : segments.length - 1
  segments
end