class Mapper

Public Class Methods

new(set) click to toggle source

Initialize the Mapper

# File lib/middleman-router/mapper.rb, line 20
def initialize(set)
  @set = set
  @resources = []
end

Public Instance Methods

route(*args, &block) click to toggle source

Route Creates a new route and any child routes

Usage: route :about_us, path: “about-us”, as: “about”

# File lib/middleman-router/mapper.rb, line 29
def route(*args, &block)
  resource = build_resource(args, @scope)

  if resource.enabled?
    @set.add_route(resource.route_name, resource.path)
  end

  # If a block is passed, then let's create routes
  # for each child route in the block
  #
  if block_given?
    previous_scope = @scope
    @scope = resource

    self.instance_exec(&block)

    @scope = previous_scope
  end
end

Private Instance Methods

build_resource(args, scope=nil) click to toggle source

Create a Resource for the route

# File lib/middleman-router/mapper.rb, line 52
def build_resource(args, scope=nil)
  options         = args.last.is_a?(::Hash) ? args.pop : {}
  options[:scope] = scope if scope
  enabled         = !(options[:index] == false)
  route           = args.first

  # Set the route attributes, name, path, etc
  name       = named(route, options)
  route_name = named_route(route, options)
  path       = url_route(route, options)

  # Create and return the route resource
  Resource.new(
    name:       name,
    route_name: route_name,
    path:       path,
    enabled:    enabled
  )
end
named(route_name, options={}) click to toggle source
# File lib/middleman-router/mapper.rb, line 80
def named(route_name, options={})
  # User can override the name
  if options[:as]
    route_name = options[:as]
  end

  # If there is a parent/scope, then let's prepend
  # that route to the named_route
  if options[:scope]
    route_name = "#{route_name}_#{options[:scope].name}"
  end

  route_name
end
named_route(route_name, options={}) click to toggle source

Returns the named_route that can be access as a global helper on the front-end

# File lib/middleman-router/mapper.rb, line 74
def named_route(route_name, options={})
  label = named(route_name, options)
  # Return it as a underscored symbol
  "#{label}_path".underscore.to_sym
end
parameterize(string) click to toggle source

Parameterize a string

# File lib/middleman-router/mapper.rb, line 123
def parameterize(string)
  string = string.to_s

  # Turn unwanted chars into the separator.
  string.gsub!(/[^a-z0-9\-_]+/i, '-')
  string.downcase!

  string
end
url_route(route_name, options={}) click to toggle source

Return the URL fro the route

# File lib/middleman-router/mapper.rb, line 96
def url_route(route_name, options={})
  if options[:url]
    return options[:url]
  end

  # Ability to customize this
  if options[:path]
    if options[:path].empty?
      return "/"
    else
      path = options[:path]
    end
  else
    path = route_name
  end

  # Nest under a parent/scope route if required
  if options[:scope]
    path = "#{options[:scope].path}#{parameterize(path)}"
  else
    path = "/#{parameterize(path)}"
  end

  "#{path}/"
end